aboutsummaryrefslogtreecommitdiffstats
path: root/core
diff options
context:
space:
mode:
authorJan Tuomi <jans.tuomi@gmail.com>2022-12-06 17:50:19 +0200
committerJan Tuomi <jans.tuomi@gmail.com>2022-12-06 17:53:35 +0200
commit765d25857b0472e3f9df89ac822ad36ea0502466 (patch)
tree6c5c462ade5213c3f72eeebe6315603a07e130fa /core
parente529e0e48c999fccb216bd3f3bc948abe22dbe76 (diff)
Rename stdlib -> core, allow import without .milch suffix
Diffstat (limited to 'core')
-rw-r--r--core/common.milch125
-rw-r--r--core/maybe.milch44
2 files changed, 169 insertions, 0 deletions
diff --git a/core/common.milch b/core/common.milch
new file mode 100644
index 0000000..f05c6a5
--- /dev/null
+++ b/core/common.milch
@@ -0,0 +1,125 @@
+(let compose (\[f g]
+ (\[x] (f (g x)))))
+
+(let id (\[a] a))
+
+(let mod (\[n k]
+ (- n (* k (/ n k)))))
+
+(let not (\[b]
+ (match b
+ true false
+ false true)))
+
+(let is-even (\[n]
+ (match (mod n 2)
+ 0 true
+ 1 false)))
+
+(let is-odd (compose not is-even))
+
+;; map :: (a -> b) -> [a] -> [b]
+(let map (\[f lst]
+ (match lst
+ []
+ []
+ otherwise
+ (prepend (f (head lst)) (map f (tail lst))))))
+
+; (map (+ 1) [1 2 3])
+
+;; foldr :: (a -> b -> b) -> b -> [a] -> b
+(let foldr (\[f accumulator lst]
+ (match lst
+ []
+ accumulator
+ otherwise
+ (f (head lst) (foldr f accumulator (tail lst))))))
+
+;; filter :: (a -> Bool) -> [a] -> [a]
+(let filter (\[pred lst]
+ (match lst
+ [] []
+ otherwise (match (pred (head lst))
+ true
+ (prepend (head lst) (filter pred (tail lst)))
+ false
+ (filter pred (tail lst))))))
+
+(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]
+ (reverse_ v [])))
+
+(let flow (\[fs] (foldr compose id (reverse fs))))
+(let pipe (\[x fs] ((flow fs) x)))
+
+(let leq? (\[a b]
+ (or?
+ (eq? a b)
+ (lt? a b))))
+
+(let rt? (compose not leq?))
+(let req? (compose not lt?))
+
+(let max2 (\[a b]
+ (match (lt? a b)
+ true b
+ false a)))
+
+(let max (\[vals]
+ (let lazy v (head vals))
+ (let vs (tail vals))
+
+ (match vs
+ [] v
+ otherwise (max2 v (max vs)))))
+
+(let split-by' (\[delim acc vals]
+ (let lazy v (head vals))
+ (let vs (tail vals))
+
+ (match vs
+ [] (match v
+ delim [(reverse acc)]
+ otherwise [(prepend v (reverse acc))])
+ otherwise (match v
+ delim (prepend (reverse acc) (split-by' delim [] vs))
+ otherwise (split-by' delim (prepend v acc) vs)))))
+
+; split vector by delimiter
+(let split-by (\[delim vals]
+ (split-by' delim [] vals)))
+
+(let at (\[n seq]
+ (match seq
+ [] (fatal! (fmt "at out of bounds, n: {0}" [n]))
+ otherwise (match n
+ 0 (head seq)
+ otherwise (at (- n 1) (tail seq))))))
+
+(let print-fmt! (\![fstr args]
+ (print! (fmt fstr args))))
+
+(let sort-by (\[keyf vals]
+ (let sorted (sort-by-first
+ (map (\[v]
+ [(keyf v) v]) vals)))
+
+ (map (at 1) sorted)))
+
+(let take (\[n xs]
+ (match n
+ 0 []
+ otherwise (prepend (head xs) (take (- n 1) (tail xs))))))
+
+(let drop (\[n xs]
+ (match n
+ 0 xs
+ otherwise (drop (- n 1) (tail xs)))))
diff --git a/core/maybe.milch b/core/maybe.milch
new file mode 100644
index 0000000..ef9e906
--- /dev/null
+++ b/core/maybe.milch
@@ -0,0 +1,44 @@
+; LIB
+
+(record Maybe/Just value)
+(record Maybe/Nothing)
+
+(let Maybe/just Maybe/Just/create)
+(let Maybe/nothing Maybe/Nothing/create)
+
+(let Maybe/map (\[f m]
+ (match (kind m)
+ "Maybe/Just" (Maybe/just (f (Maybe/Just/get-value m)))
+ "Maybe/Nothing" (Maybe/nothing))))
+
+(let Maybe/and-then (\[f m]
+ (match (kind m)
+ "Maybe/Just" (f (Maybe/Just/get-value m))
+ "Maybe/Nothing" (Maybe/nothing))))
+
+; TESTING
+
+;; (let print-line! (\[s]
+;; (print! (concat s "\n"))))
+
+;; (let a (Maybe/just 10))
+;; (let b (Maybe/nothing))
+
+;; (Maybe/map (+ 5) a)
+;; (Maybe/map (+ 5) b)
+
+;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) a)
+;; (Maybe/and-then (\[n] (Maybe/just (+ n 5))) b)
+
+;; (let m (Maybe/just 10))
+;; (match m
+;; (Maybe/just _)
+;; (print-line! (fmt "found just {0}!" [(Maybe/Just/get-value m)]))
+;; (Maybe/nothing)
+;; (print-line! "found nothing!"))
+
+;; (let m (Maybe/just 10))
+;; (pipe m [
+;; (Maybe/map (+ 5))
+;; (Maybe/and-then (\[val] (Maybe/just (- val 3))))
+;; ])