Skip to content

Instantly share code, notes, and snippets.

@swlaschin
Last active January 1, 2016 07:19
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save swlaschin/8111200 to your computer and use it in GitHub Desktop.
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