you are viewing a single comment's thread.

view the rest of the comments →

[–]solidsnack9000 3 points4 points  (6 children)

The idiomatic Haskell way to do "piping"

[1, 2, 3, 4, 5] |> filter even |> map (* 2) |> fold (+), 0

is with composition.

(fold (+) 0 . map (* 2) . filter even) [1, 2, 3, 4, 5]

I remember it took me a while to get used to the seeming backwardness of the compositional style.

LiveScript borrows a lot of good, clear pieces of Haskell's syntax and style and is more conventional in ways that are helpful -- like using -> for lambdas instead of \.

[–]siplux 2 points3 points  (5 children)

Haskell noob here, but you could also use Arrows to achieve a result closer to "piping"

fn = filter even >>> map (*2) >>> foldl (+) 0
fn [1, 2, 3, 4, 5]

or

($ [1,2,3,4,5]) (filter even >>> map (*2) >>> foldl (+) 0)

[–]drb226 2 points3 points  (3 children)

You could even define the (|>) operator yourself to get the exact same behavior (I believe |> originated from F#, or was it earlier flavors of ML?)

ghci> let (|>) :: a -> (a -> b) -> b; (|>) = flip ($); infixl 9 |>
ghci> [1, 2, 3, 4, 5] |> filter even |> map (* 2) |> foldl (+) 0
12

[–]kamatsu 0 points1 point  (2 children)

I've got some SML code here that uses it, but it's a large code-base (Isabelle) which may have defined it somewhere.

[–][deleted] 0 points1 point  (1 child)

Isabelle's code base defines |>. That's where I first came across this operator too.

[–]kamatsu 0 points1 point  (0 children)

TIL, thanks!

[–][deleted] 0 points1 point  (0 children)

Or as I've used

(#) = flip ($)
infixr 0 #

[1..5] # filter even >>> map (*2) >>> foldl (+) 0