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

Implementing a C# interface with an F# object expression

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

Read more of this post

Implementing a C# interface with an F# type

Suppose you have the following C# interface ICalculator with 4 functions:

namespace Project.Domains
{
	public interface ICalculator
	{
		int Add(int x, int y);
		int Subtract(int x, int y);
		float Divide(float x, float y);
		int Multiply(int x, int y);
	}
}

We can implement this interface in an F# type as follows:

Read more of this post

Consuming a C# class in F#

F# and C# can work together pretty easily. Say that your domain classes are contained in a C# class library called Project.Domains. Let’s take the following Product class as an example:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Project.Domains
{
	public class Product
	{
		public Product(string name, int price)
		{
			Name = name;
                        Price = price;
		}

		public string Name { get; }
		public int Price { get; }

		public double CalculateDiscountedPrice(double percentageOff)
		{
			double discount = Price * percentageOff;
			return Price - discount;
		}
	}
}

Read more of this post

Filtering exceptions on exception messages in F#

Here’s an example of pattern matching in a with clause in F#:

let testDotNetExceptions =
    try
        raise (new System.IndexOutOfRangeException("Your list is not large enough"))
    with
        | 😕 System.IndexOutOfRangeException as ex ->
            printfn "Out of range: %s" ex.Message
            0
        | 😕 System.Exception as ex ->
            printfn "Something bad has happened: %s" ex.Message
            0

Read more of this post

Handling .NET exceptions in F#

When working with .NET languages from F# it can happen that the F# code needs to deal with .NET exceptions from the System namespace. The way to deal with those exceptions is about the same as in the case of F# exceptions but pattern matching in the with clause is slightly different:

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: