Last active
May 25, 2019 14:44
-
-
Save manuelsidler/6cc970bd32759cbbdb7b80b9a5ba8d55 to your computer and use it in GitHub Desktop.
F# collections with Option module
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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