Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Design pattern matching syntax + roadmap #160

Open
kantord opened this issue Nov 23, 2018 · 3 comments
Open

Design pattern matching syntax + roadmap #160

kantord opened this issue Nov 23, 2018 · 3 comments
Labels

Comments

@kantord
Copy link
Owner

kantord commented Nov 23, 2018

The basic idea is to have a syntax similar to this:

(
  ["foo", $bar] -> $bar.value * 2,
  [$first, {"baz": $value}] -> [$value, $first],
  $value, $value >= 3.14 -> "Bigger than 3.14",
  otherwise -> error "Nonsense"
)
@kantord
Copy link
Owner Author

kantord commented Dec 26, 2018

Def syntax to define functions with pattern matching:

def factorial: (
    0 -> 1,
    $ -> $ * (($ - 1) factorial)
)

@kantord
Copy link
Owner Author

kantord commented Dec 26, 2018

Perhaps ; should be considered instead of ,

@kantord
Copy link
Owner Author

kantord commented Mar 19, 2019

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 #347

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant