Units of measure in F#

A very interesting feature of F# – besides many others – is that we can assign a unit of measure to a variable value. These units of measure are located in the Microsoft.FSharp.Data.UnitSystems.SI.UnitNames namespace. As the namespace suggests the units are taken from the international system of units.

They are surrounded with angled brackets in code. Here’s an example:

Read more of this post


Code comments in F#

F# uses the double-slash for single line comments like in many other popular programming languages:

//this is a single line comment

You might expect that /* … */ would work for multi-line comments but there’s a slight variation to that:

Read more of this post

Making variables mutable in F#

F# takes immutability of variables very seriously. If a variable is declared with the let keyword then it is also immutable, meaning we can’t change its value after the declaration. In many popular OOP languages immutability only concerns strings but in F# all types are covered.

Here’s an immutable integer:

let thisIsImmutable = 3

Trying to assign a new value will fail:

Read more of this post

Ignoring the return value of a function in F#

From time to time we need to call a function which has a return value which we don’t care about. E.g. a function can perform some action and then return true if it has succeeded but throw an exception otherwise. In that case it would be enough to call the function and ignore the return value assuming that the function succeeded in case no exception was thrown.

Here’s an extremely simple F# function which only returns true:

let booleanFunction someInput =

Read more of this post

Various return types through choices in F#

F# has a type called Choice which makes it possible for a function to return values of different types. A function can return either a boolean, or a string or an integer wrapped in a Choice object. So in fact the function still returns a single return type, i.e. a Choice, but the type contained within a Choices can vary.

Choices are represented by specialised ChoiceOf objects such as Choice1Of2 or Choice2Of7. If a function has, say, 3 possible return types then it must be able return 3 objects: Choice1Of3, Choice2Of3 and Choice3Of3. Let’s see an example:

Read more of this post

Options in F#

There are functions whose purpose is to locate an object in a collection or database. They either find the resource or they don’t. In case they find it then they return it from the function body. What if the resource is not found? E.g. we might be looking for the first number above 100 in an integer list. Then it is either found or not, we don’t know in advance. Mainstream OOP languages like C# or Java would return null or the maybe the default value of the type that’s being searched.

In F# we have the Option object to solve this problem. F# understands the “null” keyword as well but it’s mainly used when interacting with other .NET languages. Otherwise F# has no real notion of null as in an object that’s not pointing to an address in memory. F#’s approximation of a missing value is an Option that has no enclosed object. It’s called None. None is an object that indicates missing data which is not the same as null. Its opposite is called Some which has an enclosed object of some type. Both Some and None create an instance of type Option but None contains nothing.

Read more of this post

Lazy loaded sequences in F#

F# has a number of standard collection types that are available in other popular computing languages: lists, arrays, tuples. There’s, however, an odd one called a sequence. A sequence in F# is a normal collection for IEnumerable types but its members are lazily evaluated. This means that the sequence is not instantiated with all its elements included. Instead, its elements are evaluated when needed.

Here’s how we can declare a sequence:

Read more of this post

LINQ statements in F#

There’s very little difference between executing LINQ statements in C# and F#. Adding a reference to System.Linq in an F# file gives access to the same LINQ extension methods we’ve all come to love. There’s a choice between LINQ extension methods and LINQ queries with lambda statements. Any LINQ statement in C# can be directly ported to F# and will have the same result.

Note that many LINQ statements can be rewritten using built-in functions from the various collection types in F# such as List and Seq, e.g. List.average or Seq.map . In this short post we’ll just look at a couple of LINQ examples. There’s no point in regurgitating the various LINQ functions here. There’s a large collection on this blog available here if you are new to this topic.

Read more of this post

Consuming C# functions with out parameters from F#

C# has a number of functions with “out” parameters such as the various “TryParse” functions like Int32.TryParse. Functions with out parameters typically have a primary return value, like TryParse returns a boolean. The out parameters are also populated within the body of the function. We can call them secondary return values. Here’s an example of using Int32.TryParse:

int res;
bool parseSuccess = Int32.TryParse("123", out res);

TryParse returns true if the string input was successfully parsed into an integer and “res” will be assigned that value. F# has no out values so how can we consume our functions from an F# program?

Read more of this post

Feeding a function result into a pattern matching function in F#

Say we have an F# function that returns a tuple of two elements:

let isGreaterThan x y =     
    if x > y then (true, x - y)
    else (false, 0)

…, i.e. we return true and the difference between the two input integers if the first integer is greater. Otherwise we return a false and a 0. Here’s how we can consume this function:

Read more of this post

Elliot Balynn's Blog

A directory of wonderful thoughts

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog


Once Upon a Camayoc

Bite-size insight on Cyber Security for the not too technical.

%d bloggers like this: