about summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorelioat <{ID}+{username}@users.noreply.github.com>2024-06-12 22:24:21 -0400
committerelioat <{ID}+{username}@users.noreply.github.com>2024-06-12 22:24:21 -0400
commitd81e97f8f0fd0689715383553e79f0da0b211e5c (patch)
treeb2ecf18f6b3c4324a1d50876c96389d233ad3104
parentf7588626ff536d2493ec4f71d23ab15ef3762606 (diff)
downloadtour-d81e97f8f0fd0689715383553e79f0da0b211e5c.tar.gz
*
-rw-r--r--js/life.combinators.js55
1 files changed, 43 insertions, 12 deletions
diff --git a/js/life.combinators.js b/js/life.combinators.js
index 04f7cc0..1fb77ea 100644
--- a/js/life.combinators.js
+++ b/js/life.combinators.js
@@ -11,6 +11,9 @@ chain			S_³ (a → b → c) → (b → a) → b → c
 converge		S2³ (b → c → d) → (a → b) → (a → c) → a → d
 psi				P	(b → b → c) → (a → b) → a → a → c
 fix-point4	    Y	(a → a) → a
+
+ref:
+https://www.willtaylor.blog/combinators-and-church-encoding-in-javscript/
 */
 
 const I  = x => x
@@ -19,13 +22,31 @@ const A  = f => x => f (x)
 const T  = x => f => f (x)
 const W  = f => x => f (x) (x)
 const C  = f => y => x => f (x) (y)
-const B  = f => g => x => f (g (x))
+const B  = f => g => x => f (g (x)) // FIXME: getting an error that g isn't a function when used in a pipeline
 const S  = f => g => x => f (x) (g (x))
 const S_ = f => g => x => f (g (x)) (x)
 const S2 = f => g => h => x => f (g (x)) (h (x))
 const P  = f => g => x => y => f (g (x)) (g (y))
 const Y  = f => (g => g (g)) (g => f (x => g (g) (x)))
 
+
+// validate combinators
+(function() {
+    console.assert(I(1) === 1, 'I failed');
+    console.assert(K(1)(2) === 1, 'K failed');
+    console.assert(A(I)(1) === 1, 'A failed');
+    console.assert(T(1)(I) === 1, 'T failed');
+    console.assert(W(I)(1) === 1, 'W failed');
+    console.assert(C(I)(1)(2) === 1, 'C failed');
+    console.assert(B(I)(I)(1) === 1, 'B failed');
+    console.assert(S(I)(I)(1) === 1, 'S failed');
+    console.assert(S_(I)(I)(1) === 1, 'S_ failed');
+    console.assert(S2(I)(I)(I)(1) === 1, 'S2 failed');
+    console.assert(P(I)(I)(1)(2) === 1, 'P failed');
+    console.assert(Y(I)(1) === 1, 'Y failed');
+}());
+
+
 // Count the number of live neighbors of a cell
 const countLiveNeighbors = B (A (B (A (B (A (K (A (I)))))))) (A (B (A (K (A (I))))))
 
@@ -34,22 +55,32 @@ const rules = B (A (B (A (K (A (I)))))) (A (B (A (K (A (I))))))
 const nextState = B (A (B (A (K (A (I)))))) (A (B (A (K (A (I))))))
 const nextBoardState = B (A (B (A (K (A (I)))))) (A (B (A (K (A (I))))))
 
+// validate countLiveNeighbors rules
+(function() {
+    // FIXME: I think I messed up these test values, maybe?
+    console.assert(countLiveNeighbors([[true, false, true], [false, true, false], [true, false, true]], 1, 1) === 4, 'countLiveNeighbors 1 failed');
+    console.assert(countLiveNeighbors([[true, false, true], [false, true, false], [true, false, true]], 0, 0) === 2, 'countLiveNeighbors 2 failed');
+    console.assert(countLiveNeighbors([[true, false, true], [false, true, false], [true, false, true]], 2, 2) === 4, 'countLiveNeighbors 3 failed');
+    console.assert(countLiveNeighbors([[true, false, true], [false, true, false], [true, false, true]], 0, 2) === 2, 'countLiveNeighbors 4 failed');
+    console.assert(countLiveNeighbors([[true, false, true], [false, true, false], [true, false, true]], 2, 0) === 2, 'countLiveNeighbors 5 failed');
+}());
+
 // validate isAlive rules
 (function() {
-    console.assert(isAlive(true)(2) === true, 'Test 1 failed');
-    console.assert(isAlive(true)(3) === true, 'Test 2 failed');
-    console.assert(isAlive(true)(4) === false, 'Test 3 failed');
-    console.assert(isAlive(false)(3) === true, 'Test 4 failed');
-    console.assert(isAlive(false)(2) === false, 'Test 5 failed');
+    console.assert(isAlive(true)(2) === true, 'isAlive 1 failed');
+    console.assert(isAlive(true)(3) === true, 'isAlive 2 failed');
+    console.assert(isAlive(true)(4) === false, 'isAlive 3 failed');
+    console.assert(isAlive(false)(3) === true, 'isAlive 4 failed');
+    console.assert(isAlive(false)(2) === false, 'isAlive 5 failed');
 }());
 
 // validate nextState rules
 (function() {
-    console.assert(nextState(true)(2) === true, 'Test 1 failed');
-    console.assert(nextState(true)(3) === true, 'Test 2 failed');
-    console.assert(nextState(true)(4) === false, 'Test 3 failed');
-    console.assert(nextState(false)(3) === true, 'Test 4 failed');
-    console.assert(nextState(false)(2) === false, 'Test 5 failed');
+    console.assert(nextState(true)(2) === true, 'nextState 1 failed');
+    console.assert(nextState(true)(3) === true, 'nextState 2 failed');
+    console.assert(nextState(true)(4) === false, 'nextState 3 failed');
+    console.assert(nextState(false)(3) === true, 'nextState 4 failed');
+    console.assert(nextState(false)(2) === false, 'nextState 5 failed');
 }());
 
 // validate nextBoardState rules
@@ -64,5 +95,5 @@ const nextBoardState = B (A (B (A (K (A (I)))))) (A (B (A (K (A (I))))))
         [false, true, false],
         [false, true, false]
     ];
-    console.assert(JSON.stringify(nextBoardState(board)) === JSON.stringify(nextBoard), 'Test 1 failed');
+    console.assert(JSON.stringify(nextBoardState(board)) === JSON.stringify(nextBoard), 'nextBoardState 1 failed');
 }());
\ No newline at end of file