F# – how to use it

Up to this point, we have been discussing the general characteristics of F# without even taking into consideration FRP. In addition, we have seen some of the theoretical aspects of the language. They detailed the general nature of the programming that is functional through simple examples.

Through this section, we will show how you can leverage the features of F#: both syntactic and technical. We will precisely discuss this in the various sections of some constructors required to exploit the full potential of the language. In this way, it will be easier to introduce and above all understand FRP, which will be addressed later.

In detail, you will see the following:

  • Pattern Matching and pipeline for very concise code and functions
  • Record type and Discriminate Union to represent and query simple types in F#
  • Active Pattern to customize the code used in Pattern Matching

Pattern Matching and pipe forward

Pattern Matching is a very important model for comparison. In particular, through Pattern Matching, you can write input data conditions and output actions to transform data from the condition to the right action. Pattern Matching can be a constructor, or a model, or perhaps, even better called a syntax to compare.

It is easy; refer to the following code:

let v1 = 3 
let v2 = 2 
 
let printEvenOdd x = 
    match x % 2 with 
    | 0 -> printfn "The value is odd" 
    | _ -> printfn "The value is even"  
 
printEvenOdd v1 
printEvenOdd v2 

The result for v1 and v2 will be even and odd, respectively.

The composition of this pattern requires an expression of control to indicate the keywords match <expression> with =. Then, it should include a set of models of the condition and the result to be obtained. The expected syntax is the following: | pattern [when condition] -> result-expression.

All of this is similar to the switch case of C#, but in fact, Pattern Matching is much more powerful.

Note

The heart of this matching is the pattern of the condition. The model can be any constant or identifier, any Boolean condition, list, array, tuple, record, type, wildcard, and much more.

In the example, you can see precisely the use of the wildcard (_). This is a jolly character and represents a placeholder for any input value that does not match a specific case of a result or simply groups all the other values in a single output. The wildcard can be seen as the default keyword in the C# switch construct.

In the syntax of the pattern condition | pattern [when condition] -> result-expression, when is an optional keyword to indicate or compare, for example, group-specific cases. For example, the following code is used to compare the first value to the second:

let compareTwoValue x = 
    match x with 
    | (v1, v2) when v1 > v2 -> printfn "%d is greater than %d" v1 v2 
    | (v1, v2) when v1 < v2 -> printfn "%d is less than %d" v1 v2 
    | (v1, v2) -> printfn "%d equals %d" v1 v2 
 
compareTwoValue (0, 1) 
compareTwoValue (1, 0) 
compareTwoValue (0, 0) 

Once you understand the operation of Pattern Matching, it is easy to see that, in the functional-oriented context, writing code using Pattern Matching instead of the usual comparison conditions, such as if <..> then <..> else, is a best practice. In fact, the design of the code turns out to be cleaner and certainly more concise.

Pipeline and composition

Pipe forward is one of the most commonly used operators in F#. It is so common that it has been used since the earliest examples of the chapter. The operation is very simple and it involves the passage of the result of the one work to the next one.

There are two types of operator. They are as follows:

  • The classic pipe forward (|>) passes the result of the operation to the left toward the right function. Refer to the following example:
[ 0..10 ] |> List.iter (fun x -> printfn "the value is %d" x) 
  • The pipe backward (<|) inversely passes the result of the operation to the right toward the left function. Refer to the following example:
List.iter (fun x -> printfn "the value is %d" x) <| [ 0..10] 

The Composition operator (forward is >> and backward is <<) is similar to pipeline, but it is used to concatenate the n functions with a single result, as shown in the following example:

let add1 x = x + 1 
let times2 x = 2 * x 
 
let Compose2 = add1 >> times2 //Forward 
let Compose1 = add1 << times2 //Backward 
 
Compose2 1 //The result is 4 
Compose1 1 //The result is 4 

Note

The main difference between pipeline and composition is the signature and the use. The pipeline operator takes functions and arguments, while composition combines the functions.

Even through these two operators, it is known when the syntax of F# favors a concise code writing and, especially in this case, with the possibility of making it very readable. Nothing prevents you from writing multiple lines respecting the rules of indentation, or just one:

[ 0..10 ] 
|> List.map (fun x -> x * x) 
|> List.iter (fun y -> printfn "the value is: %i " y) 
 
//or 
 
[ 0..10 ] |> List.map (fun x -> x * x) |> List.iter (fun y -> printfn "the value is: %i " y) 
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.119.131.10