Monday, 28 April 2014

Copying sequences of records to and from Excel

The F# Journal just published an article about interoperability and reflection:
"This article walks through the design and implementation of a small library that allows sequences of values of F# record types to be read from and written to Excel easily. The implementation includes a signature file with Intellisense documentation and example F# script files..."
To read this article and more, subscribe to The F# Journal today!

Monday, 17 March 2014

Call graph visualizer

The F# Journal just published an article about metaprogramming:
"The call graph of a program is the network of functions and caller-callee dependencies between them. This article walks through the design and implementation of a simple F# program that extracts the call graph of a reflected definition and visualizes it using the GraphViz toolkit..."
To read this article and more, subscribe to The F# Journal today!

Wednesday, 22 January 2014

Function call performance on .NET

The F# Journal just published an article about performance:
"In order to write efficient programs it is essential to have an accurate mental model of the cost of various operations. Function calls are one of the most important operations. This article examines various kinds of function calls and other constructs that are used equivalently and studies their performance in detail..."
To read this article and more, subscribe to The F# Journal today!

Wednesday, 8 January 2014

BCL vs F#: lists

The F# Journal just published an article about libraries:
"The new immutable collections library for the BCL includes a several purely functional data structures. This article is the first in a series comparing and contrasting F# collections with these new BCL counterparts, starting with the humble list..."
To read this article and more, subscribe to The F# Journal today!

Monday, 6 January 2014

Parsing ints quickly

The built-in .NET library functions for parsing integers are very easy to use but a lower-level approach can be around 6x faster. Here is a function that parses a sequence of 32-bit integers:

  let intsOfString (s: string) =
    let ints = ResizeArray()
    let rec inside n i =
      if i = s.Length then
        ints.Add n
      else
        let c = s.[i]
        if '0' <= c && c <= '9' then
          inside (10*n + int c - 48) (i+1)
        else
          ints.Add n
          outside(i+1)
    and outside i =
      if i < s.Length then
        let c = s.[i]
        if '0' <= c && c <= '9' then
          inside (int c - 48) (i+1)
        else
          outside (i+1)
    outside 0
    ints.ToArray()

For example:

> intsOfString "123 456 789";;
val it : int [] = [|123; 456; 789|]

Using the F# CodeDOM to develop a minimal IDE

The F# Journal just published an article about metaprogramming:
"The F# Code DOM and AvalonEdit WPF control provide a great basis for building an integrated development environment for the F# programming language. This article walks through a simple graphical IDE written in 100 lines of F# code that provides error throwback..."
To read this article and more, subscribe to The F# Journal today!

Bit tricks in F#: integer logarithm

The following function computes the base-2 logarithm of the given integer:

let log2 v =
  let mutable v, r = v, 0
  if v &&& 0xFFFF0000 <> 0 then
    let s = 1 <<< 4
    v <- v >>> s
    r <- r ||| s
  if v &&& 0xFF00 <> 0 then
    let s = 1 <<< 3
    v <- v >>> s
    r <- r ||| s
  if v &&& 0xF0 <> 0 then
    let s = 1 <<< 2
    v <- v >>> s
    r <- r ||| s
  if v &&& 0xC <> 0 then
    let s = 1 <<< 1
    v <- v >>> s
    r <- r ||| s
  if v &&& 0x2 <> 0 then
    let s = 1 <<< 0
    r <- r ||| s
  r