summary refs log tree commit diff
path: root/lib
diff options
context:
space:
mode:
authordozens2024-05-28 15:04:00 -0600
committerdozens2024-05-28 15:04:00 -0600
commitf265d24c0cacb92c7f7db19f364a155d87938184 (patch)
treea6e3ce61dee9020b79c8f2d8e3c91b51bc29e959 /lib
inits
Diffstat (limited to 'lib')
-rw-r--r--lib/contains.fnl7
-rw-r--r--lib/contains.test.fnl11
-rw-r--r--lib/either.fnl20
-rw-r--r--lib/either.test.fnl40
-rw-r--r--lib/head.fnl6
-rw-r--r--lib/head.test.fnl11
-rw-r--r--lib/index.fnl13
-rw-r--r--lib/mill.fnl16
-rw-r--r--lib/mill.test.fnl1
-rw-r--r--lib/tableprint.fnl7
-rw-r--r--lib/tail.fnl7
-rw-r--r--lib/tail.test.fnl1
12 files changed, 140 insertions, 0 deletions
diff --git a/lib/contains.fnl b/lib/contains.fnl
new file mode 100644
index 0000000..75275af
--- /dev/null
+++ b/lib/contains.fnl
@@ -0,0 +1,7 @@
+(fn contains [t x]
+  (accumulate [found false
+               _ v (ipairs t)
+               &until found] ; escape early
+    (or found (= x v))))
+
+{: contains}
diff --git a/lib/contains.test.fnl b/lib/contains.test.fnl
new file mode 100644
index 0000000..1c7fb06
--- /dev/null
+++ b/lib/contains.test.fnl
@@ -0,0 +1,11 @@
+(let [{: contains } (require :lib.contains)]
+  (let [given "a list and an element it contains"
+        should "returns true"
+        expected true
+        actual (contains [:apple :orange :pear] :apple)]
+    (assert (= actual expected) (.. "Given " given " should " should)))
+  (let [given "a list and an element it does not contain"
+        should "returns false"
+        expected false
+        actual (contains [:apple :orange :pear] :gorilla)]
+    (assert (= actual expected) (.. "Given " given " should " should))))
diff --git a/lib/either.fnl b/lib/either.fnl
new file mode 100644
index 0000000..24aafca
--- /dev/null
+++ b/lib/either.fnl
@@ -0,0 +1,20 @@
+(local Either {})
+(local Left {})
+(local Right {})
+(setmetatable Right Either)
+(setmetatable Left Either)
+
+(fn Either.new [self x]
+  (local obj { :value (or x {}) })
+  (tset self "__index" self)
+  (setmetatable obj self))
+(fn Either.of [x] (Right:new x))
+
+(fn Right.map [self f] (Either.of (f self.value)))
+(fn Left.map [self f] self)
+
+{
+ : Either
+ : Left
+ : Right
+}
diff --git a/lib/either.test.fnl b/lib/either.test.fnl
new file mode 100644
index 0000000..fc819dc
--- /dev/null
+++ b/lib/either.test.fnl
@@ -0,0 +1,40 @@
+(local {: pprint} (require :lib.tableprint))
+
+(let [{
+       : Either
+       : Left
+       : Right
+       } (require :lib.either)]
+
+  ;; either
+  ;(print "Either Inspection")
+  ;(pprint Either)
+
+  ;; you can set and get values
+  (let [ v :poop x (Either:new v)]
+    (assert (= v x.value) (.. "The value is " v)))
+
+  (let [r (Right:new "rain")
+        map (r:map #(.. "b" $1))
+        expected :brain
+        actual (. map :value)
+        ]
+    (assert (= expected actual) "You can map a Right value"))
+
+  (let [l (Left:new "rain")
+        map (l:map #(.. "b" $1))
+        expected :rain
+        actual (. map :value)
+        ]
+    (assert (= expected actual) "You can NOT map a Left value"))
+
+  (let [e (Either.of "rank")
+        map (e:map #(.. "f" $1))
+        expected :frank
+        actual (. map :value)
+        ]
+    (assert (= expected actual) "You can map a Either.of"))
+
+
+
+)
diff --git a/lib/head.fnl b/lib/head.fnl
new file mode 100644
index 0000000..ddee698
--- /dev/null
+++ b/lib/head.fnl
@@ -0,0 +1,6 @@
+; return the first item in a table
+(fn head [t] (if (> (length t) 0)
+               (?. t 1)
+               []))
+
+{: head}
diff --git a/lib/head.test.fnl b/lib/head.test.fnl
new file mode 100644
index 0000000..7514121
--- /dev/null
+++ b/lib/head.test.fnl
@@ -0,0 +1,11 @@
+(let [{: head } (require :lib.head)]
+  (let [given "a lift of elements"
+        it "returns the first element of a list"
+        expected :apple
+        actual (head [:apple :orange :pear])]
+    (assert (= actual expected) (.. "Given " given " it " it)))
+  (let [given "an empty list"
+        it "returns an empty list"
+        expected 0
+        actual (length (head []))]
+    (assert (= actual expected) (.. "Given " given " it " it))))
diff --git a/lib/index.fnl b/lib/index.fnl
new file mode 100644
index 0000000..4d15b9a
--- /dev/null
+++ b/lib/index.fnl
@@ -0,0 +1,13 @@
+(local {:contains contains} (require :lib.contains))
+(local {:head head} (require :lib.head))
+(local {:mill? mill?} (require :lib.mill))
+(local {:pprint pprint} (require :lib.tableprint))
+(local {:tail tail} (require :lib.tail))
+
+{
+ :contains contains
+ :head head
+ :mill? mill?
+ :pprint pprint
+ :tail tail
+ }
diff --git a/lib/mill.fnl b/lib/mill.fnl
new file mode 100644
index 0000000..de54128
--- /dev/null
+++ b/lib/mill.fnl
@@ -0,0 +1,16 @@
+(local {: contains} (require :lib.contains))
+
+;; Does this move result in a mill?
+(fn mill? [rules state move]
+  (let [candidates (icollect [_ mill (ipairs rules)] (if (contains mill move) mill))
+        candidate->moves (icollect [_ spaces (ipairs candidates)]
+                                  (icollect [_ space (ipairs spaces)] (. state space)) )
+        candidate-mill? (icollect [_ moves (ipairs candidate->moves)]
+                                  (accumulate [acc true
+                                              idx m (ipairs moves)]
+                                              (and acc (not= 0 m) (= (. moves idx) m)))) ]
+    (accumulate [acc true
+                 _ x (ipairs candidate-mill?)]
+                (and acc x))))
+
+{: mill?}
diff --git a/lib/mill.test.fnl b/lib/mill.test.fnl
new file mode 100644
index 0000000..358b218
--- /dev/null
+++ b/lib/mill.test.fnl
@@ -0,0 +1 @@
+;; TODO: test me
diff --git a/lib/tableprint.fnl b/lib/tableprint.fnl
new file mode 100644
index 0000000..4d9bfbe
--- /dev/null
+++ b/lib/tableprint.fnl
@@ -0,0 +1,7 @@
+; print a table
+(fn pprint [tbl]
+  (each [k v (pairs tbl)]
+    (let [table? (= (type v) :table)]
+      (print k v))))
+
+{: pprint}
diff --git a/lib/tail.fnl b/lib/tail.fnl
new file mode 100644
index 0000000..24de254
--- /dev/null
+++ b/lib/tail.fnl
@@ -0,0 +1,7 @@
+; return the table minus the head
+(fn tail [t]
+  (icollect [i v (ipairs t)]
+            (if (> i 1)
+              v)))
+
+{: tail}
diff --git a/lib/tail.test.fnl b/lib/tail.test.fnl
new file mode 100644
index 0000000..358b218
--- /dev/null
+++ b/lib/tail.test.fnl
@@ -0,0 +1 @@
+;; TODO: test me