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

Consider introducing starstarmap #679

Open
Skeen opened this issue Feb 24, 2023 · 1 comment
Open

Consider introducing starstarmap #679

Skeen opened this issue Feb 24, 2023 · 1 comment
Labels
pr-welcome We are open to PRs that fix this issue - leave a note if you're working on it!

Comments

@Skeen
Copy link

Skeen commented Feb 24, 2023

Description

starstarmap is the lost cousin of itertools.starmap, so whereas the relation between map and starmap parallels the relation between function(a,b) and function(*c), the relationship between map and starstarmap parallels the relation between function(a,b) and function(**d).

And similar to how starmap can be implemented as:

def starmap(function, iterable):
    for args in iterable:
        yield function(*args)

starstarmap could be implemented as:

def starstarmap(function, iterable):
    for args in iterable:
        yield function(**args)

References

The argument for inclusion would be the fact that it simply seems missing from itertools, and its usefulness follows from the same pattern as starmap.

Examples

A REST interface returns a list of JSON objects:

[
    {"identifier": 1, "title": "The Cathedral and the Bazaar"},
    {"identifier": 2, "title": "Homesteading the Noosphere"},
   ...
]

Each object needs to be processed and transformed via a processing function:

def func(identifier, title):
    ...
    return Book(...)

Now to convert from the JSON-parsed list of dictionaries to processed books, we can simply do:

books = starstarmap(func, dictionaries)
@bbayles bbayles added the pr-welcome We are open to PRs that fix this issue - leave a note if you're working on it! label Feb 24, 2023
@bbayles
Copy link
Collaborator

bbayles commented Feb 24, 2023

Yes, good idea! I'll accept a PR for this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-welcome We are open to PRs that fix this issue - leave a note if you're working on it!
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants