This commit is contained in:
2018-06-04 21:02:05 +02:00
parent 33e378fcd1
commit fd0ea53218
4 changed files with 19 additions and 7 deletions

View File

@@ -1,4 +1,6 @@
# nothing # nothing
A WIP implementation of Tom Stuarts [Programming with Nothing](https://www.youtube.com/watch?v=VUhlNx_-wYk) Am implementation of Tom Stuarts [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. challenge in ES6.
Look at [`example.js`](/example.js) if you want to see the thing in action.

10
example.js Normal file
View File

@@ -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)));

View File

@@ -41,7 +41,7 @@ const zip = (a, b) => a.map((e, i) => [e, b[i]]);
const check_represent = function(r, v) { const check_represent = function(r, v) {
if (typeof v == "number") return to_int(r) == v; if (typeof v == "number") return to_int(r) == v;
if (typeof v == "boolean") return to_bool(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)) { if (Array.isArray(v)) {
let a = to_array(r); let a = to_array(r);
return a.length == v.length && zip(a, v).every(([r, v]) => check_represent(r,v)); 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 dont know how check this: ${v}`); throw Error(`Oops, I dont know how check this: ${v}`);
}; };
module.exports = { module.exports = {
to_string, to_array, representation_of, check_represent, to_int, to_char representation_of, check_represent
}; };

View File

@@ -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 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 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 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 PAIR = x => y => f => f(x)(y);
const LEFT = p => p(x => y => x); const LEFT = p => p(x => y => x);
const RIGHT = p => p (x => y => y); 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 TEN = INC(MULT(THREE)(THREE));
const RADIX = TEN; const RADIX = TEN;
const FOUR = INC(THREE); const FOUR = INC(THREE);
const FIVE = INC(INC(THREE)); const FIVE = INC(FOUR);
const FIFTEEN = MULT(THREE)(FIVE); const FIFTEEN = ADD(FIVE)(TEN);
const FIZZ = MAP(UNSHIFT(UNSHIFT(UNSHIFT(UNSHIFT(EMPTY)(FOUR))(FOUR))(TWO))(ONE))(ADD(RADIX)); 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 BUZZ = MAP(UNSHIFT(UNSHIFT(UNSHIFT(UNSHIFT(EMPTY)(FOUR))(FOUR))(THREE))(ZERO))(ADD(RADIX));
const FIZZBUZZ = n => MAP(RANGE(ONE)(n))(m => const FIZZBUZZ = n => MAP(RANGE(ONE)(n))(m =>