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

Starmap #83

Open
ghost opened this issue Aug 31, 2022 · 7 comments
Open

Starmap #83

ghost opened this issue Aug 31, 2022 · 7 comments

Comments

@ghost
Copy link

ghost commented Aug 31, 2022

Hi,
I was thinking of submitting a PR with a starmap function, something like this:

@Pipe
def starmap(iterable, selector):
    def starfunc(args):
        return selector(*args)
    return builtins.map(starfunc, iterable)

This is really useful in situations where you have an iterable of args that you want to pass to a function and have them unpacked positionally. An example might be in parsing a row of values into a datamodel E.g:

PersonAges = namedtuple("PersonAges", ("name", "age"))

rows = [["john", 32], ["paul", 31], ["ringo", 33], ["george", 34]]

people = list(
    rows
    | starmap(PersonAges)
)

This is what you get:

[PersonAges(name='john', age=32),
 PersonAges(name='paul', age=31),
 PersonAges(name='ringo', age=33),
 PersonAges(name='george', age=34)]

Makes it much simpler than doing something like this (especially when you have a large number of values to map):

map(lambda row: PersonAges(name=row[0], age=row[1]))

I couldn't find anything resembling a starmap but if there is another way to achieve this let me know.

@JulienPalard
Copy link
Owner

What about:

@Pipe
def starmap(iterable, selector):
    for element in iterable:
        yield selector(*element)

?

@ghost
Copy link
Author

ghost commented Sep 1, 2022

What about:

@Pipe
def starmap(iterable, selector):
    for element in iterable:
        yield selector(*element)

?

Yeah this is better. I'm happy to work on a PR if you think its worth adding?

@JulienPalard
Copy link
Owner

I'm not in favor of adding pipes to the module: it would lead to an infinite number of pipes each usefull to only a handfull of people.

I prefer to see the module and the readme as a way to teach people how to do their own specific pipes tailored for their own jobs. Yes I fear the case when someone don't find the needed pipe in the module and goes away instead of implementing it, while my module is here to provide the syntax, not the implementations.

But I like your starmap idea, can you please add a section in the README, after "Constructing your own", titled maybe "Recipes" and putting it here? I would accept such a PR. It would be a nice way not to loose it, and would be a nice way to encourage people to write their own @pipe functions freely.

@ghost
Copy link
Author

ghost commented Sep 1, 2022

Yeah can do - I appreciate the sentiment of what you want this library to do. And for such easy to implement functionality its not an issue re-creating it when you need it. I like the recipes idea too, will put a PR up for this

@jeff-dh
Copy link

jeff-dh commented Sep 4, 2022

You could also create a repository (or even pypi package) that depends on pipe and which includes a collection of "custom pipes". This would leave the core package (pipe) as compact as it is, but there could still be a package -- or repository -- available that provides all kinds of pipes.

pip install pipe_collection

from pipe_collection import starmap
...

@JulienPalard
Copy link
Owner

@jeff-dh Yes, it would work! Sadly for the moment I don't have much to add to it, so I won't do it in the near future.

Anyone interested in maintaining this is obviously free and encouraged to do it.

@AdrianCert
Copy link

As @JulienPalard said earlier, it can be a solution to have a collection for different pipes, but there is a problem: if you have a large number of functions, the initialization time will be very high, even if you use only one function, all the others must be defined. Is it worth it?

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

3 participants