Skip to content

Curried bug second attempt #75

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

Open
wants to merge 4 commits 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
28 changes: 16 additions & 12 deletions fn/func.py
Original file line number Diff line number Diff line change
@@ -1,40 +1,41 @@
from functools import partial, wraps
from functools import partial, wraps, update_wrapper
from inspect import getargspec

from .op import identity, flip
from .op import identity


class F(object):
"""Provide simple syntax for functions composition
(through << and >> operators) and partial function
application (through simple tuple syntax).
(through << and >> operators) and partial function
application (through simple tuple syntax).

Usage example:

>>> func = F() << (_ + 10) << (_ + 5)
>>> print(func(10))
25
>>> func = F() >> (filter, _ < 6) >> sum
>>> print(func(range(10)))
>>> print(func(range(10)))
15
"""

__slots__ = "f",
__slots__ = "f",

def __init__(self, f = identity, *args, **kwargs):
def __init__(self, f=identity, *args, **kwargs):
self.f = partial(f, *args, **kwargs) if any([args, kwargs]) else f

@classmethod
def __compose(cls, f, g):
"""Produces new class intance that will
"""Produces new class intance that will
execute given functions one by one. Internal
method that was added to avoid code duplication
in other methods.
"""
return cls(lambda *args, **kwargs: f(g(*args, **kwargs)))

def __ensure_callable(self, f):
"""Simplify partial execution syntax.
Rerurn partial function built from tuple
"""Simplify partial execution syntax.
Rerurn partial function built from tuple
(func, arg1, arg2, ...)
"""
return self.__class__(*f) if isinstance(f, tuple) else f
Expand All @@ -47,7 +48,7 @@ def __lshift__(self, g):
"""Overload << operator for F instances"""
return self.__class__.__compose(self.f, self.__ensure_callable(g))

def __call__(self, *args, **kwargs):
def __call__(self, *args, **kwargs):
"""Overload apply operator"""
return self.f(*args, **kwargs)

Expand Down Expand Up @@ -80,5 +81,8 @@ def _curried(*args, **kwargs):
if count == len(spec.args) - len(args):
return func(*args, **kwargs)

return curried(partial(func, *args, **kwargs))
para_func = partial(func, *args, **kwargs)
update_wrapper(para_func, f)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

f or func?

f was the iterator variable in the function loop, above. I suspect it's not the right choice here.

return curried(para_func)

return _curried
30 changes: 30 additions & 0 deletions tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,42 @@
from fn import op, _, F, Stream, iters, underscore, monad, recur
from fn.uniform import reduce
from fn.immutable import SkewHeap, PairingHeap, LinkedList, Stack, Queue, Vector, Deque
from fn.func import curried


class InstanceChecker(object):
if sys.version_info[0] == 2 and sys.version_info[1] <= 6:
def assertIsInstance(self, inst, cls):
self.assertTrue(isinstance(inst, cls))


class Curriedtest(unittest.TestCase):

def test_curried_wrapper(self):

@curried
def _child(a, b, c, d):
return a + b + c + d

@curried
def _moma(a, b):
return _child(a, b)

def _assert_instance(expected, acutal):
self.assertEqual(expected.__module__, acutal.__module__)
self.assertEqual(expected.__name__, acutal.__name__)

res1 = _moma(1)
_assert_instance(_moma, res1)
res2 = res1(2)
_assert_instance(_child, res2)
res3 = res2(3)
_assert_instance(_child, res3)
res4 = res3(4)

self.assertEqual(res4, 10)


class OperatorTestCase(unittest.TestCase):

def test_currying(self):
Expand Down