Last active
August 16, 2017 10:31
-
-
Save shariffy/e73dac22f361ec13dd5db9d13901d3b4 to your computer and use it in GitHub Desktop.
Functional/Reactive WIP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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