Skip to content

Conversation

dimitriye98
Copy link

An implementation of a utility function I find myself needing to implement in almost every codebase I use toolz in. uncurry takes a function of multiple arguments and returns a function which takes a single tuple or dict and calls the wrapped function with the argument unpacked. Based on the Haskell function of the same name. This is particularly useful for function pipelines with lambdas, as Python 3 removed the implicit destructuring of lambda arguments present in Python 2.

I considered also the use of star as the name, to match itertools.starmap, however as this also supports dictionaries I decided a new name made sense. Alternatively, we could be explicit on whether a dict or a tuple is being passed, e.g.

@curry
def star(func, args):
    return func(*args)

@curry
def sstar(func, kwargs):
    return func(**kwargs)

My preference is letting the dispatching be automatic, but I can see the argument both ways.

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

Successfully merging this pull request may close these issues.

1 participant