forked from kachayev/fn.py
-
Notifications
You must be signed in to change notification settings - Fork 14
/
underscore.py
196 lines (158 loc) · 6.48 KB
/
underscore.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import operator
import random
import re
import string
from itertools import count, repeat
from sys import version_info
from .func import F
from .op import apply, flip, identity
from .uniform import map, zip
div = operator.div if version_info[0] == 2 else operator.truediv
letters = string.letters if version_info[0] == 2 else string.ascii_letters
def _random_name():
return "".join(random.choice(letters) for _ in range(14))
def fmap(f, format):
def applyier(self, other):
fmt = "(%s)" % format.replace("self", self._format)
if isinstance(other, self.__class__):
return self.__class__(
(f, self, other),
fmt.replace("other", other._format),
dict(
list(self._format_args.items()) +
list(other._format_args.items())
),
self._arity + other._arity
)
else:
call = F(flip(f), other) << F(self)
name = _random_name()
return self.__class__(
call,
fmt.replace("other", "%%(%s)r" % name),
dict(list(self._format_args.items()) + [(name, other)]),
self._arity)
return applyier
class ArityError(TypeError):
def __str__(self):
return "{0!r} expected {1} arguments, got {2}".format(*self.args)
def unary_fmap(f, format):
def applyier(self):
fmt = "(%s)" % format.replace("self", self._format)
return self.__class__(
F(self) << f, fmt, self._format_args, self._arity
)
return applyier
class _Callable(object):
__slots__ = "_callback", "_format", "_format_args", "_arity"
# Do not use "flipback" approach for underscore callable,
# see https://github.com/kachayev/fn.py/issues/23
__flipback__ = None
def __init__(self, callback=identity, format="_", format_args=None,
arity=1):
self._callback = callback
self._format = format
self._format_args = format_args or {}
self._arity = arity
def call(self, name, *args, **kwargs):
"""Call method from _ object by given name and arguments"""
return self.__class__(
F(lambda f:
apply(f, args, kwargs)) << operator.attrgetter(name) << F(self)
)
def __getattr__(self, name):
if name == '__wrapped__': # Guard for recursive call by doctest
raise AttributeError
attr_name = _random_name()
return self.__class__(
F(operator.attrgetter(name)) << F(self),
"getattr(%s, %%(%s)r)" % (self._format, attr_name),
dict(
list(self._format_args.items()) + [(attr_name, name)]
),
self._arity
)
def __getitem__(self, k):
if isinstance(k, self.__class__):
return self.__class__(
(operator.getitem, self, k),
"%s[%s]" % (self._format, k._format),
dict(
list(self._format_args.items()) +
list(k._format_args.items())
),
self._arity + k._arity
)
item_name = _random_name()
return self.__class__(
F(operator.itemgetter(k)) << F(self),
"%s[%%(%s)r]" % (self._format, item_name),
dict(list(self._format_args.items()) + [(item_name, k)]),
self._arity
)
def __str__(self):
"""Build readable representation for function
(_ < 7): (x1) => (x1 < 7)
(_ + _*10): (x1, x2) => (x1 + (x2*10))
"""
# args iterator with produce infinite sequence
# args -> (x1, x2, x3, ...)
args = map("".join, zip(repeat("x"), map(str, count(1))))
l, r = [], self._format
# replace all "_" signs from left to right side
while r.count("_"):
n = next(args)
r = r.replace("_", n, 1)
l.append(n)
r = r % self._format_args
return "({left}) => {right}".format(left=", ".join(l), right=r)
def __repr__(self):
"""
Return original function notation to ensure that eval(repr(f)) == f
"""
return re.sub(r"x\d+", "_", str(self).split("=>", 1)[1].strip())
def __call__(self, *args):
if len(args) != self._arity:
raise ArityError(self, self._arity, len(args))
if not isinstance(self._callback, tuple):
return self._callback(*args)
f, left, right = self._callback
return f(left(*args[:left._arity]), right(*args[left._arity:]))
__add__ = fmap(operator.add, "self + other")
__mul__ = fmap(operator.mul, "self * other")
__sub__ = fmap(operator.sub, "self - other")
__mod__ = fmap(operator.mod, "self %% other")
__pow__ = fmap(operator.pow, "self ** other")
__and__ = fmap(operator.and_, "self & other")
__or__ = fmap(operator.or_, "self | other")
__xor__ = fmap(operator.xor, "self ^ other")
__div__ = fmap(div, "self / other")
__divmod__ = fmap(divmod, "self / other")
__floordiv__ = fmap(operator.floordiv, "self / other")
__truediv__ = fmap(operator.truediv, "self / other")
__lshift__ = fmap(operator.lshift, "self << other")
__rshift__ = fmap(operator.rshift, "self >> other")
__lt__ = fmap(operator.lt, "self < other")
__le__ = fmap(operator.le, "self <= other")
__gt__ = fmap(operator.gt, "self > other")
__ge__ = fmap(operator.ge, "self >= other")
__eq__ = fmap(operator.eq, "self == other")
__ne__ = fmap(operator.ne, "self != other")
__neg__ = unary_fmap(operator.neg, "-self")
__pos__ = unary_fmap(operator.pos, "+self")
__invert__ = unary_fmap(operator.invert, "~self")
__radd__ = fmap(flip(operator.add), "other + self")
__rmul__ = fmap(flip(operator.mul), "other * self")
__rsub__ = fmap(flip(operator.sub), "other - self")
__rmod__ = fmap(flip(operator.mod), "other %% self")
__rpow__ = fmap(flip(operator.pow), "other ** self")
__rdiv__ = fmap(flip(div), "other / self")
__rdivmod__ = fmap(flip(divmod), "other / self")
__rtruediv__ = fmap(flip(operator.truediv), "other / self")
__rfloordiv__ = fmap(flip(operator.floordiv), "other / self")
__rlshift__ = fmap(flip(operator.lshift), "other << self")
__rrshift__ = fmap(flip(operator.rshift), "other >> self")
__rand__ = fmap(flip(operator.and_), "other & self")
__ror__ = fmap(flip(operator.or_), "other | self")
__rxor__ = fmap(flip(operator.xor), "other ^ self")
shortcut = _Callable()