This repository has been archived by the owner on Jun 15, 2023. It is now read-only.
/
Monad.purs
70 lines (56 loc) · 2.15 KB
/
Monad.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
module ECharts.Monad
( DSL
, DSLMonad(DSL)
, buildObj
, buildArr
, buildSeries
, set
, get
, lastWithKeys
) where
import Prelude
import Control.Monad.Eff (kind Effect)
import Control.Monad.Writer (Writer, execWriter)
import Control.Monad.Writer.Class (tell)
import Data.Array as Arr
import Data.Foldable as F
import Data.Foreign (Foreign, toForeign)
import Data.Maybe (Maybe(..))
import Data.Tuple (Tuple(..), uncurry, snd)
import ECharts.Internal (unsafeSetField, emptyObject)
newtype DSLMonad (i ∷ # Effect) a = DSL (Writer (Array (Tuple String Foreign)) a)
unDSL ∷ ∀ i a. DSLMonad i a → Writer (Array (Tuple String Foreign)) a
unDSL (DSL m) = m
derive newtype instance functorDSL ∷ Functor (DSLMonad i)
derive newtype instance applyDSL ∷ Apply (DSLMonad i)
derive newtype instance applicativeDSL ∷ Applicative (DSLMonad i)
derive newtype instance bindDSL ∷ Bind (DSLMonad i)
derive newtype instance monadDSL ∷ Monad (DSLMonad i)
type DSL i = DSLMonad i Unit
set ∷ ∀ i. String → Foreign → DSL i
set k v = DSL $ tell $ Arr.singleton $ Tuple k v
get ∷ ∀ i. String → DSL i → Maybe Foreign
get k (DSL cs) =
F.foldl (foldFn k) Nothing $ execWriter cs
where
foldFn ∷ String → Maybe Foreign → Tuple String Foreign → Maybe Foreign
foldFn k' Nothing (Tuple kk f) | k' == kk = Just f
foldFn _ a _ = a
lastWithKeys ∷ ∀ i f. F.Foldable f ⇒ f String → DSL i → Maybe Foreign
lastWithKeys ks (DSL cs) =
F.foldl (foldFn ks) Nothing $ Arr.reverse $ execWriter cs
where
foldFn ∷ f String → Maybe Foreign → Tuple String Foreign → Maybe Foreign
foldFn ks' Nothing (Tuple kk f) | F.elem kk ks' = Just f
foldFn _ a _ = a
applyOnePair ∷ Tuple String Foreign → Foreign → Foreign
applyOnePair opt obj = uncurry (unsafeSetField obj) opt
buildObj ∷ ∀ i. DSL i → Foreign
buildObj (DSL cs) =
F.foldr applyOnePair (emptyObject unit) $ execWriter cs
buildSeries ∷ ∀ i. DSL i → Foreign
buildSeries (DSL cs) =
toForeign $ map (\(Tuple ty f) → unsafeSetField f "type" $ toForeign ty) $ execWriter cs
buildArr ∷ ∀ i. DSL i → Foreign
buildArr (DSL cs) =
toForeign $ map snd $ execWriter cs