-
Notifications
You must be signed in to change notification settings - Fork 17
Expand file tree
/
Copy pathfunctional.py
More file actions
61 lines (43 loc) · 1.12 KB
/
Copy pathfunctional.py
File metadata and controls
61 lines (43 loc) · 1.12 KB
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
"""
functional
----------
Functional programming utilities.
"""
from collections import deque
from operator import itemgetter
from .compat import viewkeys
def complement(f):
def not_f(*args, **kwargs):
return not f(*args, **kwargs)
return not_f
def keyfilter(f, d):
return {k: v for k, v in d.items() if f(k)}
def keysorted(d):
return sorted(d.items(), key=itemgetter(0))
def valfilter(f, d):
return {k: v for k, v in d.items() if f(v)}
def dzip(left, right):
return {k: (left.get(k), right.get(k)) for k in viewkeys(left) & viewkeys(right)}
def sliding_window(iterable, n):
it = iter(iterable)
items = deque(maxlen=n)
try:
for i in range(n):
items.append(next(it))
except StopIteration:
return
yield tuple(items)
for item in it:
items.append(item)
yield tuple(items)
def merge(dicts):
dicts = list(dicts)
if len(dicts) == 0:
return {}
elif len(dicts) == 1:
return dicts[0].copy()
else:
out = dicts[0].copy()
for other in dicts[1:]:
out.update(other)
return out