Skip to content

Instantly share code, notes, and snippets.

@manuelsidler
Last active May 25, 2019 14:44
Show Gist options
  • Save manuelsidler/6cc970bd32759cbbdb7b80b9a5ba8d55 to your computer and use it in GitHub Desktop.
Save manuelsidler/6cc970bd32759cbbdb7b80b9a5ba8d55 to your computer and use it in GitHub Desktop.
F# collections with Option module
let tryLoadCustomerWithOption customerId =
// customerId // does not work as function is expecting an option type
Some customerId // unwrap value first
|> Option.filter(fun x -> x >= 2 && x <= 7)
|> Option.map(fun x -> sprintf "Customer %i" x)
let tryLoadCustomer customerId =
if customerId >= 2 && customerId <= 7 then Some(sprintf "Customer %i" customerId)
else None
let customerIds = [ 0 .. 10 ]
customerIds |> List.choose tryLoadCustomer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment