Skip to content
Permalink
Browse files

feat: Add function repeatFn()

  • Loading branch information...
koraa committed Aug 27, 2019
1 parent 67e8182 commit 81de232a579410dee253542e6de5dc15ec8ae164
Showing with 29 additions and 4 deletions.
  1. +21 −3 src/sequence.js
  2. +8 −1 test/sequence.test.js
@@ -215,13 +215,30 @@ function* range(start, end) {
*/
const range0 = b => range(0, b);

/** Generates an infinite iterator of the given value. */
function* repeat(val) {
/**
* Generates an infinite iterator by invoking the
* given function repeatedly.
*
* @function
* @param {Function} fn
* @returns {Sequence}
*/
function* repeatFn(fn) {
while (true) {
yield val;
yield fn();
}
}

/**
* Generates an infinite iterator of the given value.
*
* @function
* @template T
* @param {T} val
* @returns {Sequence<T>}
*/
const repeat = val => repeatFn(() => val);

/**
* Generate a sequence by repeatedly calling the same function on the
* previous value.
@@ -1616,6 +1633,7 @@ module.exports = {
iter,
range,
range0,
repeatFn,
repeat,
extend,
extend1,
@@ -17,7 +17,7 @@ const assert = require('assert');
const {
and, plus, or, mul, not,
size, TraitNotImplemented, _typedArrays,
iter, range, range0, repeat, extend, extend1, flattenTree,
iter, range, range0, repeat, repeatFn, extend, extend1, flattenTree,
IteratorEnded, next, tryNext, nth, first, second, last, tryNth, tryFirst,
trySecond, tryLast, seqEq, each, find, tryFind, contains, count, list,
uniq, join, dict, obj, into, foldl, foldr, any, all, sum, product, map,
@@ -111,6 +111,13 @@ it('range(), range0()', () => {

it('repeat()', () => {
ckEqSeq(tryTake(repeat(2), 4), [2, 2, 2, 2]);

let x = 0;
const fn = () => {
x += 1;
return x;
};
ckEqSeq(tryTake(repeatFn(fn), 4), [1, 2, 3, 4]);
});

it('extend(), extend1()', () => {

0 comments on commit 81de232

Please sign in to comment.
You can’t perform that action at this time.