Skip to content

Commit

Permalink
Merge 015666a into ab176ab
Browse files Browse the repository at this point in the history
  • Loading branch information
matyama committed Sep 29, 2019
2 parents ab176ab + 015666a commit 70fc12b
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions ftoolz/adt/ring.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
from collections import deque
from typing import Iterable, Iterator, Sized, TypeVar

from cytoolz.functoolz import do

from ftoolz.typing import Seq

_E = TypeVar('_E')


class Ring(Iterator[_E], Sized): # pylint: disable=E0239
"""Fixed-size in-memory rotating list of elements"""

# pylint: disable=W0231
def __init__(self, elements: Iterable[_E] = tuple()) -> None:
self._ring = deque(elements)

def __len__(self) -> int:
return len(self._ring)

def __bool__(self) -> bool:
return bool(self._ring)

def __next__(self) -> _E:
if not self:
raise StopIteration
return do(self._ring.append, self._ring.popleft())

def state(self) -> Seq[_E]:
return tuple(self._ring)

0 comments on commit 70fc12b

Please sign in to comment.