-
Notifications
You must be signed in to change notification settings - Fork 202
/
Copy pathfunc.py
84 lines (66 loc) · 2.22 KB
/
func.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from functools import partial, wraps
from inspect import getargspec
from .op import identity, flip
class F(object):
"""Provide simple syntax for functions composition
(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)))
15
"""
__slots__ = "f",
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
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
(func, arg1, arg2, ...)
"""
return self.__class__(*f) if isinstance(f, tuple) else f
def __rshift__(self, g):
"""Overload >> operator for F instances"""
return self.__class__.__compose(self.__ensure_callable(g), self.f)
def __lshift__(self, g):
"""Overload << operator for F instances"""
return self.__class__.__compose(self.f, self.__ensure_callable(g))
def __call__(self, *args, **kwargs):
"""Overload apply operator"""
return self.f(*args, **kwargs)
def curried(func):
"""A decorator that makes the function curried
Usage example:
>>> @curried
... def sum5(a, b, c, d, e):
... return a + b + c + d + e
...
>>> sum5(1)(2)(3)(4)(5)
15
>>> sum5(1, 2, 3)(4, 5)
15
"""
@wraps(func)
def _curried(*args, **kwargs):
f = func
count = 0
while isinstance(f, partial):
if f.args:
count += len(f.args)
f = f.func
spec = getargspec(f)
if count == len(spec.args) - len(args):
return func(*args, **kwargs)
return curried(partial(func, *args, **kwargs))
return _curried