Skip to content

Commit 5fb9101

Browse files
committed
add new adapters
1 parent 33106e2 commit 5fb9101

File tree

3 files changed

+29
-3
lines changed

3 files changed

+29
-3
lines changed

readme.md

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -230,6 +230,8 @@ iterrr "hello".items: # or "hello".items.iterrr:
230230
- `window`
231231
- `cycle`
232232
- `flatten`
233+
- `drop`
234+
- `take`
233235

234236
**Usage**:
235237
example:
@@ -245,7 +247,7 @@ matrix.items |> flatten().map(-it).cycle(11).group(4).toseq()
245247
```
246248
result:
247249
```nim
248-
>> @[
250+
@[
249251
@[-1, -2, -3, -4],
250252
@[-5, -6, -7, -8],
251253
@[-9, -1, -2]
@@ -304,8 +306,11 @@ you can send your donation to my [crypo wallets](https://github.com/hamidb80/ham
304306
improve existing code.
305307

306308
## Change Logs
307-
### `0.4.2` -> `0.4.3`:
308-
- add `window` adapter
309+
### `0.4.2` -> `0.4.4`:
310+
- add new adapters:
311+
- `window`
312+
- `take`
313+
- `drop`
309314

310315
### `0.4.1` -> `0.4.2`:
311316
- optimize

src/iterrr/adapters.nim

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,21 @@ iterator cycle*(loopItems: T; `limit`: int): T {.adapter.} =
8181
if `c` == `limit`:
8282
break cycleLoop
8383

84+
iterator drop*(loopItems: T; `limit`: int): T {.adapter.} =
85+
var `c` = 0
86+
for it in loopItems:
87+
inc `c`
88+
if `c` > `limit`:
89+
yield it
90+
91+
iterator take*(loopItems: T; `limit`: int): T {.adapter.} =
92+
var `c` = 0
93+
for it in loopItems:
94+
if `c` == `limit`: break
95+
else: yield it
96+
inc `c`
97+
98+
8499
iterator flatten*(loopItems: T): typeof loopItems[0] {.adapter.} =
85100
for it in loopItems:
86101
for it in it:

tests/test.nim

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,6 +204,12 @@ suite "adapters":
204204
test "cycle":
205205
check (1..3) |> cycle(7).toseq() == @[1, 2, 3, 1, 2, 3, 1]
206206

207+
test "drop":
208+
check (1..7) |> drop(3).toseq() == @[4, 5, 6, 7]
209+
210+
test "take":
211+
check (1..7) |> take(3).toseq() == @[1, 2, 3]
212+
207213
test "flatten":
208214
check matrix.items |> flatten().toseq() == toseq(1..9)
209215

0 commit comments

Comments
 (0)