paldepind / flyd

The minimalistic but powerful, modular, functional reactive programming library in JavaScript.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

split stream into two

StreetStrider opened this issue · comments

Hi. Does flyd has an operation which takes a stream and a predicate, just like filter but returns not one stream, but two: one for values where predicate is true and other for falsy.

var input = flyd.stream()

var pred = isEven /* any predicate */
var pair = split(pred, input) /*[0] → «truey», [1] → «falsy» */

var evens = pair[0]
var odds  = pair[1]
/* or use destructurization */

Is this a good idea or I got something conceptually wrong? I can use just two filter's but split is better, since it automatically negate predicate for second stream, so both streams does not have rubbish values from ancestor. Is this acceptable for flyd? Other FRP?

Hello

I am not sure exactly what you mean that both streams will have rubbish values from their ancestor if you use filter?

@paldepind,

var evens = filter(isEven, input)
var odds  = input // I can't just use input as «else»
// I need to negate predicate:
var odds  = filter(not(isEven), input) 
// split do it for me

What about:

function split(pred, stream) {
  return [filter(pred, stream), filter(function(v) { return !pred(v); }, stream);
};

@paldepind, yes, I've implemented it in similar manner.