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

Advertisements

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

Feeding a function result into a pattern matching lambda expression 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

Lambda expressions in F#

Lambda expressions in F# are inline anonymous functions, i.e. functions without a name. They are typically short and concise functions that are not meant to be reused.

Here’s a normal F# function that adds three numbers:

let addThreeNumbers x y z = x + y + z

This is a conventional named function that can be invoked from other parts of the application. We can convert it to a lambda function using the “fun” keyword. However, it must be invoked in place.

Read more of this post

More complex pattern matching in F#

Pattern matching branches in F# can be more advanced using the when keyword. Consider the following list matching function:

let listMatcher (l:list<'a>) =
    match l with
        | _ when l.IsEmpty -> "This is an empty list"
        | _ when l.Length > 10 -> "This is a large list"
        | _ when l.Length < 10 -> "This is a small list"
        | _ -> l |> List.map (sprintf "%A") |> String.concat ","

Read more of this post

Basic pattern matching in F#

Pattern matching in F# is somewhat similar to switch blocks in C#. However, pattern matching blocks can provide much more complex branching logic than switch blocks.

Consider the following F# function:

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. In other words the return type is a tuple with two elements, a boolean and an integer.

Read more of this post

ultimatemindsettoday

A great WordPress.com site

Elliot Balynn's Blog

A directory of wonderful thoughts

Softwarearchitektur in der Praxis

Wissenswertes zu Webentwicklung, Domain-Driven Design und Microservices

Technology Talks

on Microsoft technologies, Web, Android and others

Software Engineering

Web development

Disparate Opinions

Various tidbits

chsakell's Blog

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

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: