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

Use duck typing instead of abc checking in pattern matching #259

Open
evhub opened this issue May 21, 2017 · 3 comments
Open

Use duck typing instead of abc checking in pattern matching #259

evhub opened this issue May 21, 2017 · 3 comments

Comments

@evhub
Copy link
Owner

evhub commented May 21, 2017

Should be significantly faster.

@evhub evhub added this to the v1.3.0 milestone May 21, 2017
@evhub evhub modified the milestone: v1.3.0 Aug 4, 2017
@evhub evhub added this to the v1.3.2 milestone Feb 28, 2018
@evhub evhub modified the milestones: v1.3.2, v1.3.3 Jul 30, 2018
@munael
Copy link

munael commented Dec 26, 2018

You still have to check. What do you mean by using duck typing here? Try to match, assuming everything is in order, and skip if an exception is raised?

That may make sense in irrefutable patterns.

@evhub evhub removed the bounty: $50 label Dec 26, 2018
@evhub evhub removed this from the v1.4.1 milestone Dec 26, 2018
@evhub
Copy link
Owner Author

evhub commented Dec 26, 2018

@narfanar Instead of

if isinstance(thing, Sequence) and thing[0] == 1:
    ...

do

no_ind = object()
def try_index(obj, ind):
    try:
        return obj[ind]
    except:
        return no_ind
check_ind = try_index(thing, 0)
if check_ind is not no_ind and check_ind == 1:
    ...

though honestly I'm not sure if that's actually a good idea.

@munael
Copy link

munael commented Dec 27, 2018

With absolutely no data to support my claim, I'll guess this would be slower than the isinstance check.

Though as mentioned, it might make sense in places where patterns should not fail but can (that is, if they fail, the function is bound to return (with a fallback) or throw).

I'm not sure this construct even exists in Coconut though. Think something like guard let in Swift but you always throw in the handler.

It is.

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

No branches or pull requests

2 participants