Question

Further to my last question:

let safeInput:Map<'a,'b> option -> 'a -> 'b -> Map<'a,'b> option = fun x y z ->
    match x with
    | Some d -> Some(d.Add(y,z))
    | None -> Some([y,z]|>Map.ofList)

I understand how simple higher-order functions are declared as a type... reading from right to left if '->' is used more than once to decipher what it returns.. i.e. a higher order function.

For the above example from a book... I understand this is adding to a map, I'm just a bit hazy on how the top line should be deciphered? And the use of the 'fun' keyword?

Was it helpful?

Solution

So the first line states that safeInput is a function which takes a Map option a key a value and then returns a map option. The fun keyword is just a way of creating the function.

An equivalent function is

let safeInput (x:Map<'a,'b> option) (y:'a) (z:'b) :Map<'a,'b> option =
        match x with
            | Some d -> Some(d.Add(y,z))
            | None -> Some([y,z]|>Map.ofList)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top