Skip to content

Instantly share code, notes, and snippets.

@talios
Last active August 29, 2015 14:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save talios/bdb79750df7e5cba6d54 to your computer and use it in GitHub Desktop.
Save talios/bdb79750df7e5cba6d54 to your computer and use it in GitHub Desktop.
Simple implementation of the clojure "thread macro" or F# pipe operator...
implicit class Piper[A](val a: A) extends AnyVal {
def |>[B](fn: (A => B)): B = fn(a)
}
def add5(a: Int) = a + 5
def times2(a: Int) = a * 2
def pipeit(a: Int) = {
(a |> add5
|> times2
|> (_ / 2)
|> (_.toString()))
}
pipeit(15)
@kiwidev
Copy link

kiwidev commented Jun 25, 2014

I really like the scala _ instead of the explicit F# style (fun x -> x / 2)

The pipe comes into its own when functions are all setup to curry the last parameter

a |> (+) 5
  |> (*) 2
  |> (fun x -> x / 2)
  |> sprintf "%f"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment