Skip to content

Instantly share code, notes, and snippets.

@joescii
Created May 11, 2015 03:25
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 joescii/514c4f95cea2df8121d4 to your computer and use it in GitHub Desktop.
Save joescii/514c4f95cea2df8121d4 to your computer and use it in GitHub Desktop.
Unix-like piping DSL for purescript
> import Fun
> import Data.Array
> 20 >> fibs |> filter isEven |> length
7
module Fun where
import Data.Array (map, filter, (..))
-- Given an argument `a` applies the function `f`
(>>) :: forall a b. a -> (a -> b) -> b
(>>) a f = f a
infix 1 >>
-- Given functions `f` and `g`, composes to yield `g` of `f`
(|>) :: forall a b c. (a -> b) -> (b -> c) -> (a -> c)
(|>) f g a = g $ f a
infixr 2 |>
-- Calculates the nth fibonacci number
fib :: Number -> Number
fib 0 = 1
fib 1 = 1
fib n = fib (n - 1) + fib (n - 2)
-- Returns the first n fibonacci numbers
fibs :: Number -> [Number]
fibs n = 0..n >> map fib
-- Determines if a given number is even
isEven :: Number -> Boolean
isEven n = n % 2 == 0
@joescii
Copy link
Author

joescii commented May 11, 2015

How many of the first 20 Fibonacci numbers are even? Redirect 20 into the pipeline of functions!

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