Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.