baconjs / bacon.matchers

Matchers API for Bacon.js
MIT License
21 stars 6 forks source link

Multiple clauses #11

Closed mkaemmerer closed 10 years ago

mkaemmerer commented 10 years ago

Adds matchers "some" and "every" for matching multiple conditions at once.

jliuhtonen commented 10 years ago

Good stuff. Would it be possible to build the matcher context in one place instead of modifying it afterwards in asMatchers? This would also make .not() work as expected as it does with other .is() and .where() matchers. I think this kind of consistency in the API would be good.

mkaemmerer commented 10 years ago

Great points! Will refactor when I have time.

mkaemmerer commented 10 years ago

I lifted the definitions of some/every from asMatchers to addMatchers. I couldn't figure out a decent way to move it into addPositiveMatchers yet, so there's still some duplication.

jliuhtonen commented 10 years ago

IMO it would serve the purpose to just negate the value of the end result stream so that

stream.is().every(fs).not()

would produce the same values as

stream.is().not().every(fs)

Does this help with reducing the duplication?

mkaemmerer commented 10 years ago

Not quite. It's fine for is but doesn't work for where.

This:

stream.where().not().every(fs)

is not the same as this:

stream.where().every(fs).not()
jliuhtonen commented 10 years ago

Ah, of course, totally missed that.

So you can't just negate the operation's result, although it should be possible to negate the result of isEvery and isSome streams and then use those.

Because of DeMorgan's laws you don't need to do this in not

matches = Bacon._.map(((f) -> f(stream).not()), fs)
isEvery = Bacon._.fold(matches, Bacon.constant(false), (x,y) -> x.or(y))

because this is the same, right?

matches = Bacon._.map(((f) -> f(stream)), fs)
isEvery = Bacon._.fold(matches, Bacon.constant(false), (x,y) -> x.and(y)).not()

So you could partially re-use the positive every/some by extracting functions that return the boolean property.

Or are my tired eyes still missing something? :)

mkaemmerer commented 10 years ago

I'm pretty sure that works correctly. My tests seem to think so too :)

mkaemmerer commented 10 years ago

Ooops. Fixed now.

jliuhtonen commented 10 years ago

Released version 0.4.0.