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 =
    true

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

ultimatemindsettoday

A great WordPress.com site

iReadable { }

.NET Tips & Tricks

Robin Sedlaczek's Blog

Developer on Microsoft Technologies

HarsH ReaLiTy

A Good Blog is Hard to Find

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

the software architecture

thoughts, ideas, diagrams,enterprise code, design pattern , solution designs

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

Anything around ASP.NET MVC,WEB API, WCF, Entity Framework & AngularJS

Cyber Matters

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

Guru N Guns's

OneSolution To dOTnET.

Johnny Zraiby

Measuring programming progress by lines of code is like measuring aircraft building progress by weight.

%d bloggers like this: