Giter VIP home page Giter VIP logo

Comments (6)

EntilZha avatar EntilZha commented on June 14, 2024 1

The idea to have a default doesn't necessarily mean an equivalent way of doing it is bad style-wise. The reason for not doing sequence=[] is because the array is then a singleton across all calls, which leads to very weird behavior. The right way to implement this pattern, is to set sequence=None and then have a if sequence is None check, if so then set sequence = []. In this particular case, I don't think you'd need to change the Sequence code since the type error is thrown in _parse_args and the Sequence constructor usually isn't called by users, rather by the library itself.

Back to the question of whether you should be able to call seq() with arguments, I can get behind the argument of mirroring set() and list() in returning an empty sequence. The example stephan gave also seems like a reasonable use case for this.

Given that, I'd welcome a PR that modifies the len(args) == 0 code section to instead initialize to an empty sequence. I think that is probably the only change needed, but its possible I'm missing something

from pyfunctional.

EntilZha avatar EntilZha commented on June 14, 2024

Is there a use case for this?

from pyfunctional.

Kache avatar Kache commented on June 14, 2024

Primarily for consistency, and it seems to let users avoid special-casing. Same as usage as tuple() and set() for empty containers. (list() and dict() too, although [] and {} are generally preferred.) I happened to want an empty sequence and was surprised.

It seems strange to me to intentionally define a special case that will raise TypeError. Conceptually, I would expect the following to work:

pattern_of_lists = [
    [0, 1, 2],
    [0, 1],
    [0],
    [],
]
for arr in pattern_of_lists:
    for_conversion = seq(arr)
    for_expressing_literal = seq(*arr)  # why specifically TypeError for []?
    for_conversion == for_expressing_literal

Also (I'm sure you know):

seq([], [], []) == [[], [], []]
seq([], []) == [[], []]
seq([])  # surprise! But sure, this is special-cased for seq(*literal_elements) convenience
seq()  # also surprise! (raises)

Was there a particular edge case or other ambiguity that warrants the TypeError?

Could (should?) make empty seq a "singleton" if you wanted, since it's immutable.

Not common, but I've also used "empty container" as the identity object in a reduce/fold before.

from pyfunctional.

stephan-rayner avatar stephan-rayner commented on June 14, 2024

Hello friends, I thought about this for a bit today and I had an idea.

I think the easiest way might be to change the constructor in Sequence by giving a default value to the sequence parameter like so.

class Sequence(object):

    def __init__(self, sequence=[], transform=None, engine=None, max_repr_items=None):
        ...

That said, I imagine pylint would be bothered by this.

I do have an alternative that I have used in production myself. For the code your example you provided it would look something like this

seq(pattern_of_lists) \
    .map(lambda arr: (seq(arr), seq(*arr) if arr else seq([])))

This results in a sequence of tuples (for_conversion, for_conversion_literal to use the original example's variables) and from there you could compare them in a map or run .for_all(lambda pair: pair[0] == pair[1]) depending on your use case.

The output from the above example ends up like this:

[([0, 1, 2], [0, 1, 2]), ([0, 1], [0, 1]), ([0], [0]), ([], [])]

I liked this when I used it because it made the substitution explicit. From the library maintainer perspective it doesn't require a change which may or may not (not the maintainer of this lib so I will defer there) induce a bug down the road.

I know that isn't what was asked for but I hope that helps someone 🙂.

from pyfunctional.

Kache avatar Kache commented on June 14, 2024

Cool! I've created #161

I've followed CONTRIBUTING.md except for the TravisCI part, as I'm a little unclear about what to do there.

from pyfunctional.

Kache avatar Kache commented on June 14, 2024

merged!

from pyfunctional.

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.