From 5b222b35f1875048c0940fa27733e216c64a2e34 Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Thu, 16 Aug 2018 14:05:55 +0100 Subject: [PATCH 1/7] added clojure style function --- toolz/functoolz.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/toolz/functoolz.py b/toolz/functoolz.py index 238d1b16..e8bfc3ae 100644 --- a/toolz/functoolz.py +++ b/toolz/functoolz.py @@ -21,6 +21,19 @@ def identity(x): return x +def apply(func, *args, **kwargs): + """ Applies a function and returns the results + >>> def double(x): return 2*x + >>> def inc(x): return x + 1 + >>> apply(double, 5) + 10 + + >>> tuple(map(apply, [double, inc, double], [10, 500, 8000])) + (20, 501, 16000) + """ + return func(*args, **kwargs) + + def thread_first(val, *forms): """ Thread value through a sequence of functions/forms From 89665afc3f92cc67c6ea2da337729c31e1145d37 Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Thu, 16 Aug 2018 14:26:54 +0100 Subject: [PATCH 2/7] test for apply --- toolz/functoolz.py | 2 +- toolz/tests/test_functoolz.py | 7 ++++++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/toolz/functoolz.py b/toolz/functoolz.py index e8bfc3ae..792ddbc5 100644 --- a/toolz/functoolz.py +++ b/toolz/functoolz.py @@ -8,7 +8,7 @@ from .utils import no_default -__all__ = ('identity', 'thread_first', 'thread_last', 'memoize', 'compose', +__all__ = ('identity', 'apply', 'thread_first', 'thread_last', 'memoize', 'compose', 'pipe', 'complement', 'juxt', 'do', 'curry', 'flip', 'excepts') diff --git a/toolz/tests/test_functoolz.py b/toolz/tests/test_functoolz.py index deda6068..01830a9c 100644 --- a/toolz/tests/test_functoolz.py +++ b/toolz/tests/test_functoolz.py @@ -1,7 +1,7 @@ import platform from toolz.functoolz import (thread_first, thread_last, memoize, curry, - compose, pipe, complement, do, juxt, flip, excepts) + compose, pipe, complement, do, juxt, flip, excepts, apply) from operator import add, mul, itemgetter from toolz.utils import raises from functools import partial @@ -23,6 +23,11 @@ def double(x): return 2 * x +def test_apply(): + assert apply(double, 5) == 10 + assert tuple(map(apply, [double, inc, double], [10, 500, 8000])) == (20, 501, 16000) + + def test_thread_first(): assert thread_first(2) == 2 assert thread_first(2, inc) == 3 From 68a3591f33305d5458c9e72bff015589c7c06d75 Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Fri, 23 Nov 2018 12:13:24 +0000 Subject: [PATCH 3/7] change signature of `apply` as per @eriknw --- toolz/functoolz.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/toolz/functoolz.py b/toolz/functoolz.py index 792ddbc5..78eccff2 100644 --- a/toolz/functoolz.py +++ b/toolz/functoolz.py @@ -21,7 +21,7 @@ def identity(x): return x -def apply(func, *args, **kwargs): +def apply(*func_and_args, **kwargs): """ Applies a function and returns the results >>> def double(x): return 2*x >>> def inc(x): return x + 1 @@ -31,6 +31,9 @@ def apply(func, *args, **kwargs): >>> tuple(map(apply, [double, inc, double], [10, 500, 8000])) (20, 501, 16000) """ + if not func_and_args: + raise TypeError('func argument is required') + func, args = func_and_args[0], func_and_args[1:] return func(*args, **kwargs) From dcebedde4b5c8ceeffca1a2ae42a35bf61d53e0b Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Fri, 23 Nov 2018 12:23:21 +0000 Subject: [PATCH 4/7] add apply to toolz.curried --- toolz/curried/__init__.py | 1 + 1 file changed, 1 insertion(+) diff --git a/toolz/curried/__init__.py b/toolz/curried/__init__.py index 43aeffd4..11a0d32f 100644 --- a/toolz/curried/__init__.py +++ b/toolz/curried/__init__.py @@ -55,6 +55,7 @@ from .exceptions import merge, merge_with accumulate = toolz.curry(toolz.accumulate) +apply = toolz.curry(toolz.apply) assoc = toolz.curry(toolz.assoc) assoc_in = toolz.curry(toolz.assoc_in) cons = toolz.curry(toolz.cons) From 4d670925aca373cca4bb450b39022f487e101b91 Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Fri, 23 Nov 2018 13:30:55 +0000 Subject: [PATCH 5/7] add apply to toolz.curried --- toolz/curried/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/toolz/curried/__init__.py b/toolz/curried/__init__.py index 11a0d32f..fab6255f 100644 --- a/toolz/curried/__init__.py +++ b/toolz/curried/__init__.py @@ -26,6 +26,7 @@ import toolz from . import operator from toolz import ( + apply, comp, complement, compose, @@ -55,7 +56,6 @@ from .exceptions import merge, merge_with accumulate = toolz.curry(toolz.accumulate) -apply = toolz.curry(toolz.apply) assoc = toolz.curry(toolz.assoc) assoc_in = toolz.curry(toolz.assoc_in) cons = toolz.curry(toolz.cons) From 65c81ca35a46596f27798fe2a7ba94ef845b904f Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Fri, 23 Nov 2018 13:36:12 +0000 Subject: [PATCH 6/7] format __all__ to fit in line breaks --- toolz/functoolz.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/toolz/functoolz.py b/toolz/functoolz.py index 78eccff2..be6fa204 100644 --- a/toolz/functoolz.py +++ b/toolz/functoolz.py @@ -8,8 +8,9 @@ from .utils import no_default -__all__ = ('identity', 'apply', 'thread_first', 'thread_last', 'memoize', 'compose', - 'pipe', 'complement', 'juxt', 'do', 'curry', 'flip', 'excepts') +__all__ = ('identity', 'apply', 'thread_first', 'thread_last', 'memoize', + 'compose', 'pipe', 'complement', 'juxt', 'do', 'curry', 'flip', + 'excepts') def identity(x): From 47047483fb30b58ef765adaf01cc03777ac56375 Mon Sep 17 00:00:00 2001 From: Elias Mistler Date: Fri, 23 Nov 2018 13:46:08 +0000 Subject: [PATCH 7/7] complete code coverage --- toolz/tests/test_functoolz.py | 1 + 1 file changed, 1 insertion(+) diff --git a/toolz/tests/test_functoolz.py b/toolz/tests/test_functoolz.py index 01830a9c..dcb43af1 100644 --- a/toolz/tests/test_functoolz.py +++ b/toolz/tests/test_functoolz.py @@ -26,6 +26,7 @@ def double(x): def test_apply(): assert apply(double, 5) == 10 assert tuple(map(apply, [double, inc, double], [10, 500, 8000])) == (20, 501, 16000) + assert raises(TypeError, apply) def test_thread_first():