add Parsimmon.alt for more efficient .or() chaining #13

Merged
merged 1 commit into from Feb 28, 2014

Projects

None yet

3 participants

@jneen
Owner
Parsimmon.alt([p1, p2, p3])
// equiv to
p1.or(p2).or(p3)
// but with less fuss
@laughinghan
Collaborator

Do we want to document this?

@jneen
Owner

yes.

@jneen
Owner

I wanted to get a sense of the feature first. Do you think it makes more sense as varargs?

@laughinghan
Collaborator

yes.

@laughinghan
Collaborator

.seq(), too

@laughinghan
Collaborator

If we do Array.prototype.slice.call(arguments), I think it won't even be slow, the bailout should happen only on initialization

@jneen
Owner

even though .seq() yields an array...?

@jneen
Owner

#16 is relevant

@laughinghan
Collaborator

well, my original conception of .seq() was special-cased so it could also do .apply(), so there were no arrays anywhere (well, there were no Arrays anywhere, there were arguments arrays), but your idea of yielding and going through .then is more consistent. We could potentially do .apply() everywhere (on all the ._()s), although, even if that's possible (and I might be making a mistake in my algebra), that might make performance even worse.

@jneen
Owner

I'm just gonna make them varargs I think. Make it a major version bump, because it'll break existing code.

@laughinghan
Collaborator

You're gonna make what varargs? You're gonna make everything varargs? Or just .alt() and .seq()?

@jneen
Owner

Just alt and seq. Do you mind if I merge this and then branch off to do the varargs thing? I promise I'll write documentation.

@laughinghan
Collaborator

Sure, whatever

@laughinghan laughinghan merged commit b498010 into master Feb 28, 2014

1 check passed

Details default The Travis CI build passed
@laughinghan laughinghan deleted the feature.alt branch Feb 28, 2014
@Risto-Stevcev

@jneen Are both Parsimmon.alt([p1, p2, p3]) and Parsimmon.alt(p1, p2, p3) valid? The docs suggest the latter is the correct syntax. I think the docs for alt should also mention that it's equivalent to or chaining.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment