-
Notifications
You must be signed in to change notification settings - Fork 0
/
lamderp.py
308 lines (264 loc) · 7.94 KB
/
lamderp.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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
import operator
class lambderp(object):
def __init__(self):
self.stack = []
self.start = False
def binary(self, other, op, right=False):
if self.start is True:
ret = lambderp()
else:
ret = self
if type(other) == lambderp:
if right:
ret.stack.append(lambda x: op(other(x), x))
else:
ret.stack.append(lambda x: op(x, other(x)))
else:
if right:
ret.stack.append(lambda x: op(other, x))
else:
ret.stack.append(lambda x: op(x, other))
return ret
def unary(self, op):
self.stack.append(lambda x: op(x))
return self
def __add__(self, other):
"""Left addition
>>> map(_ + 1, [1, 2, 3])
[2, 3, 4]
"""
return self.binary(other, operator.add)
def __sub__(self, other):
"""Left subtraction
>>> map(_ - 1, [1, 2, 3])
[0, 1, 2]
"""
return self.binary(other, operator.sub)
def __mul__(self, other):
"""Left Multiplication
>>> map(_ * 2, [1, 2, 3])
[2, 4, 6]
"""
return self.binary(other, operator.mul)
def __floordiv__(self, other):
"""Left floor division
>>> map(_ // 2, [1, 2, 3])
[0, 1, 1]
"""
return self.binary(other, operator.floordiv)
def __mod__(self, other):
"""Left Modulo operator
>>> filter(_ % 2 == 1, range(10))
[1, 3, 5, 7, 9]
"""
return self.binary(other, operator.mod)
def __divmod__(self, other):
raise Exception('divmod is not currently supported')
def __pow__(self, other):
"""Exponentiation operator
>>> map(_ ** 2, [2, 3, 4])
[4, 9, 16]
"""
return self.binary(other, operator.pow)
def __lshift__(self, other):
"""Left shift operator
>>> map(_ << 2, [2, 3, 4])
[8, 12, 16]
"""
return self.binary(other, operator.lshift)
def __rshift__(self, other):
"""Right shift operator
>>> map(_ >> 2, [2, 3, 4])
[0, 0, 1]
"""
return self.binary(other, operator.rshift)
def __and__(self, other):
"""Binary and operator
>>> map(_ & 1, [1, 0])
[1, 0]
"""
return self.binary(other, operator.__and__)
def __xor__(self, other):
"""Binary xor operator
>>> map(_ ^ 1, [0, 1])
[1, 0]
"""
return self.binary(other, operator.xor)
def __or__(self, other):
"""Binary or operator
>>> map(_ | 1, [0, 1])
[1, 1]
"""
return self.binary(other, operator.__or__)
def __div__(self, other):
"""Divison operator
>>> map(_ / 2, [1, 2, 4, 8])
[0, 1, 2, 4]
"""
return self.binary(other, operator.div)
def __truediv__(self, other):
"""Divison operator
>>> from __future__ import division
>>> map(_ / 2, [1, 2, 4, 8])
[0, 1, 2, 4]
"""
return self.binary(other, operator.truediv)
def __radd__(self, other):
"""Right addition
>>> map(1 + _, [1, 2, 3])
[2, 3, 4]
"""
return self.binary(other, operator.add, right=True)
def __rsub__(self, other):
"""Right subtraction
>>> map(1 - _, [1, 2, 3])
[0, -1, -2]
"""
return self.binary(other, operator.sub, right=True)
def __rmul__(self, other):
"""Right Multiplication
>>> map(2 * _, [1, 2, 3])
[2, 4, 6]
"""
return self.binary(other, operator.mul, right=True)
def __rfloordiv__(self, other):
"""Right floor division
>>> map(2 // _, [1, 2, 3])
[2, 1, 0]
"""
return self.binary(other, operator.floordiv, right=True)
def __rmod__(self, other):
"""Right Modulo operator
>>> map(2 % _, range(1, 5))
[0, 0, 2, 2]
"""
return self.binary(other, operator.mod, right=True)
def __rdivmod__(self, other):
raise Exception('divmod is not currently supported')
def __rpow__(self, other):
"""Exponentiation operator
>>> map(2 ** _, [2, 3, 4])
[4, 8, 16]
"""
return self.binary(other, operator.pow, right=True)
def __rlshift__(self, other):
"""Right Left shift operator
>>> map(2 << _, [2, 3, 4])
[8, 16, 32]
"""
return self.binary(other, operator.lshift, right=True)
def __rrshift__(self, other):
"""Right Right shift operator
>>> map(8 >> _, [2, 3, 4])
[2, 1, 0]
"""
return self.binary(other, operator.rshift, right=True)
def __rand__(self, other):
"""Right Binary and operator
>>> map(1 & _, [1, 0])
[1, 0]
"""
return self.binary(other, operator.__and__, right=True)
def __rxor__(self, other):
"""Right Binary xor operator
>>> map(1 ^ _, [0, 1])
[1, 0]
"""
return self.binary(other, operator.xor, right=True)
def __ror__(self, other):
"""Right Binary or operator
>>> map(1 | _, [0, 1])
[1, 1]
"""
return self.binary(other, operator.__or__, right=True)
def __rdiv__(self, other):
"""Right Divison operator
>>> map(8 / _, [1, 2, 4, 8])
[8, 4, 2, 1]
"""
return self.binary(other, operator.div, right=True)
def __rtruediv__(self, other):
"""Right True Divison operator
>>> from __future__ import division
>>> map(8 / _, [1, 2, 4, 8])
[8, 4, 2, 1]
"""
return self.binary(other, operator.truediv, right=True)
def __lt__(self, other):
"""Less than operator
>>> map(_ < 2, [1, 2, 3])
[True, False, False]
>>> map(2 < _, [1, 2, 3])
[False, False, True]
"""
return self.binary(other, operator.lt)
def __le__(self, other):
"""Less than or equal to operator
>>> map(_ <= 2, [1, 2, 3])
[True, True, False]
>>> map(2 <= _, [1, 2, 3])
[False, True, True]
"""
return self.binary(other, operator.le)
def __eq__(self, other):
"""Equal to operator
>>> map(_ == 2, [1, 2, 3])
[False, True, False]
>>> map(2 == _, [1, 2, 3])
[False, True, False]
"""
return self.binary(other, operator.eq)
def __ne__(self, other):
"""Not equal to operator
>>> map(_ != 2, [1, 2, 3])
[True, False, True]
>>> map(2 != _, [1, 2, 3])
[True, False, True]
"""
return self.binary(other, operator.ne)
def __ge__(self, other):
"""Greater than or equal to operator
>>> map(_ >= 2, [1, 2, 3])
[False, True, True]
>>> map(2 >= _, [1, 2, 3])
[True, True, False]
"""
return self.binary(other, operator.ge)
def __gt__(self, other):
"""Greater than operator
>>> map(_ > 2, [1, 2, 3])
[False, False, True]
>>> map(2 > _, [1, 2, 3])
[True, False, False]
"""
return self.binary(other, operator.gt)
# Unary operations
def __neg__(self):
"""Negation operator
>>> map(- _, [1, 2, -3])
[-1, -2, 3]
"""
return self.unary(operator.neg)
def __pos__(self):
"""Positive operator
>>> map(+ _, [1, -2, 3])
[1, -2, 3]
"""
return self.unary(operator.pos)
def __abs__(self):
"""Absolute value operator
>>> map(abs(_), [1, -2, 3])
[1, 2, 3]
"""
return self.unary(operator.abs)
def __call__(self, arg):
self.stack.insert(0, lambda x: x) # Push an identity
res = arg
for f in self.stack:
res = f(res)
return res
_ = lambderp()
_.start = True
if __name__ == "__main__":
import doctest
doctest.testmod()