Skip to content

Commit ed50c6d

Browse files
committed
functional sequence op
1 parent de2bf5b commit ed50c6d

File tree

2 files changed

+26
-0
lines changed

2 files changed

+26
-0
lines changed

functional_program/functional_sequence_operate/__init__.py

Whitespace-only changes.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
from typing import Iterator, Any, Tuple
2+
3+
Item_Iter = Iterator[Any]
4+
Pairs_Iter = Iterator[Tuple[float, float]]
5+
6+
7+
def pairs(iterator: Item_Iter) -> Pairs_Iter:
8+
def pairs_from(head: Any, iterable_tail: Item_Iter) -> Pairs_Iter:
9+
nxt = next(iterable_tail)
10+
yield head, nxt
11+
yield from pairs_from(nxt, iterable_tail)
12+
13+
try:
14+
return pairs_from(next(iterator), iterator)
15+
except StopIteration:
16+
return iter([])
17+
18+
19+
if __name__ == '__main__':
20+
l = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7]
21+
# l = [0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7] * 1000 # maximum recursion depth.
22+
for item in pairs(iter(l)):
23+
print(item)
24+
25+
26+

0 commit comments

Comments
 (0)