diff --git a/README.md b/README.md index d4afc69..e116b5e 100644 --- a/README.md +++ b/README.md @@ -1,4 +1,6 @@ # 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. +Am implementation of Tom Stuart’s [Programming with Nothing](https://www.youtube.com/watch?v=VUhlNx_-wYk) +challenge in ES6. + +Look at [`example.js`](/example.js) if you want to see the thing in action. diff --git a/example.js b/example.js new file mode 100644 index 0000000..ff283f2 --- /dev/null +++ b/example.js @@ -0,0 +1,10 @@ +const nothing = require('./nothing.js'); +const helper = require('./nothing-helper.js'); +const fizzbuzz = (x) => { + let a = []; + for (let i = 1; i <= x; i++) { + a.push(i % 15 ? i % 5 ? i % 3 ? i : "Fizz" : "Buzz" : "FizzBuzz"); + } + return a; +}; +console.log(helper.check_represent(nothing.FIZZBUZZ(helper.representation_of(100)), fizzbuzz(100))); diff --git a/nothing-helper.js b/nothing-helper.js index 618a959..2023f96 100644 --- a/nothing-helper.js +++ b/nothing-helper.js @@ -41,7 +41,7 @@ const zip = (a, b) => a.map((e, i) => [e, b[i]]); const check_represent = function(r, v) { if (typeof v == "number") return to_int(r) == v; if (typeof v == "boolean") return to_bool(r) == v; - if (typeof v == "string") { console.log("checking ", to_array(r).map(to_int), " against ", v); return to_string(r) == v; } + if (typeof v == "string") return to_string(r) == v; if (Array.isArray(v)) { let a = to_array(r); return a.length == v.length && zip(a, v).every(([r, v]) => check_represent(r,v)); @@ -49,5 +49,5 @@ const check_represent = function(r, v) { throw Error(`Oops, I don’t know how check this: ${v}`); }; module.exports = { - to_string, to_array, representation_of, check_represent, to_int, to_char + representation_of, check_represent }; diff --git a/nothing.js b/nothing.js index e595508..a0fe06f 100644 --- a/nothing.js +++ b/nothing.js @@ -25,7 +25,7 @@ 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 = Z(f => n => m => IF(IS_LEQ(m)(n))(_ => INC(f(SUB(n)(m))(m))(_))(ZERO)); -const MOD = Z(f => n => m => IF(IS_LEQ(m)(n))(_ => f(SUB(n)(m))(m)(_))(m)); +const MOD = Z(f => n => m => IF(IS_LEQ(m)(n))(_ => f(SUB(n)(m))(m)(_))(n)); const PAIR = x => y => f => f(x)(y); const LEFT = p => p(x => y => x); const RIGHT = p => p (x => y => y); @@ -48,8 +48,8 @@ const DOUBLE_ALL = l => MAP(l)(MULT(TWO)); const TEN = INC(MULT(THREE)(THREE)); const RADIX = TEN; const FOUR = INC(THREE); -const FIVE = INC(INC(THREE)); -const FIFTEEN = MULT(THREE)(FIVE); +const FIVE = INC(FOUR); +const FIFTEEN = ADD(FIVE)(TEN); const FIZZ = MAP(UNSHIFT(UNSHIFT(UNSHIFT(UNSHIFT(EMPTY)(FOUR))(FOUR))(TWO))(ONE))(ADD(RADIX)); const BUZZ = MAP(UNSHIFT(UNSHIFT(UNSHIFT(UNSHIFT(EMPTY)(FOUR))(FOUR))(THREE))(ZERO))(ADD(RADIX)); const FIZZBUZZ = n => MAP(RANGE(ONE)(n))(m =>