diff options
Diffstat (limited to '')
-rw-r--r-- | stdlib/source/library/lux/data/store.lux | 50 |
1 files changed, 50 insertions, 0 deletions
diff --git a/stdlib/source/library/lux/data/store.lux b/stdlib/source/library/lux/data/store.lux new file mode 100644 index 000000000..3a6d73386 --- /dev/null +++ b/stdlib/source/library/lux/data/store.lux @@ -0,0 +1,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)))) |