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

Add collectby built-in #620

Closed
evhub opened this issue Nov 16, 2021 · 0 comments
Closed

Add collectby built-in #620

evhub opened this issue Nov 16, 2021 · 0 comments

Comments

@evhub
Copy link
Owner

evhub commented Nov 16, 2021

Approximate implementation:

def collectby(key_func, iterable, value_func=None, reduce_func=None):
    collection = defaultdict(list) if reduce_func is None else {}
    for item in iterable:
        key = key_func(item)
        if value_func is not None:
            item = value_func(item)
        if reduce_func is None:
            collection[key].append(item)
        else:
            old_item = collection.get(key, sentinel)
            if old_item is not sentinel:
                item = reduce_func(old_item, item)
            collection[key] = item
    return collection
@evhub evhub added the feature label Nov 16, 2021
@evhub evhub added this to the v2.0.0 milestone Nov 16, 2021
evhub added a commit that referenced this issue Nov 17, 2021
@evhub evhub added the resolved label Nov 17, 2021
@evhub evhub closed this as completed Nov 17, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant