Data type conversion in F#

F# has much the same primitive data types as C#. Here are some examples:

let integer = 123
let floating = 23.
let dec = 24M
let bool = true
let text = "This is some string"
let character = 'c'

Occasionally we may need to convert one primitive type to another.

Read more of this post

Infix operators in F#

F# makes the distinction between “prefix” and “infix” functions. The word prefix means that something comes before something else. Prepositions in many Indo-European languages, like “in”, “for” and “on” in English are examples of prefixes, they are placed in front of another word, most often a noun: in the house, at the station, for that purpose.

Prefix functions are how we most often define and call functions in F#. The function name comes first which is then followed by the function arguments.

Here’s a function that adds two numbers:

let addTwoNumbers x y = x + y

Unsurprisingly we call the above function as follows:

Read more of this post

Embedded functions in F#

F# allows us to declare a function within a function. The embedded function will only be visible within the encasing function. Indentation is important in this case as the embedded function can also span multiple rows.

Consider the following function:

let calculate x = 
    printfn "Your input parameter was %i" x
    let embedded y z = 
        printfn "%s" "Running the embedded function"
        let a = (x + y) * 2
        x + y + a
    let finalRet = embedded 5 4
    printfn "The result is %i" (finalRet)
    finalRet

let calculateRes = calculate 5

Read more of this post

Function composition in F#

F# lets us build new functions that consists of other functions using the composition operators >> and <<. The direction of the arrows determines how the individual constituent functions are linked.

Consider the following simple functions:

let incrementByOne x = x + 1
let triple x = x * 3
let halve x = x / 2
let double x = x * 2

A common feature of each function is that each accepts a single parameter and has a return value. The input and return types are also identical. These functions are good candidates for function composition.

Here’s how to do it:

Read more of this post

Forward piping in F#

In F# it’s possible to chain function calls and feed the return value of each function to the next using the forward type operator which looks like this: |> . We can literally read it as “pipe forward”.

If we have a function that increments an integer like this…:

let incrementByOne x = x + 1

Then we normally call it as follows:

let res = incrementByOne 10

With forward piping we can supply the input parameter first as follows:

Read more of this post

Partially applied functions in F#

F# has a number of interesting features related to functions. After all it’s a functional language, right? One of those features is called partial application of a function. Partial application means that in F# we can supply the arguments to function at various stages. We don’t have to provide all input parameters at once.

This feature can be explained through function types in F#.

Consider the following function that adds three numbers:

Read more of this post

Anonymous functions in F#

Anonymous functions in F# are used to build functions that are meant to be used within a statement. They have no names so other parts of the application can’t call them.

The following named function…:

let increment x = x + 1

…has the following anonymous counterpart:

Read more of this post

Basic function declaration in F#

We declare functions in F# using the “let” keyword. F# functions are quite spartan compared to their C# and Java counterparts. There are no parentheses, curly braces, commas, semicolons. Type declaration is optional and there’s no need for the “return” keyword if the function returns something. F# functions are similar to LINQ statements we use in C#.

The anatomy of a basic function is the following:

  • The “let” keyword
  • Followed by the function name
  • Followed by input parameters if any
  • Then comes the equality operator ‘=’ which acts as an assignment operator in this case
  • Finally we have the function body assigned to the function with the assignment operator

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

WEB APPLICATION DEVELOPMENT TUTORIALS WITH OPEN-SOURCE PROJECTS

Once Upon a Camayoc

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