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
I find right pipe useful on those occasions when you have a chain of pipes, | |
but for one of them you want to pass the data in as the FIRST param, | |
rather than the last. | |
Here's an example: | |
// setup some functions | |
let replace (oldS:string) newS (a:string) = a.Replace(oldS,newS) | |
let add a b = sprintf "%s %s" a b | |
let toUpper (a:string) = a.ToUpper() | |
// pipe "hello" through the functions | |
"hello" | |
|> replace "h" "j" | |
|> add "world" // "world is first param | |
|> toUpper | |
//result "WORLD JELLO" // not what we want | |
// pipe "hello" through the functions | |
"hello" | |
|> replace "h" "j" | |
|> add <| "world" // "world is second param | |
|> toUpper | |
//result "JELLO WORLD" // correct! | |
// A similar approach is useful with defaultArg | |
Some "hello" | |
|> Option.map toUpper | |
|> defaultArg <| "world" | |
// Result "HELLO" | |
None | |
|> Option.map toUpper | |
|> defaultArg <| "world" | |
// Result "world" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment