-
Notifications
You must be signed in to change notification settings - Fork 0
/
usage.py
98 lines (80 loc) · 2.79 KB
/
usage.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
from __future__ import print_function
from funciter import Sequence
from funciter import IteratorAlreadyStarted
# since these are `iterables` and NOT iterators,
# we can reuse our constructer iterator
A = range(11)
B = "cats like catnip"
# ------------------------------------------------------------------------------
# 1. use it like a normal iterator
# ------------------------------------------------------------------------------
for i in Sequence(range(5)):
print(i)
# ------------------------------------------------------------------------------
# 2. Filter certain elements
# ------------------------------------------------------------------------------
cats_only = Sequence(B).filter(lambda x: x in 'cat ')
print(''.join(cats_only))
# it's reusable because it's backed by an iteraBLE
print("it works again! again!")
for x in cats_only:
print(x)
# ------------------------------------------------------------------------------
# 3. Skip the even elements
# ------------------------------------------------------------------------------
Ai = Sequence(A)[1::2]
for i in Ai:
print(i)
# again, since it's backed by a iterable, it's reusable
for i in Ai:
print(i)
# ------------------------------------------------------------------------------
# 4. Apply another operation to the sequence
# ------------------------------------------------------------------------------
print("skip the first odd element (1)")
Qi = Ai[1:]
for i in Qi:
print(i)
# ------------------------------------------------------------------------------
# 5. what about a depletable source?
# ------------------------------------------------------------------------------
# x is now a sequence backed by an iteraTOR
x = Sequence(_ for _ in range(10))
# Either s or e can be used, but not both, since
# they are backed by the same iteraTOR
s = x[::2]
e = x[::3]
# Consume the first element of `s`
for i in s:
print(i)
break
# Let's try using `e`
try:
for i in e:
print(i)
except IteratorAlreadyStarted as e:
print(e)
print("yup, started iterators can't be reused!")
else:
raise RuntimeError('Expected an exception to be raised, but none was!')
# ------------------------------------------------------------------------------
# 6. Another iteraTOR example
# ------------------------------------------------------------------------------
x = Sequence(_ for _ in range(10))
e = x[::3]
ei = iter(e)
print("HEAD", e.head())
# Cannot start again!
try:
x[::2].filter(lambda x: x > 0)[0]
except IteratorAlreadyStarted as e:
print(e)
else:
raise RuntimeError('Expected an exception to be raised, but none was!')
try:
for i in ei:
print(i)
except IteratorAlreadyStarted as e:
print(e)
else:
raise RuntimeError('Expected an exception to be raised, but none was!')