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

Thoughts on partialz with ellipsis? #601

Open
Diego-MX opened this issue Jan 8, 2025 · 0 comments
Open

Thoughts on partialz with ellipsis? #601

Diego-MX opened this issue Jan 8, 2025 · 0 comments

Comments

@Diego-MX
Copy link

Diego-MX commented Jan 8, 2025

I really enjoy using this package.
The functional approach is very attractive to me.
And in particular, I often use pipe and thread_(first|last).

Upon lot's of use I've made my own modifications and therefore would like to ask about it here to see if it's the philosophy of the package and perhaps I can try a pull request.

I should mention that I've never made a contribution to an external package, and therefore don't know about the real blows and whistles about such collaboration. So thank you for your comprehension.

The functions that I like are the following:

from functools import partial, reduce
from itertools import chain

VARHOLDER = Ellipsis

class partialz(partial): 
    """Allow Ellipsis (...) as variable holder."""
    def __call__(self, *args, **keywords): 
        args_i = iter(args)
        args_0 = (next(iargs) if arg is VARHOLDER else arg for arg in self.args)
        argz   = chain(args_0, args_i) 
        keywordz = {**self.keywords, **keywords}
        return self.func(*argz, **keywordz)

def thread(val, *forms): 
    """Unify thread_(first|last) to use Ellipsis placeholder."""
    eval_ff = (lambda vv, ff: 
        partial2(*ff)(vv) if isinstance(ff, tuple) else ff(vv))
    return reduce(eval_ff, forms, val)

## This one is less important, but shows up in partialz

def interweave(seq1, seq2, cond=None):
    """Replace elements of seq1 where cond is True with elements from seq2."""
    cond = cond or (lambda ee: ee is None)  # return_none? 
    seq_i = iter(seq2)
    seq_0 = (next(seq_i) if cond(elem) else elem for elem in seq1)
    return chain(seq_0, seq_i)

So the motivation for all of this was generalizing thread_(first|last) to having the arguments in any place, and that's how I came to find about something like partialz that use Ellipsis as placeholder for the variables.

And finally I was looking into ways to combine sequences based on the arguments argz in partialz, as the following line seemed to show room for simplification.

(next(iargs) if arg is VARHOLDER else arg for arg in self.args)

So my question is, Do these changes make sense in the project?
Thank you for reading me.

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

1 participant