aboutsummaryrefslogtreecommitdiff
path: root/compilador/nanoiter.py
blob: 928522f39e43ddcd750c8e1be9910c6052bc4f54 (plain)
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
from typing import Optional, NoReturn, List, TypeVar

T = TypeVar('T')

class NanoIter:
    def __init__(self, l: List[T]):
        self.i = -1
        self.l: List[T] = l

    def next(self) -> T:
        if self.i == len(self.l):
            raise StopIteration()
        else:
            self.i += 1
            return self.l[self.i]

    def get(self, i = None):
        return self.l[self.i] if not i else self.l[i]

    def back(self) -> NoReturn:
        if self.i <= 0:
            self.i = -1
        else:
            self.i -= 1

    def peek(self) -> T:
        if self.i == len(self.l):
            raise StopIteration()
        else:
            return self.l[self.i + 1]