Skip to content

Instantly share code, notes, and snippets.

@shariffy
Last active August 16, 2017 10:31
Show Gist options
  • Save shariffy/e73dac22f361ec13dd5db9d13901d3b4 to your computer and use it in GitHub Desktop.
Save shariffy/e73dac22f361ec13dd5db9d13901d3b4 to your computer and use it in GitHub Desktop.
Functional/Reactive WIP
const assert = require('assert');
const stream = (name, fn) => {
return fn;
};
module.exports.stream = stream;
const test = (stream, tests) => {
tests.forEach((t) =>{
t(stream, assert);
});
return stream;
};
module.exports.test = test;
const log = (stream) => {
return (...args) => {
console.log('IN ', args);
const result = stream(...args);
console.log('OUT', result);
return result;
}
};
module.exports.log = log;
const takePosArgs = (stream, argPos) => {
return (...args) => {
if (typeof args[0] === 'object') {
return stream(...args);
}
const newArgs = [{}];
argPos.forEach((argName, index) => {
newArgs[0][argName] = args[index];
});
return stream(...newArgs);
};
};
module.exports.takePosArgs = takePosArgs;
const { stream, test, log, takePosArgs } = require('./libstream');
let adder = stream('adder', (ev) => {
return ev.x + ev.y;
});
adder = takePosArgs(adder, ['x', 'y']);
adder = log(adder);
adder = test(adder, [
(s, assert) => assert(s({ x: 1, y: 2 }) === 3),
(s, assert) => assert(s(4, 3) === 7),
]);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment