From 2845a5e308047be70ef73a1566d7f9529295e15f Mon Sep 17 00:00:00 2001 From: hellerve Date: Wed, 30 May 2018 10:23:25 +0200 Subject: [PATCH] initial WIP --- README.md | 4 ++++ nothing-helper.js | 4 ++++ nothing.js | 54 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 62 insertions(+) create mode 100644 README.md create mode 100644 nothing-helper.js create mode 100644 nothing.js diff --git a/README.md b/README.md new file mode 100644 index 0000000..d4afc69 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +# nothing + +A WIP implementation of Tom Stuart’s [Programming with Nothing](https://www.youtube.com/watch?v=VUhlNx_-wYk) +challenge in ES6. I got to factorial, and will try to do the whole thing soon. diff --git a/nothing-helper.js b/nothing-helper.js new file mode 100644 index 0000000..d54700c --- /dev/null +++ b/nothing-helper.js @@ -0,0 +1,4 @@ +module.exports = { + to_int: (f) => f(n => n+1)(0), + to_boolean: (f) => f(true)(false), +}; diff --git a/nothing.js b/nothing.js new file mode 100644 index 0000000..da1b579 --- /dev/null +++ b/nothing.js @@ -0,0 +1,54 @@ +const ZERO = f => x => x; +const ONE = f => x => f(x); +const TWO = f => x => f(f(x)); +const THREE = f => x => f(f(f(x))); +const TIMES = n => f => x => n(f)(x); +const INC = n => f => x => f(n(f)(x)); +const ADD = n => m => n(INC)(m); +const MULT = n => m => n(ADD(m))(ZERO); +const POWER = n => m => n(MULT(m))(ONE); +// DEC is stolen from PRED here: +// https://en.wikipedia.org/wiki/Lambda_calculus#Arithmetic_in_lambda_calculus +const DEC = n => f => x => n(g => h => h(g(f)))(y => x)(y => y); +const SUB = n => m => m(DEC)(n); +const TRUE = x => y => x; +const FALSE = x => y => y; +const IF = b => b; +const NOT = b => IF(b)(FALSE)(TRUE); +const AND = a => b => IF(a)(IF(b)(TRUE)(FALSE))(FALSE); +const OR = a => b => IF(a)(TRUE)(IF(b)(TRUE)(FALSE)); +const IS_ZERO = n => n(x => FALSE)(TRUE); +const IS_LEQ = n => m => IS_ZERO(SUBTRACT(n)(m)); // we only have positive ints +const IS_EQ = n => m => AND(IS_LEQ(n)(m))(IS_LEQ(m)(n)); // this is stupid +// time to get recursive! +const Y = f => (x => f(x(x)))(x => f(x(x))); // lazy recursion +const Z = f => (x => f(_ => x(x)(_)))(x => f(_ => x(x)(_))); // eager recursion +const FACT = Z(f => n => IF(IS_ZERO(n))(ONE)(_ => MULT(n)(f(DEC(n)))(_))); +const DIV = null; +const MOD = null; +const PAIR = null; +const LEFT = null; +const RIGHT = null; +const EMPTY = null; +const UNSHIFT = null; +const IS_EMPTY = null; +const FIRST = null; +const REST = null; +const INJECT = null; +const FOLD = null; +const MAP = null; +const RANGE = null; +const SUM = null; +const PROD = null; +const CONCAT = null; +const PUSH = null; +const REVERSE = null; +const INC_ALL = null; +const DOUBLE_ALL = null; +const TO_DIGITS = null; +const TO_STRING = null; +const FIZZBUZZ = null; +module.exports = { + ZERO, ONE, TWO, THREE, TIMES, INC, ADD, MULT, POWER, DEC, SUB, TRUE, + FALSE, IF, NOT, AND, OR, IS_ZERO, IS_LEQ, IS_EQ, IS_LEQ, Y, Z, FACT +};