Open kantord opened 5 years ago
Def syntax to define functions with pattern matching:
def factorial: (
0 -> 1,
$ -> $ * (($ - 1) factorial)
)
Perhaps ;
should be considered instead of ,
A possible idea might be to use "matcher functions" for pattern matching. These would be simply functions that return either null, or any other value. When they return null, they are considered not to be a match. When they don't return null, they are considered to match their value.
A basic matcher function called match
should be implemented. This function should take a tuple, for example $.age : $.age >= 18
. If the condition in the second element is true, match $.age : $.age >= 18
should return $.age
, otherwise it should return null.
An even better implementation for this could be a custom datatype only used for pattern matching. This datatype could encode a value + whether it's a matching value or not, and could be created using matchSuccess
or matchFailure
.
This combined with creating new matchers by wrapping existing matchers in data structures, etc should be quite powerful.
It would be also nice to reuse operators +
and |
in some way to create combined matchers. That should enable using P.alt, P.seq etc.. from parsimmon to match patterns in strings. related to https://github.com/kantord/emuto/issues/347
The basic idea is to have a syntax similar to this: