Skip to content

Instantly share code, notes, and snippets.

@mofas
Created April 29, 2018 00:50
Show Gist options
  • Save mofas/a46915298daddcd965359034084c45d3 to your computer and use it in GitHub Desktop.
Save mofas/a46915298daddcd965359034084c45d3 to your computer and use it in GitHub Desktop.
Try to implement simple stream
// Stream
const stream = f => (x => x(x))(y => init => [init, m => y(y)(f(m))]);
const add3Stream = stream(n => n + 3);
const mult2Stream = stream(n => n * 2);
const inspect = stream(n => {
console.log('inspect:' + n);
return 0;
});
const pipe = s1 => s2 => x => {
const ret1 = s1(x);
const ret2 = s2(ret1[0]);
return [ret2[0], pipe(ret1[1])(ret2[1])];
};
const sPlus3Mult2Stream = pipe(add3Stream)(mult2Stream);
let [value, next] = sPlus3Mult2Stream(0);
console.log(value);
[value, next] = next(3);
console.log(value);
[value, next] = next(5);
console.log(value);
const inspectEx = pipe(sPlus3Mult2Stream)(inspect);
[value, next] = inspectEx(0);
[value, next] = next(3);
[value, next] = next(5);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment