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

Improve flip to accept *args and kwargs #195

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cytoolz/_signatures.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,10 @@
lambda: None,
lambda func: None,
lambda func, a: None,
lambda func, a, b: None],
lambda func, a, b: None,
lambda func, *args, **kwargs: None],
_flip=[
lambda func, a, b: None],
lambda func, *args, **kwargs: None],
identity=[
lambda x: None],
juxt=[
Expand Down
2 changes: 1 addition & 1 deletion cytoolz/functoolz.pxd
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ cdef class juxt:
cpdef object do(object func, object x)


cpdef object flip(object func, object a, object b)
cpdef object c_flip(object func, tuple args, dict kwargs)


cpdef object return_none(object exc)
Expand Down
8 changes: 6 additions & 2 deletions cytoolz/functoolz.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -751,7 +751,11 @@ cpdef object do(object func, object x):
return x


cpdef object flip(object func, object a, object b):
cpdef object c_flip(object func, tuple args, dict kwargs):
return PyObject_Call(func, tuple(reversed(args)), kwargs)


def flip(func, *args, **kwargs):
"""
Call the function call with the arguments flipped

Expand All @@ -775,7 +779,7 @@ cpdef object flip(object func, object a, object b):
>>> only_ints
[1, 2, 3]
"""
return PyObject_CallObject(func, (b, a))
return c_flip(func, args, kwargs)


_flip = flip # uncurried
Expand Down
7 changes: 7 additions & 0 deletions cytoolz/tests/test_functoolz.py
Original file line number Diff line number Diff line change
Expand Up @@ -735,6 +735,13 @@ def f(a, b):
assert flip(f, 'a', 'b') == ('b', 'a')


def test_flip_args_kwargs():
def g(a, b, c, *, d, e):
return a, b, c, d, e

assert flip(g, 3, 2, 1, d=4, e=5) == (1, 2, 3, 4, 5)


def test_excepts():
# These are descriptors, make sure this works correctly.
assert excepts.__name__ == 'excepts'
Expand Down