-
Notifications
You must be signed in to change notification settings - Fork 5
/
functor.ghci
59 lines (36 loc) · 881 Bytes
/
functor.ghci
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
:load TreeMap
--# map
map (+1) [1,2,3]
map show [1,2,3]
:type map show
--# treeLengths
let tree = Node (Leaf "foo") (Node (Leaf "x") (Leaf "quux"))
treeLengths tree
treeMap length tree
treeMap (odd . length) tree
--# fmap
fmap length ["foo","quux"]
fmap length (Node (Leaf "Livingstone") (Leaf "I presume"))
--# id
fmap id (Node (Leaf "a") (Leaf "b"))
--# EitherInt
:load EitherInt
--# EitherIntFlexible
:load EitherIntFlexible
fmap (== "cheeseburger") (Left 1 :: Either Int String)
fmap (== "cheeseburger") (Right "fries" :: Either Int String)
--# shape
fmap odd (Just 1)
fmap odd Nothing
--# composition
(fmap even . fmap length) (Just "twelve")
fmap (even . length) (Just "twelve")
--# operator
(1+) `fmap` [1,2,3] ++ [4,5,6]
--# prefix
fmap (1+) ([1,2,3] ++ [4,5,6])
--# applicative
:m +Control.Applicative
(1+) <$> Just 2
--# invalid
:load ValidFunctor