-
Notifications
You must be signed in to change notification settings - Fork 0
/
functional.py
483 lines (385 loc) · 19.5 KB
/
functional.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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
#!/usr/bin/env python
from oamap.proxy import ListProxy
from vectorized import vectorize
# This is a hack to make Python's list and iterator types functional.
import ctypes
from functools import reduce
if hasattr(ctypes.pythonapi, "Py_InitModule4_64"):
Py_ssize_t = ctypes.c_int64
else:
Py_ssize_t = ctypes.c_int
class PyObject(ctypes.Structure): pass
PyObject._fields_ = [("ob_refcnt", Py_ssize_t), ("ob_type", ctypes.POINTER(PyObject))]
class SlotsPointer(PyObject):
_fields_ = [("dict", ctypes.POINTER(PyObject))]
def proxy_builtin(cls):
name = cls.__name__
slots = getattr(cls, "__dict__", name)
pointer = SlotsPointer.from_address(id(slots))
namespace = {}
ctypes.pythonapi.PyDict_SetItem(
ctypes.py_object(namespace),
ctypes.py_object(name),
pointer.dict
)
return namespace[name]
#### Define and attach functional methods to the Python "list" type.
def sizer(lst):
"""
Return the length of the list.
Example: [1, 2, 3, 4, 5].size == 5
(For convenience, since everything else is attached at the end of a chain of methods.)
"""
return len(lst)
def enumerater(lst):
"""
Return (index, element) pairs for all elemets of the list.
Example: ["a", "b", "c", "d", "e"].enumerate ==
[(0, "a"), (1, "b"), (2, "c"), (3, "d"), (4, "e")]
"""
if isinstance(lst, (list, tuple, ListProxy)):
return list(enumerate(lst))
else:
return ((i, x) for i, x in enumerate(lst))
def taker(lst):
"""
Return the first n elements of the list or generator.
Example: [1, 2, 3, 4, 5].take(3) == [1, 2, 3]
"""
if isinstance(lst, (list, tuple, ListProxy)):
out = lambda n: lst[:n]
else:
def gen(n):
for i, x in enumerate(lst):
yield x
if i + 1 >= n: break
out = lambda n: list(gen(n))
out.func_name = "[...].take"
out.__doc__ = taker.__doc__
return out
def collecter(lst):
"""
Return all elements of the list or generator. This is a non-operation for lists, but evaluates generators (and might not halt if the generator is infinite).
Example: (i for i in range(10)).collect == [i for i in range(10)]
"""
if isinstance(lst, (list, tuple, ListProxy)):
return lst
else:
return list(lst)
def lazyer(lst):
"""
Make a list or generator lazy. This is a non-operation for generators, which are already lazy, but makes it possible to build up a chain of operations on a list without evaluating them.
Example: [1, 2, 3, 4, 5].lazy == (i for i in range(1, 6))
"""
if isinstance(lst, (list, tuple, ListProxy)):
return (x for x in lst)
else:
return lst
def mapper(lst):
"""
Apply a given function to each element of this list or generator.
The function must take one argument.
Examples: [1, 2, 3, 4, 5].map(f) == [f(1), f(2), f(3), f(4), f(5)]
[1, 2, 3, 4, 5].map(lambda x: x + 100) == [101, 102, 103, 104, 105]
"""
if isinstance(lst, (list, tuple, ListProxy)):
out = lambda f: [f(x) for x in lst]
else:
out = lambda f: (f(x) for x in lst)
out.func_name = "[...].map"
out.__doc__ = mapper.__doc__
return out
def vmapper(lst):
"""
Apply a given function to each element of this list or generator using vectorized function.
The first argument must be a function, and the second argument must be an output array.
Any number of argument can be passed to the vectorized function
"""
if isinstance(lst, (list, tuple, ListProxy)):
# out = lambda f, len_out, *args: vectorize(f, len(lst), lst, *args)
out = lambda f, out, *args: (vectorize(f, len(lst), out, lst, *args), out)[-1]
else:
out = lambda f, out, *args: (vectorize(f, len(lst), out, lst, *args), out)[-1]
out.func_name = "[...].vmap"
out.__doc__ = mapper.__doc__
return out
def flattener(lst):
"""
Turn a list-of-lists into a list of all elements. Only reduces one level of structure.
Examples: [[1, 2], [3, 4, 5]].flatten == [1, 2, 3, 4, 5]
[[1, 2], [3, [4, 5]]].flatten == [1, 2, 3, [4, 5]
"""
if isinstance(lst, (list, tuple, ListProxy)):
return sum(lst, [])
else:
def gen():
for x in lst:
for y in x:
yield y
return gen()
def flatmapper(lst):
"""
Same as [...].map(f).flatten, but these two operations are frequently done together.
The function must take one argument.
In general: [...].flatmap(f) == [...].map(f).flatten
Example: [1, 2, 3, 4, 5].flatmap(lambda x: [x, x + 100]) == [1, 101, 2, 102, 3, 103, 4, 104, 5, 105]
Flatmap is a very general operation. You can use it to expand a table, as above, or to map and filter
at the same time. (In the theory of monads, "flatmap" is the fundamental "bind" operation.)
Example: [1, 2, 3, 4, 5].flatmap(lambda x: [100 + x] if x > 2 else []) == [103, 104, 105]
You might encounter this when you want to compute something for all particles in each event, but also
handle the case when there are no particles after cuts. In that case, "flatmap" instead of "map" and
return a singleton list [result] when you have a result and an empty list [] when you don't.
"""
if isinstance(lst, (list, tuple, ListProxy)):
out = lambda f: sum((f(x) for x in lst), [])
else:
def gen(f):
for x in lst:
for y in f(x):
yield y
out = gen
out.func_name = "[...].flatmap"
out.__doc__ = flatmapper.__doc__
return out
def filterer(lst):
"""
Apply a given function to each element of the list and return only those that return True.
The function must take one argument and return True or False.
Example: [1, 2, 3, 4, 5].filter(lambda x: x > 2) == [3, 4, 5]
"""
if isinstance(lst, (list, tuple, ListProxy)):
out = lambda f: [x for x in lst if f(x)]
else:
def gen(f):
for x in lst:
if f(x):
yield x
out = gen
out.func_name = "[...].filter"
out.__doc__ = filterer.__doc__
return out
def reducer(lst):
"""
Apply a given function to each element and a running tally to produce a single result.
The function must take two arguments. The first may be an element from the list or a tally.
The second will always be from the list.
Examples: [1, 2, 3, 4, 5].reduce(f) == f(f(f(f(1, 2), 3), 4), 5)
[1, 2, 3, 4, 5].reduce(lambda x, y: x + y) == 15
"""
out = lambda f: reduce(f, lst)
out.func_name = "[...].reduce"
out.__doc__ = reducer.__doc__
return out
def sumer(lst):
"""
Compute the sum of all elements in the list.
Example: [1, 2, 3, 4, 5].sum == 15
"""
return sum(lst)
def aggregator(lst):
"""
Same as reduce, except start the aggregation on a given zero element.
The function must take two arguments. The first will always be a tally and the second from the list.
Examples: [1, 2, 3, 4, 5].aggregate(f, 0) == f(f(f(f(f(0, 1), 2), 3), 4), 5)
[1, 2, 3, 4, 5].aggregate(lambda x, y: x + y, 0) == 15
["a", "b", "c"].aggregate(lambda x, y: x + y, "") == "abc"
"""
out = lambda f, zero: reduce(f, lst, zero)
out.func_name = "[...].aggregate"
out.__doc__ = aggregator.__doc__
return out
def reducerright(lst):
"""
Same as reduce, except start the nesting on the right and work left.
The function must take two arguments. The second may be an element from the list or a tally.
The first will always be from the list.
Example: [1, 2, 3, 4, 5].reduceright(f) == f(1, f(2, f(3, f(4, 5))))
"""
out = lambda f: reduce(lambda a, b: f(b, a), reversed(lst))
out.func_name = "[...].reduceright"
out.__doc__ = reducerright.__doc__
return out
def aggregatorright(lst):
"""
Same as aggregate, except start the nesting on the right and work left.
The function must take two arguments. The second will always be a tally and the first from the list.
Example: [1, 2, 3, 4, 5].aggregateright(f, 0) == f(1, f(2, f(3, f(4, f(5, 0)))))
"""
out = lambda f, zero: reduce(lambda a, b: f(b, a), reversed(lst), zero)
out.func_name = "[...].aggregateright"
out.__doc__ = aggregatorright.__doc__
return out
def pairser(lst):
"""
Apply a given function to pairs of elements without repetition (in either order) or duplicates.
The function must take two arguments. Both will always be elements from the list.
If you think of the input list as a vector X, this acts on the upper trianglular part of the
outer product of X with X (not including diagonal).
Alternatively, it's what you would get from these nested loops:
for i in range(len(lst)):
for j in range(i + 1, len(lst)): # j starts at i + 1
f(lst[i], lst[j])
Example: [1, 2, 3, 4, 5].pairs(lambda x, y: [x, y]) == [[1, 2], [1, 3], [1, 4], [1, 5],
[2, 3], [2, 4], [2, 5],
[3, 4], [3, 5],
[4, 5]]
Use this when you want to loop over pairs of distinct pairs of elements from a single list.
Contrast with "table", which is like a nested loop over several lists, for all elements.
"""
out = lambda f: [f(x, y) for i, x in enumerate(lst) for y in lst[i + 1:]]
out.func_name = "[...].pairs"
out.__doc__ = pairser.__doc__
return out
def tabler(lsts):
"""
Apply a given function to all combinations of elements from all input lists.
The function must take as many arguments as you have lists, and each will be an element from
each list.
If you think of the input lists as vectors X, Y, Z, etc., this acts on each element of the
outer product of X with Y with Z, etc.
Alternatively, it's what you would get from these nested loops:
for x in lst_x:
for y in lst_y:
for z in lst_z:
f(x, y, z)
Examples: [[100, 200], [1, 2, 3]].table(lambda x, y: x + y) == [101, 102, 103, 201, 202, 203]
[[100, 200], [10, 20], [1, 2]].table(lambda x, y, z: x + y + z) == [
111, 112, 121, 122, 211, 212, 221, 222]
To illustrate the difference between table and pairs, consider the following:
[1, 2, 3].pairs(lambda x, y: [x, y]) == [[1, 2], [1, 3],
[2, 3]]
[[1, 2, 3], [1, 2, 3]].table(lambda x, y: [x, y]) == [[1, 1], [1, 2], [1, 3],
[2, 1], [2, 2], [2, 3],
[3, 1], [3, 2], [3, 3]]
"""
def buildargs(first, *rest):
if len(rest) == 0:
return [[x] for x in first]
else:
return [[x] + y for x in first for y in buildargs(*rest)]
if len(lsts) == 0:
out = lambda f: []
elif len(lsts) == 1:
out = lambda f: [f(x) for x in lsts[0]]
else:
first = lsts[0]
rest = lsts[1:]
out = lambda f: [f(*args) for args in buildargs(first, *rest)]
out.func_name = "[[...], [...], ...].table"
out.__doc__ = tabler.__doc__
return out
def zipper(lsts):
"""
Apply a function to the ith element of each list, for all i.
The function must take as many arguments as there are lists, and each will be an element from
each list.
This works just like the built-in Python zip, but applies the function to its results:
for x, y, z in zip(lst_x, lst_y, lst_z):
f(x, y, z)
Example: [[1, 2, 3], ["a", "b", "c"], [101, 102, 103]].zip(lambda x, y, z: (x, y, z)) == [
(1, "a", 101), (2, "b", 102), (3, "c", 103)]
"""
if len(lsts) == 0:
out = lambda f: []
elif len(lsts) == 1:
out = lambda f: [f(x) for x in lsts[0]]
else:
out = lambda f: [f(*args) for args in zip(*lsts)]
out.func_name = "[[...], [...], ...].zip"
out.__doc__ = zipper.__doc__
return out
# attach the methods force Python to notice
proxy_builtin(list)["size"] = property(sizer); hasattr([], "size")
proxy_builtin(list)["enumerate"] = property(enumerater); hasattr([], "enumerate")
proxy_builtin(list)["take"] = property(taker); hasattr([], "take")
proxy_builtin(list)["collect"] = property(collecter); hasattr([], "collect")
proxy_builtin(list)["lazy"] = property(lazyer); hasattr([], "lazy")
proxy_builtin(list)["map"] = property(mapper); hasattr([], "map")
proxy_builtin(list)["vmap"] = property(vmapper); hasattr([], "vmap")
proxy_builtin(list)["flatten"] = property(flattener); hasattr([], "flatten")
proxy_builtin(list)["flatmap"] = property(flatmapper); hasattr([], "flatmap")
proxy_builtin(list)["filter"] = property(filterer); hasattr([], "filter")
proxy_builtin(list)["reduce"] = property(reducer); hasattr([], "reduce")
proxy_builtin(list)["sum"] = property(sumer); hasattr([], "sum")
proxy_builtin(list)["aggregate"] = property(aggregator); hasattr([], "aggregate")
proxy_builtin(list)["reduceright"] = property(reducerright); hasattr([], "reduceright")
proxy_builtin(list)["aggregateright"] = property(aggregatorright); hasattr([], "aggregateright")
proxy_builtin(list)["pairs"] = property(pairser); hasattr([], "pairs")
proxy_builtin(list)["table"] = property(tabler); hasattr([], "table")
proxy_builtin(list)["zip"] = property(zipper); hasattr([], "zip")
proxy_builtin(tuple)["size"] = property(sizer); hasattr((), "size")
proxy_builtin(tuple)["enumerate"] = property(enumerater); hasattr((), "enumerate")
proxy_builtin(tuple)["take"] = property(taker); hasattr((), "take")
proxy_builtin(tuple)["collect"] = property(collecter); hasattr((), "collect")
proxy_builtin(tuple)["lazy"] = property(lazyer); hasattr((), "lazy")
proxy_builtin(tuple)["map"] = property(mapper); hasattr((), "map")
proxy_builtin(tuple)["vmap"] = property(vmapper); hasattr([], "vmap")
proxy_builtin(tuple)["flatten"] = property(flattener); hasattr((), "flatten")
proxy_builtin(tuple)["flatmap"] = property(flatmapper); hasattr((), "flatmap")
proxy_builtin(tuple)["filter"] = property(filterer); hasattr((), "filter")
proxy_builtin(tuple)["reduce"] = property(reducer); hasattr((), "reduce")
proxy_builtin(tuple)["sum"] = property(sumer); hasattr((), "sum")
proxy_builtin(tuple)["aggregate"] = property(aggregator); hasattr((), "aggregate")
proxy_builtin(tuple)["reduceright"] = property(reducerright); hasattr((), "reduceright")
proxy_builtin(tuple)["aggregateright"] = property(aggregatorright); hasattr((), "aggregateright")
proxy_builtin(tuple)["pairs"] = property(pairser); hasattr((), "pairs")
proxy_builtin(tuple)["table"] = property(tabler); hasattr((), "table")
proxy_builtin(tuple)["zip"] = property(zipper); hasattr((), "zip")
# also attach them to OAMap ListProxies
ListProxy.size = property(sizer)
ListProxy.enumerate = property(enumerater)
ListProxy.take = property(taker)
ListProxy.collect = property(collecter)
ListProxy.lazy = property(lazyer)
ListProxy.map = property(mapper)
ListProxy.flatten = property(flattener)
ListProxy.flatmap = property(flatmapper)
ListProxy.filter = property(filterer)
ListProxy.reduce = property(reducer)
ListProxy.sum = property(sumer)
ListProxy.aggregate = property(aggregator)
ListProxy.reduceright = property(reducerright)
ListProxy.aggregateright = property(aggregatorright)
ListProxy.pairs = property(pairser)
ListProxy.table = property(tabler)
ListProxy.zip = property(zipper)
# Verify that they all work (and provide examples of their use).
assert [1, 2, 3, 4, 5].take(3) == [1, 2, 3]
assert ["a", "b", "c", "d", "e"].enumerate == [(0, "a"), (1, "b"), (2, "c"), (3, "d"), (4, "e")]
assert [1, 2, 3, 4, 5].map(lambda x: 100 + x) == [101, 102, 103, 104, 105]
assert [[1, 2], [3, 4, 5]].flatten == [1, 2, 3, 4, 5]
assert [[1, 2], [3, [4, 5]]].flatten == [1, 2, 3, [4, 5]]
assert [1, 2, 3, 4, 5].map(lambda x: [x, x + 100]) == [[1, 101], [2, 102], [3, 103], [4, 104], [5, 105]]
assert [1, 2, 3, 4, 5].map(lambda x: [x, x + 100]).flatten == [1, 101, 2, 102, 3, 103, 4, 104, 5, 105]
assert [1, 2, 3, 4, 5].flatmap(lambda x: [x, x + 100]) == [1, 101, 2, 102, 3, 103, 4, 104, 5, 105]
assert [1, 2, 3, 4, 5].flatmap(lambda x: [100 + x] if x > 2 else []) == [103, 104, 105]
assert [1, 2, 3, 4, 5].filter(lambda x: x > 2) == [3, 4, 5]
assert [1, 2, 3, 4, 5].reduce(lambda x, y: x + y) == 15
assert [1, 2, 3, 4, 5].reduce(lambda x, y: [x, y]) == [[[[1, 2], 3], 4], 5]
assert ["a", "b", "c"].aggregate(lambda x, y: x + y, "") == "abc"
assert [1, 2, 3, 4, 5].aggregate(lambda x, y: [x, y], []) == [[[[[[], 1], 2], 3], 4], 5]
assert [1, 2, 3, 4, 5].reduceright(lambda x, y: [x, y]) == [1, [2, [3, [4, 5]]]]
assert [1, 2, 3, 4, 5].aggregateright(lambda x, y: [x, y], []) == [1, [2, [3, [4, [5, []]]]]]
assert [1, 2, 3, 4, 5].pairs(lambda x, y: [x, y]) == [[1, 2], [1, 3], [1, 4], [1, 5], [2, 3],
[2, 4], [2, 5], [3, 4], [3, 5], [4, 5]]
assert [[100, 200], [1, 2, 3]].table(lambda x, y: x + y) == [101, 102, 103, 201, 202, 203]
assert [[100, 200], [10, 20], [1, 2]].table(lambda x, y, z: x + y + z) == [111, 112, 121, 122, 211, 212, 221, 222]
assert [[1, 2, 3, 4, 5], ["a", "b"]].table(lambda x, y: [x, y]) == [
[1, "a"], [1, "b"], [2, "a"], [2, "b"], [3, "a"], [3, "b"], [4, "a"], [4, "b"], [5, "a"], [5, "b"]]
assert [1, 2, 3].pairs(lambda x, y: [x, y]) == [[1, 2], [1, 3], [2, 3]]
assert [[1, 2, 3], [1, 2, 3]].table(lambda x, y: [x, y]) == [[1, 1], [1, 2], [1, 3],
[2, 1], [2, 2], [2, 3],
[3, 1], [3, 2], [3, 3]]
assert [[1, 2, 3], ["a", "b", "c"], [101, 102, 103]].zip(lambda x, y, z: [x, y, z]) == [
[1, "a", 101], [2, "b", 102], [3, "c", 103]]
def example_generator():
yield None
example_generator = example_generator()
proxy_builtin(type(example_generator))["enumerate"] = property(enumerater); hasattr(example_generator, "enumerate")
proxy_builtin(type(example_generator))["take"] = property(taker); hasattr(example_generator, "take")
proxy_builtin(type(example_generator))["collect"] = property(collecter); hasattr(example_generator, "collect")
proxy_builtin(type(example_generator))["lazy"] = property(lazyer); hasattr(example_generator, "lazy")
proxy_builtin(type(example_generator))["map"] = property(mapper); hasattr(example_generator, "map")
proxy_builtin(type(example_generator))["flatten"] = property(flattener); hasattr(example_generator, "flatten")
proxy_builtin(type(example_generator))["flatmap"] = property(flatmapper); hasattr(example_generator, "flatmap")
proxy_builtin(type(example_generator))["filter"] = property(filterer); hasattr(example_generator, "filter")