Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does PureScript have a pipe operator?

Tags:

purescript

Coming from the F# world, I am used to using |> to pipe data into functions:

[1..10] |> List.filter (fun n -> n % 2 = 0) |> List.map (fun n -> n * n);

I assume that PureScript, being inspired by Haskell, has something similar.

How do I use the pipe operator in PureScript?

like image 356
sdgfsdh Avatar asked Dec 20 '16 22:12

sdgfsdh


2 Answers

Yes, you can use # which is defined in Prelude.

Here is your example, rewritten using #:

http://try.purescript.org/?gist=0448c53ae7dc92278ca7c2bb3743832d&backend=core

module Main where

import Prelude
import Data.List ((..))
import Data.List as List

example = 1..10 # List.filter (\n -> n `mod` 2 == 0) 
                # map (\n -> n * n)
like image 167
Phil Freeman Avatar answered Oct 21 '22 08:10

Phil Freeman


Here's one way to define the |> operator for use in PureScript; it's defined in exactly the same way as # - i.e. with the same precedence and associativity:-

pipeForwards :: forall a b. a -> (a -> b) -> b
pipeForwards x f = f x
infixl 1 pipeForwards as |>
like image 28
Bellarmine Head Avatar answered Oct 21 '22 06:10

Bellarmine Head