JSFiddle - React, Tailwind, and code Playground

by dimitrs_papadimitriou

JavaScript

//https://gist.github.com/dypsilon/6b242998ba3474fc239255d42b28dd02

// var r = (x, s) => (x == 0 ? left(x) : r(x - 1, s.bind(i => right(x + i))));
//https://stackoverflow.com/questions/43592016/how-do-i-replace-while-loops-with-a-functional-programming-alternative-without-t/43596323#43596323

// r(100000000, right(0)).cata({ right: console.log, left: console.log });
//https://github.com/sanctuary-js/sanctuary-either/blob/6d25a285cbe270cface80083fac9b36a74da750b/index.js#L40

const right = v => ({
  map: f => right(f(v)),
  bind: f => f(v),
  cata: alg => alg.right(v)
});

const left = v => ({
  map: () => left(v),
  bind: () => left(v),
  cata: alg => alg.left(v)
});

var IO = fn => ({
  map: f => IO(() => f(fn())),
  chain: f => IO(() => f(fn()).run()),
  run: () => fn()
});

class Continuation {
  constructor(x) {
    this.x = x;
  }
}

Continuation.prototype.of = Continuation.of = x => {
  return new Continuation(resolve => resolve(x));
};

Continuation.prototype.chain = function(f) {
  const x = this.x;
  return new Continuation(resolve => {
    x(res => f(res).x(res2 => resolve(res2)));
  });
};

Continuation.prototype.run = function(f) {
  return this.x(f);
};

Continuation.prototype.inspect = function() {
  return `Continuation(${this.x})`;
};

// derivations
Continuation.prototype.map = function(f) {
  var m = this;
  return m.chain(a => m.of(f(a)));
};

Continuation.prototype.ap = function(m) {
  return this.chain(f => m.map(f));
};

const repeatCont = (n, f, done) => x =>
  n === 0
    ? Continuation.of(x).chain(done)
    : Continuation.of(f(x)).chain(repeatCont(n - 1, f, done));

var t = repeatCont(10, x => x + 1, Continuation.of)(0);
t.run(x => console.log("done", x));

const chainRecIO = actions=>n=>{
 
  function next(x) { return {tag: next, value: x}; }
  function done(x) { return {tag: done, value: x}; }


}

var t  = chainRecIO((next,done,x)=>x===0?done(x):next(x-1))(5);

const repeat = n => f => x => done =>
  n === 0
    ? IO(() =>...