Skip to content

Instantly share code, notes, and snippets.

@d6y
Last active August 29, 2015 14:08
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 d6y/7940c346466fedd1a906 to your computer and use it in GitHub Desktop.
Save d6y/7940c346466fedd1a906 to your computer and use it in GitHub Desktop.
Pipe

Scalaz: one symbol at a time (OSAAT#1)

Symbol

|> or

Pronounced

  • Pipe
  • The Thrush combinator

Purpose

  • Improved readability for a pipeline of functions.
  • Use where the value of one function is fed as an input to the next function.
  • Analogous to the UNIX pipe operator (|).

##Example

Instead of...

nearby(extractLocations(tweets))

... you can pipe these functions together:

import scalaz.syntax.id._

tweets |> extractLocations |> nearby

where:

  • tweets is a value;
  • the type of that value is expected as a parameter to extractLocations; and
  • the result of extractLocations is the input to the nearby function.

Alternatives

Working with an alternative API, using maps and predicates, you might write:

tweets.flatMap(findLocation).filter(isNearby)

Fun fact

The name "thrush" originates in Raymond Smullyan's To Mock a Mockingbird (and other logic puzzles). From page 100:

"A bird closely related to the cardinal is the thrush," said Bravura. "Why, there is one right over there! A thrush T is defined by the following condition:

Txy = yx

"The thrush is the simplest of the permuting birds," said Bravura. "It is derivable from a cardinal C and an identity bird I. Can you see how?"

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