Last active
August 29, 2015 14:22
-
-
Save rpominov/0b9b99f3652cc2cc8062 to your computer and use it in GitHub Desktop.
lightest streams
This file contains hidden or 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
function createStream(executor) { | |
return { | |
observe(sink) { | |
return executor(sink) | |
} | |
} | |
} | |
function map(fn, stream) { | |
return createStream(sink => { | |
retrun stream.observe(x => sink(fn(x))) | |
}) | |
} | |
function filter(fn, stream) { | |
return createStream(sink => { | |
retrun stream.observe(x => { | |
if (fn(x)) { | |
sink(x) | |
} | |
}) | |
}) | |
} | |
function flatMap(fn, stream) { | |
const unobserveCallbacks = [] | |
retrun createStream(sink => { | |
let unobserve = stream.observe(x => { | |
let unobserve = fn(x).observe(sink) | |
unobserveCallbacks.push(unobserve) | |
}) | |
unobserveCallbacks.push(unobserve) | |
retrun () => { | |
unobserveCallbacks.forEach(cb => cb()) | |
} | |
}) | |
} | |
// need `of(x)`, for which need completion |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This part
suggests, that
observe
is simply=
toexecutor
, so a stream basically is executor. Is this the "function class" pattern that was mentioned here and here?