aboutsummaryrefslogtreecommitdiff
path: root/stdlib/source/library/lux/data/store.lux
blob: 3a6d73386592b89221bb5bb6368aa04b13f2da56 (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
46
47
48
49
50
(.module:
  [library
   [lux #*
    [abstract
     [functor (#+ 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)))})

(implementation: #export functor
  (All [s] (Functor (Store s)))
  
  (def: (map f fa)
    (extend (function (_ store)
              (f (\ store peek (\ store cursor))))
            fa)))

(implementation: #export comonad
  (All [s] (CoMonad (Store s)))
  
  (def: &functor ..functor)

  (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] (-> (Functor f) (-> s (f s)) (Store s a) (f a)))
  (\ Functor<f> map (\\ peek) (change (\\ cursor))))