funcy icon indicating copy to clipboard operation
funcy copied to clipboard

Add unpack decorator

Open ADR-007 opened this issue 7 years ago • 6 comments

I propose to add a decorator which will unpack argument:

    def process(foo, bar):
        ...
    
    map(unpack(process), product(['foo1', 'foo2'], ['bar1', 'bar2']))

Is it a funcy way?

ADR-007 avatar Feb 10 '18 22:02 ADR-007

Look at itertools.starmap(), it covers your use case.

Suor avatar Feb 11 '18 00:02 Suor

Actually, no. I have code like this pool.imap_unordered(unpack(process), iterable).

But you are right: in other cases, I can use starmap. So this function is very specific and I don't know whether it is a good idea add it to funcy.

ADR-007 avatar Feb 11 '18 01:02 ADR-007

If process() is your function you can just unpack in a body, this narrows use cases. I agree that general usability is questionable. Other considerations (if we are going to add it) are name and keyword args support, which slows down the more common case.

So I let it hang for a while, let's see if anyone else wants it.

Suor avatar Feb 11 '18 07:02 Suor

Yes, process() is my own function, but I don't want to change it.

Other considerations (if we are going to add it) are name and keyword args support, which slows down the more common case.

So I let it hang for a while, let's see if anyone else wants it.

I agree

ADR-007 avatar Feb 11 '18 10:02 ADR-007

I just ran into a case where this could have been used. Unpacking the arguments in other ways is simple and readable enough that I don't think adding this is worthwhile.

taleinat avatar May 11 '18 05:05 taleinat

As the mention above indicates, I ran into a case where this could be useful on a function composition pipeline if one of the fucntions returned a tuple with the arguments the next function needs, we could wrap the next function with the unpack() decorator.

cardoso-neto avatar Oct 12 '21 20:10 cardoso-neto