O'Reilly logo

Testing with F# by Mikael Lundin

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Immutability

The default behavior of F# when using the let keyword is an immutable value. The difference between this and a variable is that the value is not possible to change. With this, there are the following benefits:

  • It encourages a more declarative coding style
  • It discourages side effects
  • It enables parallel computation

The following is an example implementation of the String.Join function using a mutable state:

// F# implementation of String.Join let join (separator : string) list = // create a mutable state let mutable result = new System.Text.StringBuilder() let mutable hasValues = false // iterate over the incoming list for s in list do hasValues <- true result .Append(s.ToString()) .Append(separator) |> ignore // if list hasValues remove ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required