Skip to content

Commit 9128f91

Browse files
committed
niche
1 parent 3449063 commit 9128f91

File tree

8 files changed

+560
-19
lines changed

8 files changed

+560
-19
lines changed

docs/core.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,12 @@
88

99
***
1010

11+
::: eaterate.call_for_next
12+
13+
***
14+
15+
::: eaterate.CallForNext
16+
17+
***
18+
1119
::: eaterate.Eaterator

docs/custom.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# Custom Eaterator
2+
3+
An `Eaterator` mainly consists of two things:
4+
5+
- `T`: The type of each element.
6+
- `next()`: The function that advances the iterator.
7+
8+
Unlike Python's built-in iterator ecosystem, `eaterate` takes [the Rust approach](https://doc.rust-lang.org/std/iter/trait.Iterator.html), which eliminates the need for raising a `StopIteration` exception everytime an iterator ends.
9+
10+
Every `next()` function override should return an [`Option[T]`](./utilities.md#eaterate.Option), which has two variants, either `Some(...)` (there's data) or `Option.none()` (there's no data, in otherwords, the iterator ended).
11+
12+
Let's say you want to create an iterator that says "chocolate" out loud for `n` times.
13+
14+
```python
15+
from eaterate import Eaterator, Option
16+
17+
18+
class ChocolateEaterator(Eaterator[str]):
19+
n: int
20+
21+
def __init__(self, n: int):
22+
self.n = n
23+
24+
def next(self) -> Option[str]:
25+
if self.n <= 0:
26+
return Option.none()
27+
else:
28+
self.n -= 1
29+
return Option.some("chocolate")
30+
31+
# Say, 3 chocolates!
32+
chocolates = ChocolateEaterator(n=3)
33+
34+
print(chocolates.next()) # Some("chocolate")
35+
print(chocolates.next()) # Some("chocolate")
36+
print(chocolates.next()) # Some("chocolate")
37+
print(chocolates.next()) # Option.none()
38+
```
39+
40+
You can also use a `for` block:
41+
42+
```python
43+
for item in chocolates:
44+
print(item)
45+
46+
# chocolate
47+
# chocolate
48+
# chocolate
49+
```
50+
51+
You can check out the [Core API](./core.md) to see if there's anything cool you'd like to implement. Then, submit your [Pull Request](https://github.com/AWeirdDev/eaterate/pulls) (if you'd like)!
52+
53+
!!! note
54+
55+
You can also achieve this without writing custom `Eaterator`:
56+
57+
```python
58+
from eaterate import repeat
59+
60+
chocolates = repeat("chocolate", 3)
61+
```

docs/index.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ Eaterate *(eet-uh-rate)* is a toolkit of modern Python iterators. Or whatever th
55
**Key features**:
66

77
- **Fully typed**. C'mon, type annotations are just essential these days.
8+
- **Quick protyping**. Just chain everything.
9+
- **Flexible**. Build your custom iterators.
810

911
```python
1012
from eaterate import eater

eaterate/__init__.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,15 @@
1-
from .core import Eaterator, eater
1+
from .core import CallForNext, Eaterator, eater, call_for_next
22
from .range import ERange, erange
33
from .option import Option
4+
from .repeating import repeat
45

5-
__all__ = ["Eaterator", "eater", "Option", "ERange", "erange"]
6+
__all__ = [
7+
"CallForNext",
8+
"Eaterator",
9+
"eater",
10+
"Option",
11+
"ERange",
12+
"erange",
13+
"repeat",
14+
"call_for_next",
15+
]

0 commit comments

Comments
 (0)