aboutsummaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-10-04 14:46:45 +0300
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-05 14:21:53 +0200
commit7a498e3a581f26b81fc9c2c9c7af8448a7f84f0f (patch)
tree10c05309191807d0bcc31a9fcca4a422c1cda414 /examples
parentfe5d1ec1b76b8cb42359324030e701a719847a50 (diff)
Rename and refactor
Diffstat (limited to 'examples')
-rw-r--r--examples/effects-concept.lisp4
-rw-r--r--examples/maybe.lisp51
-rw-r--r--examples/test.lisp39
3 files changed, 55 insertions, 39 deletions
diff --git a/examples/effects-concept.lisp b/examples/effects-concept.lisp
index 0bde72f..5ea90d7 100644
--- a/examples/effects-concept.lisp
+++ b/examples/effects-concept.lisp
@@ -1,7 +1,7 @@
; FUNCTIONS
-(let prompt-input (\[]
- (let p "> ")
+(let! prompt-input (\[]
+ (let! p "> ")
(do get-user-input "prompted" p)))
; SIGNAL HANDLERS
diff --git a/examples/maybe.lisp b/examples/maybe.lisp
index fe0b418..dab7bf8 100644
--- a/examples/maybe.lisp
+++ b/examples/maybe.lisp
@@ -1,14 +1,14 @@
; LIB
-(let just (\[a]
+(let! just (\[a]
["maybe" "just" a]))
-(let nothing (\[]
+(let! nothing (\[]
["maybe" "nothing"]))
-(let unsafe-at (\[n seq]
+(let! unsafe-at (\[n seq]
(match seq
[]
- (fatal "unsafe-at out of bounds")
+ (fatal! "unsafe-at out of bounds")
otherwise
(match n
0
@@ -16,36 +16,45 @@
otherwise
(unsafe-at (- n 1) (tail seq))))))
-(let unpack-just (unsafe-at 2))
-(let kind (unsafe-at 1))
+(let! unpack-just (unsafe-at 2))
+(let! kind (unsafe-at 1))
-(let map (\[f m]
+(let! map (\[f m]
(match (kind m)
"just" (just (f (unpack-just m)))
"nothing" (nothing))))
-(let and-then (\[f m]
+(let! and-then (\[f m]
(match (kind m)
"just" (f (unpack-just m))
"nothing" (nothing))))
; TESTING
-(let print-line! (\[s]
+(let! print-line! (\[s]
(print! (concat s "\n"))))
-(let m (just 10))
-(match (kind m)
- "just"
+(let! a (just 10))
+(let! b (nothing))
+
+(map (+ 5) a)
+(map (+ 5) b)
+
+(and-then (\[n] (just (+ n 5))) a)
+(and-then (\[n] (just (+ n 5))) b)
+
+(let! m (just 10))
+(match m
+ (just _)
(print-line! (fmt "found just {0}!" [(unpack-just m)]))
- "nothing"
+ (nothing)
(print-line! "found nothing!"))
-(let exports [
- just
- nothing
- unpack-just
- kind
- map
- and-then
-])
+;; (let! exports [
+;; just
+;; nothing
+;; unpack-just
+;; kind
+;; map
+;; and-then
+;; ])
diff --git a/examples/test.lisp b/examples/test.lisp
index a6f7d60..fd8c379 100644
--- a/examples/test.lisp
+++ b/examples/test.lisp
@@ -1,25 +1,27 @@
-(let compose (\[f g]
+(let! compose (\[f g]
(\[x] (f (g x)))))
((compose (+ 1) (+ 2)) 3)
-(let mod (\[n k]
+(let! id (\[a] a))
+
+(let! mod (\[n k]
(- n (* k (/ n k)))))
-(let not (\[b]
+(let! not (\[b]
(match b
true false
false true)))
-(let is-even (\[n]
+(let! is-even (\[n]
(match (mod n 2)
0 true
1 false)))
-(let is-odd (compose not is-even))
+(let! is-odd (compose not is-even))
;; map :: (a -> b) -> [a] -> [b]
-(let map (\[f lst]
+(let! map (\[f lst]
(match lst
[]
[]
@@ -29,7 +31,7 @@
(map (+ 1) [1 2 3])
;; foldr :: (a -> b -> b) -> b -> [a] -> b
-(let foldr (\[f accumulator lst]
+(let! foldr (\[f accumulator lst]
(match lst
[]
accumulator
@@ -39,7 +41,7 @@
(foldr + 0 [1 2 3])
;; filter :: (a -> Bool) -> [a] -> [a]
-(let filter (\[pred lst]
+(let! filter (\[pred lst]
(match lst
[] []
otherwise (match (pred (head lst))
@@ -50,9 +52,9 @@
(filter is-even [0 1 2 3 4 5])
-(let fibo (\[n]
- (let lazy fibo-1 (fibo (- n 1)))
- (let lazy fibo-2 (fibo (- n 2)))
+(let! fibo (\[n]
+ (let! lazy fibo-1 (fibo (- n 1)))
+ (let! lazy fibo-2 (fibo (- n 2)))
(match n
0 0
1 1
@@ -60,15 +62,20 @@
(fibo 10)
-(let reverse_ (\[v a]
- (let lazy x (head v))
- (let lazy xs (tail v))
- (let lazy xa (prepend x a))
+(let! reverse_ (\[v a]
+ (let! lazy x (head v))
+ (let! lazy xs (tail v))
+ (let! lazy xa (prepend x a))
(match v
[] a
_ (reverse_ xs xa))))
-(let reverse (\[v]
+(let! reverse (\[v]
(reverse_ v [])))
(reverse [1 2 3])
+
+(let! flow (\[fs] (foldr compose id (reverse fs))))
+(let! pipe (\[x fs] ((flow fs) x)))
+
+(pipe 10 [(+ 1) (+ 2)])