-
Notifications
You must be signed in to change notification settings - Fork 1
/
Chart.hs
45 lines (34 loc) · 1.05 KB
/
Chart.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
43
44
45
module Chart where
import Data.Maybe
import Data.Monoid
import Data.Profunctor
import Control.Applicative
import Rect
import R2
newtype Chart a b = Chart { unchart :: a -> Maybe b }
instance Functor (Chart a) where
fmap = rmap
instance Profunctor Chart where
-- lmap :: (b -> a) -> Chart a z -> Chart b z
lmap f (Chart g) = Chart (g . f)
rmap f (Chart g) = Chart (fmap f . g)
instance Applicative (Chart a) where
pure x = Chart (\_ -> Just x)
ff <*> xx = Chart $ \coord -> case (unchart ff coord, unchart xx coord) of
(Just f, Just x) -> Just (f x)
_ -> Nothing
instance Monoid (Chart a b) where
mempty = Chart (\_ -> Nothing)
mappend (Chart f) (Chart g) = Chart h where
h xy = getFirst (First (f xy) <> First (g xy))
at :: Chart a b -> a -> b -> b
at ch xy d = fromMaybe d (unchart ch xy)
at' :: Chart a b -> a -> Maybe b
at' = unchart
rect :: Rect a -> Chart R2 a
rect r = clip r (pure (value r))
clip :: Rect a -> Chart R2 b -> Chart R2 b
clip r ch = Chart $ \(x,y) ->
if outsideRect x y r
then Nothing
else unchart ch (x,y)