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

Better looking nested patterns? #24

Open
senpos opened this issue Feb 19, 2019 · 0 comments
Open

Better looking nested patterns? #24

senpos opened this issue Feb 19, 2019 · 0 comments

Comments

@senpos
Copy link

senpos commented Feb 19, 2019

Hi,

Is there any better way achieving this?

from pampy import match, _

test_items = [5, '5', [5], 'not a digit']

for x in test_items:
    result = match(x,
        str, lambda x: match(x,
                           str.isdigit, lambda x: int(x),
                           _, 0
                        ),
        int, x,
        _, 0
    )
    print(f'{x} ({type(x).__name__}) -> {result}')

It is hard to describe what we want to achieve in words, but it's easy to see by example:

5 -> 5
'5' -> 5
'not a digit' -> 0
[1] -> 0

It looks over-complicated.

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

No branches or pull requests

1 participant