-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpyhuman.py
221 lines (151 loc) · 5.49 KB
/
pyhuman.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
from __future__ import annotations
from functools import partial
class Infix(object):
def __init__(self, func):
self.func = func
def __or__(self, other):
return self.func(other)
def __ror__(self, other):
return Infix(partial(self.func, other))
def __call__(self, v1, v2):
return self.func(v1, v2)
instanceof = Infix(isinstance)
curry = Infix(partial)
add = Infix(lambda a, b: a + b)
sub = Infix(lambda a, b: a - b)
mul = Infix(lambda a, b: a * b)
div = Infix(lambda a, b: a / b)
from typing import TypeVar, Iterable, Callable, Dict, List, Tuple, Set
S = TypeVar('S') # Declare type variable
T = TypeVar('T') # Declare type variable
K = TypeVar('K') # Declare type variable
V = TypeVar('V') # Declare type variable
class MyIterable(Iterable[T]):
def map(self: MyIterable[T], f: Callable[[T], S]) -> MyList[S]:
return MyList([f(e) for e in self])
def zip(self: MyIterable[T], another: Iterable[S]) -> MyList[Tuple[T, S]]:
return MyList(zip(self, another))
def zip3(self: MyIterable[T], u: Iterable[S], v: Iterable[V]) -> MyList[Tuple[T, S, V]]:
return MyList(zip(self, u, v))
def zipMany(self: MyIterable[T], *others: Iterable) -> MyList[Tuple]:
return MyList(zip(self, *others))
def fold(self: MyIterable[T], init: S, f: Callable[[S, T], S]) -> S:
acc = init
for e in self: acc = f(acc, e)
return acc
def forEach(self: MyIterable[T], f: Callable[[T], None]) -> None:
for e in self:
f(e)
def toList(self: MyIterable[T]) -> MyList[T]:
return MyList(self)
def toSet(self: MyIterable[T]) -> MySet[T]:
return MySet(self)
def toMap(self: MyIterable[Tuple[K, V]]) -> MyMap[K, V]:
return MyMap(self)
def find(self: MyIterable[T], f: Callable[[T], bool]) -> T:
for e in self:
if f(e): return e
return None
def rfind(self: MyIterable[T], f: Callable[[T], bool]) -> T:
return MyList(reversed(self)).find(f)
def forAll(self: MyIterable[T], f: Callable[[T], bool]) -> bool:
return self.fold(True, lambda acc, e: acc and f(e))
def contains(self: MyIterable[T], f: Callable[[T], bool]) -> bool:
return self.find(f) is not None
def take(self: MyIterable[T], n: int) -> MyList[T]:
return MyList(self)[:n]
def takeWhile(self: MyIterable[T], f: Callable[[T], bool]) -> MyList[T]:
l = MyList()
for e in self:
if f(e):
l.append(e)
else:
break
return l
def drop(self: MyIterable[T], n: int) -> MyList[T]:
return MyList(self)[n:]
def dropWhile(self: MyIterable[T], f: Callable[[T], bool]) -> MyList[T]:
l = MyList()
met = False
for e in self:
if f(e) and not met:
continue
met = True
l.append(e)
return l
def groupBy(self: MyIterable[T], f: Callable[[T], K]) -> MyMap[K, MyList[T]]:
m = MyMap()
for e in self:
k = f(e)
if k not in m: m[k] = MyList()
m[k].append(e)
return m
def flatten(self: MyIterable[Iterable[T]]) -> MyList[T]:
return self.fold(MyList(), lambda acc, e: acc + e)
def reversed(self: MyIterable[Iterable[T]]):
return reversed(self)
def sortBy(self: MyIterable[Iterable[T]], c: Callable[[T, T], int]) -> MyList[T]:
return sorted(self, key=c).toList()
def size(self: MyIterable[Iterable[T]]) -> int:
return len(self)
class MyList(List[T], MyIterable[T]):
def filter(self: List[T], f: Callable[[T], bool]) -> MyList[T]:
return MyList([e for e in self if f(e)])
def indexOf(self: MyIterable[T], f: Callable[[T], bool]) -> int:
index = -1
for e in self:
index += 1
if f(e): return index
return -1
def rindexOf(self: MyIterable[T], f: Callable[[T], bool]) -> int:
index = -1
for e in reversed(self):
index += 1
if f(e): return index
return -1
class MyRange(MyList):
def __init__(self, *t):
MyList.__init__(list(range(*t)))
def listOf(*args: T) -> MyList[T]:
return MyList[T](args)
def toList(self: Iterable[T]) -> MyList[T]:
if self | instanceof | dict:
self = self.items()
return MyList(self)
class MySet(Set[T], MyIterable[T]):
def filter(self: Set[T], f: Callable[[T], bool]) -> MySet[T]:
return MySet([e for e in self if f(e)])
def __add__(self: Set[T], other: Set[T]) -> Set[T]:
return self.union(other)
def setOf(*args: T) -> MySet[T]:
return MySet[T](args)
def toSet(self: Iterable[T]) -> MySet[T]:
if self | instanceof | dict:
self = self.items()
return MySet(self)
class MyMap(Dict[K, V]):
def map(self: MyMap[K, V], f: Callable[[K, V], S]) -> MyList[S]:
return MyList([f(k, v) for k, v in self.items()])
def zip(self, another) -> MyList:
return MyList(zip(self.items(), another.items() if another | instanceof | Dict else another))
def fold(self: MyMap[K, V], init: S, f: Callable[[S, K, V], S]) -> S:
acc = init
for k, v in self.items(): acc = f(acc, k, v)
return acc
def forEach(self: MyMap[K, V], f: Callable[[K, V], None]) -> None:
for k, v in self:
f(k, v)
def toList(self: MyMap[K, V]) -> MyList[K, V]:
return MyList(self.items())
def toSet(self: MyMap[K, V]) -> MySet[K, V]:
return MySet(self.items())
def filter(self: MyMap[K, V], f: Callable[[K, V], bool]) -> MyMap[K, V]:
return MyMap([(k, v) for k, v in self if f(k, v)])
def mapOf(*args: Tuple[K, V]) -> MyMap[K, V]:
return MyMap[K, V](args)
def toMap(self: Iterable[Iterable[K, V]]) -> MyMap[K, V]:
return MyMap(self)
from forbiddenfruit import curse
for t in (list, tuple, set, dict, map):
for f in (toList, toSet, toMap):
curse(t, f.__name__, f)