On Mon, Jul 9, 2018 at 12:22 PM, Chris Barker <chris.bar...@noaa.gov> wrote:
> On Fri, Jul 6, 2018 at 12:26 PM, Franklin? Lee I use this kind of function

>> I added several options, such as:
>> - key function
>> - value function
>> - "ignore": Skip values with these keys.
>> - "postprocess": Apply a function to each group after completion.
>> - Pass in the container to store in. For example, create an
>> OrderedDict and pass it in. It may already hold items.
>> - Specify the container for each group.
>> - Specify how to add to the container for each group.
>
>
> interesting...
>
>>
>> Then I cut it down to two optional parameters:
>> - key function. If not provided, the iterable is considered to have
>> key-value pairs.
>
>
> OK -- seems we're all converging on that :-)
>
>>
>> - The storage container.
>
>
> so this means you'r passing in a full set of storage containers? I'm a vit
> confused by that -- if they might be pre-populated, then they would need to
> be instance,s an you'd need to have one for every key -- how would you know
> in advance aht you needed???

No, I mean the mapping (outer) container. For example, I can pass in
an empty OrderedDict, or a dict that already contained some groups
from a previous call to the grouping function.

I took out the option for the per-group (inner) containers. I never
found it necessary to scrooge (scrooge on?) the memory, when I could
postprocessed the lists after grouping. A mapvalues function will make
postprocessing more convenient, and lend weight to a dicttools
suggestion.

    # Unfortunate double meaning of 'map' in the function signature:
    def mapvalues(f, mapping):
        try:
            items = mapping.items()
        except AttributeError:
            items = mapping
        return {k: f(v) for k, v in items}

> I played around with passing in a optional storage object:
>
> https://github.com/PythonCHB/grouper/commit/d986816905406ec402724beaed2b88c96df64469
>
> but as we might want a list or a set, or a Counter, or ??? it got pretty
> ugly, as lists and sets and Counters all have different APIs for adding
> stuff. So I gave up and figured just saying "it's always a list) made the
> most sense.

My solution at the time was to add another parameter to specify how to
add to the container.

In fuller generality, the option for the per-group container may
consist of specifying a monad (if I remember monads correctly). You
need to at least specify a per-group container constructor and a
binary function that adds to it. In the case of `Counter`, the
constructor is `int` and the binary function is `int.__add__`, and the
Counter constructor effectively runs concurrent `reduce`.
_______________________________________________
Python-ideas mailing list
Python-ideas@python.org
https://mail.python.org/mailman/listinfo/python-ideas
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to