kefirjs / kefir

A Reactive Programming library for JavaScript
https://kefirjs.github.io/kefir/
MIT License
1.87k stars 97 forks source link

scan without emitting seed #174

Closed iofjuupasli closed 8 years ago

iofjuupasli commented 8 years ago
Kefir.merge([
    plus$.map(() => v => v + 1),
    minus$.map(() => v => v - 1)
])
.scan((v, fn) => fn(v), 0)

For some reason I don't want to emit seed value 0.

If I omit seed value, then the first value becomes seed:

Kefir.merge([
    plus$.map(() => v => v + 1),
    minus$.map(() => v => v - 1)
])
.scan((v, fn) => fn(v))

In my case it emits function. And that seems very strange

I think the best will be if without seed it applies transformation with seed = undefined. So I can do:

Kefir.merge([
    plus$.map(() => v => v + 1),
    minus$.map(() => v => v - 1)
])
.scan((v, fn) => fn(v === undefined ? 0 : v))

Now I do this:

Kefir.merge([
    plus$.map(() => v => v + 1),
    minus$.map(() => v => v - 1)
])
.scan((v, fn) => fn(v === null ? 0 : v), null)
.filter(v => v !== null)

It works, but seems weird.

I think that this problem is not so specific for my task, so probably it can be fixed on library level

rpominov commented 8 years ago

This feature is supposed to be used when your items and collected result are of same type, i.e. obs.scan((a, a) => a). But in your case it's obs.scan((a, Function) => a), so you can't use it without seed, and should provide a seed of type a.

It should be easy to then remove that first value. Try to use .skip(1) or .changes().