forked from dunossauro/live-de-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmytuple.py
More file actions
84 lines (57 loc) · 1.87 KB
/
Copy pathmytuple.py
File metadata and controls
84 lines (57 loc) · 1.87 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
from collections.abc import Sequence
class MyTuple(Sequence):
def __init__(self, tuple_):
self._d = tuple(tuple_)
def __getitem__(self, pos):
"""Outra é usando __iter__ e __next__."""
return self._d[pos]
def __len__(self):
return len(self._d)
def __repr__(self):
return '{}'.format(self._d)
class TupleSet(Sequence):
def __init__(self, tuple_, ordered=False):
tup = []
for value in tuple_:
if value not in tup:
tup.append(value)
self._d = tuple(tup) if not ordered else tuple(sorted(tup))
def __getitem__(self, pos):
"""Outra é usando __iter__ e __next__."""
return self._d[pos]
def __len__(self):
return len(self._d)
def __repr__(self):
return '{}'.format(self._d)
class TupleFlat(Sequence):
def __init__(self, tuple_):
self._d = tuple(sum(tuple_, []))
def __getitem__(self, pos):
"""Outra é usando __iter__ e __next__."""
return self._d[pos]
def __len__(self):
return len(self._d)
def __repr__(self):
return '{}'.format(self._d)
class TupleFlatSize(Sequence):
def __init__(self, tuple_):
self._d = tuple(tuple_)
def __getitem__(self, pos):
"""Outra é usando __iter__ e __next__."""
return self._d[pos]
def __len__(self):
return len(tuple(sum(self._d, [])))
def __repr__(self):
return '{}'.format(self._d)
class TupleFunctor(Sequence):
def __init__(self, tuple_):
self._d = tuple(tuple_)
def __getitem__(self, pos):
"""Outra é usando __iter__ e __next__."""
return self._d[pos]
def __len__(self):
return len(self._d)
def __repr__(self):
return '{}'.format(self._d)
def fmap(self, function):
return TupleFunctor(function(x) for x in self._d)