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 e4d5010 commit 8d28f73Copy full SHA for 8d28f73
2 files changed
src/sequence.js
@@ -1439,6 +1439,29 @@ const zipLongest = curry('zipLongest', (seqs, fallback) => pipe(
1439
*/
1440
const zipLongest2 = curry('zipLongest2', (a, b, fallback) => zipLongest([a, b], fallback));
1441
1442
+/**
1443
+ * Inserts an element between every two elements of the given sequence.
1444
+ *
1445
+ * ```
1446
+ * const { intersperse } = require('ferrum');
1447
+ * assertSequenceEquals(
1448
+ * intersperse('ABC', '|'),
1449
+ * ['A', '|', 'B', '|', 'C']);
1450
1451
1452
+ * @function
1453
+ * @param {Any} what – The element to intersperse
1454
+ * @param {Sequence} a
1455
+ * @returns {Sequence}
1456
+ */
1457
+const intersperse = curry('intersperse', (seq, e) => pipe(
1458
+ seq,
1459
+ map(v => [e, v]),
1460
+ flat,
1461
+ trySkip(1)
1462
+));
1463
+
1464
1465
/**
1466
* Forms a sliding window on the underlying iterator.
1467
*
@@ -1812,6 +1835,7 @@ module.exports = {
1812
1835
zipLeast2,
1813
1836
zip2,
1814
1837
zipLongest2,
1838
+ intersperse,
1815
1839
slidingWindow,
1816
1840
trySlidingWindow,
1817
1841
lookahead,
test/sequence.test.js
@@ -26,7 +26,7 @@ const {
26
take, takeWhile, takeUntilVal, takeDef, flat, concat, prepend, append,
27
mapSort, zipLeast, zip, zipLongest, zipLeast2, zip2, zipLongest2,
28
slidingWindow, trySlidingWindow, lookahead, mod, union, union2,
29
- cartesian, cartesian2,
+ cartesian, cartesian2, intersperse,
30
} = require('../src/index');
31
const { ckEq, ckEqSeq, ckThrows } = require('./util');
32
@@ -525,3 +525,10 @@ it('union/union2', () => {
525
ckEq(m2.get('b'), 99);
526
ckEq(m2.get('x'), 13);
527
});
528
529
+it('intersperse', () => {
530
+ ckEqSeq(intersperse('', 'x'), '');
531
+ ckEqSeq(intersperse('a', 'x'), 'a');
532
+ ckEqSeq(intersperse('ab', 'x'), 'axb');
533
+ ckEqSeq(intersperse('abc', 'x'), 'axbxc');
534
+});
0 commit comments