Last active
August 29, 2015 14:02
-
-
Save talios/bdb79750df7e5cba6d54 to your computer and use it in GitHub Desktop.
Simple implementation of the clojure "thread macro" or F# pipe operator...
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
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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