Skip to content

Instantly share code, notes, and snippets.

@alexland
Forked from SteveGilham/gist:e9ef541553e09be75994
Last active August 29, 2015 14:20
Show Gist options
  • Save alexland/6ff9df0e8cd279deb573 to your computer and use it in GitHub Desktop.
Save alexland/6ff9df0e8cd279deb573 to your computer and use it in GitHub Desktop.
pipe operator implemented in scala
package operator
object FunctionalPipeline {
class PipedObject[T] private[Functional] (value:T)
{
def |>[R] (f : T => R) = f(this.value)
}
implicit def toPiped[T] (value:T) = new PipedObject[T](value)
}
// inspired by a blog post in mkaz.com using this operator in a Sieve of Eratosthenes implementation:
(n: Int) => (2 to n) |> (r => r.foldLeft(r.toSet)((ps, x) => if (ps(x)) ps -- (x * x to n by x) else ps))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment