We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 67e8182 commit 81de232Copy full SHA for 81de232
2 files changed
src/sequence.js
@@ -215,13 +215,30 @@ function* range(start, end) {
215
*/
216
const range0 = b => range(0, b);
217
218
-/** Generates an infinite iterator of the given value. */
219
-function* repeat(val) {
+/**
+ * Generates an infinite iterator by invoking the
220
+ * given function repeatedly.
221
+ *
222
+ * @function
223
+ * @param {Function} fn
224
+ * @returns {Sequence}
225
+ */
226
+function* repeatFn(fn) {
227
while (true) {
- yield val;
228
+ yield fn();
229
}
230
231
232
233
+ * Generates an infinite iterator of the given value.
234
235
236
+ * @template T
237
+ * @param {T} val
238
+ * @returns {Sequence<T>}
239
240
+const repeat = val => repeatFn(() => val);
241
+
242
/**
243
* Generate a sequence by repeatedly calling the same function on the
244
* previous value.
@@ -1616,6 +1633,7 @@ module.exports = {
1616
1633
iter,
1617
1634
range,
1618
1635
range0,
1636
+ repeatFn,
1619
1637
repeat,
1620
1638
extend,
1621
1639
extend1,
test/sequence.test.js
@@ -17,7 +17,7 @@ const assert = require('assert');
17
const {
18
and, plus, or, mul, not,
19
size, TraitNotImplemented, _typedArrays,
20
- iter, range, range0, repeat, extend, extend1, flattenTree,
+ iter, range, range0, repeat, repeatFn, extend, extend1, flattenTree,
21
IteratorEnded, next, tryNext, nth, first, second, last, tryNth, tryFirst,
22
trySecond, tryLast, seqEq, each, find, tryFind, contains, count, list,
23
uniq, join, dict, obj, into, foldl, foldr, any, all, sum, product, map,
@@ -111,6 +111,13 @@ it('range(), range0()', () => {
111
112
it('repeat()', () => {
113
ckEqSeq(tryTake(repeat(2), 4), [2, 2, 2, 2]);
114
115
+ let x = 0;
116
+ const fn = () => {
117
+ x += 1;
118
+ return x;
119
+ };
120
+ ckEqSeq(tryTake(repeatFn(fn), 4), [1, 2, 3, 4]);
121
});
122
123
it('extend(), extend1()', () => {
0 commit comments