Git Product home page Git Product logo

Comments (7)

bugaevc avatar bugaevc commented on July 18, 2024
sorted(strings, key=len, reverse=True)

And the name should indicate it works only for strings and that it is different from alt() in supporting backtracking. With that in mind, wouldn't it be better to just add an optional backtrack argument to alt()?

Off-topic, but I see that now @jneen's repo is marked as a fork of this one; any better way to do that than contacting GitHub support?

from parsy.

spookylukey avatar spookylukey commented on July 18, 2024

@bugaevc - it would be difficult to add it into alt, because the sorting logic only works for strings, and because we want to wrap the options in parsy.string.

The motivations is that things like:

alt(string("a"), string("b"), string("c"))
alt(string("foo"), string("bar"))

are common enough to want a shortcut like:

one_of("abc")
one_of(["foo", "bar"])

and secondly, the backtracking issue of:

alt(string("thing", string("thing2"), string("thing3"))

which doesn't work as intended, can also benefit from something that gets the backtracking right.

For the first case, I think we'd want a more efficient implementation that doesn't use alt etc. - what I've posted above covers the second case.

I'm not convinced we need to make it more verbose by adding something into the name to indicate the type, especially if we have two different types that it can accept. Parsimmon has something very similar, only for strings: https://github.com/jneen/parsimmon/blob/master/API.md#parsimmononeofstring

We could have two different functions, but then that would require two names, and I can't think of better ones. I think they are similar enough in purpose to benefit from being a single function.

Regarding changing the root repo - it seems the only way is to contact GitHub support, they just need confirmation from the relevant people.

from parsy.

bugaevc avatar bugaevc commented on July 18, 2024

Okay, so I've come the full circle from trying to write down the changes to alt() I'd replace this with, to realizing it should be better done by the caller and to thinking of writing a simple wrapper around alt() to do that.

But I'd still like a more descriptive name, how about string_from()? And the docstring should mention backtracking (ex. "this function picks the longest of the passed strings that do match").

from parsy.

jneen avatar jneen commented on July 18, 2024

How about .keywords("thing", "thing2", ...)?

from parsy.

spookylukey avatar spookylukey commented on July 18, 2024

@jneen that works well for multiple strings, not so good for one_of('0123456789').
@bugaevc similarly for string_from('0123456789') which could be confusing.
one_of seems pretty descriptive to me:

digit = one_of('0123456789')
boolean = one_of(["true", "false"])

Maybe we could have string_from and char_from, and just have two separate functions, if we want to have the data type in the name. I'm thinking of adding any_char so maybe that would complement it.

from parsy.

bugaevc avatar bugaevc commented on July 18, 2024

I'd rather have the function accept *strings so that you don't have to pack them in a list at the call site. If you want to pick a character from a string, call it like string_from(*'1234567890').

one_of would be fine if it was the only way to alternate things (and indeed, it would make sense to name the current alt one_of), but it is specific to strings/keywords and the name should indicate that.

from parsy.

spookylukey avatar spookylukey commented on July 18, 2024

This is implemented now in PR #15

I implemented char_from separately because:

  1. string_from(*"abc") is really pretty obscure for most Python users, and this is a really common case
  2. char_from implemented using the in operator to test is many times faster. (5 times faster for 10 digits, 25 times faster for 62 chars (upper, lower, digits)).

from parsy.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.