aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/lux/data/store.lux
blob: 4cacb83295fc11c406cbe7cb1edb3717810ee431 (plain)
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:
  [lux #*
   [control
    ["F" functor]
    comonad]
   [type
    implicit]])

(type: #export (Store s a)
  {#cursor s
   #peek (-> s a)})

(def: (extend f wa)
  (All [s a b] (-> (-> (Store s a) b) (Store s a) (Store s b)))
  {#cursor (get@ #cursor wa)
   #peek (function (_ s) (f (set@ #cursor s wa)))})

(structure: #export Functor<Store> (All [s] (F.Functor (Store s)))
  (def: (map f fa)
    (extend (function (_ store)
              (f (:: store peek (:: store cursor))))
            fa)))

(structure: #export CoMonad<Store> (All [s] (CoMonad (Store s)))
  (def: functor Functor<Store>)

  (def: (unwrap wa) (::: peek (::: cursor)))

  (def: split (extend id)))

(def: #export (peeks trans store)
  (All [s a] (-> (-> s s) (Store s a) a))
  (|> (::: cursor) trans (::: peek)))

(def: #export (seek cursor store)
  (All [s a] (-> s (Store s a) (Store s a)))
  (:: (::: split store) peek cursor))

(def: #export (seeks change store)
  (All [s a] (-> (-> s s) (Store s a) (Store s a)))
  (|> store (::: split) (peeks change)))

(def: #export (experiment Functor<f> change store)
  (All [f s a] (-> (F.Functor f) (-> s (f s)) (Store s a) (f a)))
  (:: Functor<f> map (::: peek) (change (::: cursor))))