Skip to content

Commit 45ce0c7

Browse files
committed
iterateWhile
1 parent c6f27a6 commit 45ce0c7

File tree

6 files changed

+49
-4
lines changed

6 files changed

+49
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## Version 4.3.0
2+
3+
- `Seq.iterateWhile` generates a sequence until the interation function returns `None`.
4+
15
## Version 4.2.0
26

37
- `Seq.at` returns the item at a specified zero-based index.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ let fromArray: (~start: int=?, ~end: int=?, array<'a>) => t<'a>
6262
let fromList: list<'a> => t<'a>
6363
let init: (int, int => 'a) => t<'a>
6464
let iterate: ('a, 'a => 'a) => t<'a>
65+
let iterateWhile: ('a, 'a => option<'a>) => t<'a>
6566
let once: 'a => t<'a>
6667
let onceWith: (unit => 'a) => t<'a>
6768
let permutations: (t<'a>, int) => t<(int, t<'a>)>

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@jmagaram/rescript-seq",
3-
"version": "4.2.0",
3+
"version": "4.3.0",
44
"module": "",
55
"description": "Lazy sequences for ReScript.",
66
"keywords": [

src/Seq.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -943,3 +943,12 @@ let at = (xx, inx) => {
943943
}
944944
go(xx, 0)
945945
}
946+
947+
let iterateWhile = (seed, f) => () => {
948+
let rec go = seed => () =>
949+
switch f(seed) {
950+
| None => End
951+
| Some(seed) => Next(seed, go(seed))
952+
}
953+
Next(seed, go(seed))
954+
}

src/Seq.resi

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,17 @@ let forever: 'a => t<'a>
7070
let foreverWith: (unit => 'a) => t<'a>
7171

7272
/**
73-
`iterate(seed, f)` creates an infinite sequence starting with `seed` and
74-
repeatedly calling function `f` on it.
73+
`iterate(seed, f)` creates an infinite sequence that starts with `seed`.
74+
Subsequent items are generated by calling `f` on the previous item.
7575
*/
7676
let iterate: ('a, 'a => 'a) => t<'a>
7777

78+
/**
79+
`iterate(seed, f)` creates a sequence that starts with `seed`. Subsequent items
80+
are generated by calling `f` on the previous item until `None`.
81+
*/
82+
let iterateWhile: ('a, 'a => option<'a>) => t<'a>
83+
7884
/**
7985
`cycle` is the sequence that consists of an infinite number of repetitions of
8086
the input sequence. If the original sequence is empty, the resulting sequence

tests/Seq__SeqTests.res

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -624,12 +624,36 @@ let iterateTests = makeSeqEqualsTests(
624624
)->Array.concat([
625625
willNotThrow(~title="iterate", ~expectation="lazy", () =>
626626
S.iterate(0, i => {
627-
Exn.raiseError("Boom!")->raise->ignore
627+
Exn.raiseError("Boom!")->ignore
628628
i
629629
})
630630
),
631631
])
632632

633+
let iterateWhileTests = {
634+
let increment = (max, seed) => {
635+
let next = seed + 1
636+
next <= max ? Some(next) : None
637+
}
638+
makeSeqEqualsTests(
639+
~title="iterateWhile",
640+
[
641+
(S.iterateWhile(4, increment(10, _)), [4, 5, 6, 7, 8, 9, 10], ""),
642+
(S.iterateWhile(9, increment(10, _)), [9, 10], ""),
643+
(S.iterateWhile(10, increment(10, _)), [10], ""),
644+
(S.iterateWhile(999, increment(10, _)), [999], "Always includes seed"),
645+
(S.iterateWhile(1, increment(1_000_000, _))->Seq.drop(999_998), [999_999, 1_000_000], ""),
646+
],
647+
)->Array.concat([
648+
willNotThrow(~title="iterateWhile", ~expectation="lazy", () =>
649+
S.iterateWhile(0, i => {
650+
Exn.raiseError("Boom!")->ignore
651+
Some(i)
652+
})
653+
),
654+
])
655+
}
656+
633657
let fromArrayTests = {
634658
let basicTests = makeSeqEqualsTests(
635659
~title="fromArray",
@@ -2230,6 +2254,7 @@ let tests =
22302254
isEmptyTests,
22312255
isSortedByTests,
22322256
iterateTests,
2257+
iterateWhileTests,
22332258
joinTests,
22342259
lastTests,
22352260
lengthTests,

0 commit comments

Comments
 (0)