[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: [LPeg] Switch matcher
- From: Dirk Laurie <dirk.laurie@...>
- Date: Sun, 16 Aug 2015 21:19:53 +0200
2015-08-16 17:16 GMT+02:00 Soni L. <fakedme@gmail.com>:
> It should be constructed as `pattern | {cases}`.
>
> Here's how it works:
>
> You match the pattern `pattern`. You use the result of that match to index
> into `{cases}`. `{cases}` is a key-value map from strings (match results) to
> patterns. If the key doesn't exist, the match fails, otherwise we attempt to
> match the indexed pattern (match the value we got from `{cases}`).
>
> A default case can be done with `pattern - (pattern | {cases})`.
>
> This is faster than using alternations: O(1) vs O(n).
We already have `pattern/func`, which replaces the match by
the result of `func`. This is a good deal more general: the original
string, the position and all the captures get passed to `func`.