Thoughts on partialz with ellipsis?

Open
#601 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
25/100
Issue type
Feature
Clarity
Needs clarification
Activity status
Stale
Tech stack
python
Domain
tooling

Research direction

Start by reviewing the existing thread_(first|last) entry points and partial-related APIs in toolz, then compare the proposed partialz and Ellipsis placeholder behavior with the package's functional design. The issue does not name files or tests, and its open-ended question means completion would require maintainer agreement on scope and direction.

Written by the indexing model from the issue text.

Description

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.

Dominant language
Python
Stars
5.2k
Forks
280
Avg merge
6d 17h
Merged PRs (30d)
4

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from pytoolz/toolz

All issues in pytoolz/toolz

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.