highland
highland copied to clipboard
Idea for discussion: forkBy
I often find myself in the situation where I want to do the inverse of merge, i.e. split the values of a stream into two different streams. I might be missing some obvious functionality, but this is how I do it today - is there a sleeker way?
let forkBy = (strm, evaluator) => {
let a = _(), b = _()
_(strm).each(x => evaluator(x) ? a.write(x) : b.write(x))
return [a, b]
}
Example usage
let isSpecial = str => !!str.match(/^specialcasething/)
let [ specialThings, otherThings ] = forkBy(connectionStream, isSpecial)
Is this a pattern you recognize in your coding?
const isSpecial = str => !!str.match(/^specialcasething/)
const specialThings = connectionStream.fork().filter(str => isSpecial(str))
const otherThings = connectionStream.fork().filter(str => !isSpecial(str))
Is that really the same thing? If I try to replace my code with that all my tests break.
In what way do they break? The problem with your implementation is that it isn't lazy and it cannot end...