-
Notifications
You must be signed in to change notification settings - Fork 21
/
Box.purs
39 lines (31 loc) · 1.06 KB
/
Box.purs
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
module Data.Box
( Box(..)
) where
import Prelude
import Control.Comonad (class Comonad, class Extend)
-- const Box = x =>
newtype Box a = Box a
-- map: f => Box(f(x))
instance functorBox :: Functor Box where
map f (Box x) = Box (f x)
-- inspect: () => 'Box($(x))'
instance showBox :: Show a => Show (Box a) where
show (Box a) = "Box(" <> show a <> ")"
instance applyBox :: Apply Box where
apply (Box f) (Box x) = Box (f x)
-- module.exports = { Box, of: Box }
instance applicativeBox :: Applicative Box where
pure = Box
-- Bonus: We'll be using these class instances in a later tutorial
-- So you can ignore them for now. But for those who are familer with
-- Monads, take a look at applyDiscount' in Main.purs
-- chain: f => f(x)
instance bindBox :: Bind Box where
bind (Box m) f = f m
instance monadBox :: Monad Box
instance extendBox :: Extend Box where
extend f m = Box (f m)
-- My approach to addressing fold: f => f(x), but instead
-- don't apply a function f, just take x out of the Box
instance comonadBox :: Comonad Box where
extract (Box x) = x