-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathpurescript.purs
More file actions
34 lines (29 loc) · 881 Bytes
/
Copy pathpurescript.purs
File metadata and controls
34 lines (29 loc) · 881 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
module Main where
import Prelude
import Effect (Effect)
import Effect.Console (logShow)
import Data.Array
import Data.Maybe
main :: Effect Unit
main = do
let sum = (add 5) 6
logShow sum
let out = each (add 7) ([ 1, 2, 3, 4 ])
logShow out
let out2 = each2 (add 7) ([ 1, 2, 3, 4 ])
logShow out2
-- much like Haskell, this Returns a function adding `x` to its parameter.
add :: Int -> Int -> Int
add = (+)
-- Applies function `fn` to each element in the list
-- effectively a map function
each :: forall a b. (a -> b) -> Array a -> Array b
each fn arr = case uncons arr of
Just res -> (fn res.head) : (each fn res.tail)
Nothing -> []
-- Slightly different way of writing it
each2 :: forall a b. (a -> b) -> Array a -> Array b
each2 fn arr = case (head arr), (tail arr) of
Just x, Just xs -> (fn x) : (each2 fn xs)
Just x, Nothing -> [ (fn x) ]
_, _ -> []