-
Notifications
You must be signed in to change notification settings - Fork 4
/
Lenses.hs
43 lines (28 loc) · 911 Bytes
/
Lenses.hs
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
module Lenses where
import Prelude hiding ((.), id)
import Control.Category
data Lens s a = Lens (s -> a) ((a -> a) -> s -> s)
instance Category Lens where
id = Lens id id
(Lens lg lm) . (Lens rg rm) = Lens (lg . rg) (rm . lm)
(^.) :: s -> Lens s a -> a
s ^. (Lens f _) = f s
(%=) :: Lens s a -> (a -> a) -> s -> s
((Lens _ m) %= f) s = m f s
(^=) :: Lens s a -> a -> s -> s
l ^= a = l %= (const a)
infixl 8 ^.
infixr 4 %=
infixr 4 ^=
data Point = Point {x :: Double, y :: Double}
deriving (Eq, Show)
xLens :: Lens Point Double
xLens = Lens x (\f p -> p{x= f (x p)})
yLens :: Lens Point Double
yLens = Lens y (\f p -> p{y= f (y p)})
data Circle = Circle {origin::Point, radius :: Double}
deriving (Eq, Show)
radiusLens :: Lens Circle Double
radiusLens = Lens radius (\f c -> c{radius=f (radius c)})
originLens :: Lens Circle Point
originLens = Lens origin (\f c -> c{origin=f (origin c)})