Created
May 11, 2015 03:25
-
-
Save joescii/514c4f95cea2df8121d4 to your computer and use it in GitHub Desktop.
Unix-like piping DSL for purescript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
> import Fun | |
> import Data.Array | |
> 20 >> fibs |> filter isEven |> length | |
7 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
How many of the first 20 Fibonacci numbers are even? Redirect 20 into the pipeline of functions!