@@ -915,7 +915,7 @@ const into = curry('into', (seq, t) => Into.invoke(t, seq));
915
915
* ```
916
916
* # Remove odd numbers from a set
917
917
* const st = new Set([1,1,2,2,3,4,5]);
918
- * into(filter(st, n => n % 2 == 0), Set) # Set(2,4)
918
+ * into(filter(st, n => n % 2 === 0), Set) # Set(2,4)
919
919
*
920
920
* # Remove a key/value pair from an object
921
921
* const obj = {foo: 42, bar: 5};
@@ -1053,7 +1053,7 @@ const map = curry('map', function* map(seq, fn) {
1053
1053
* Remove values from the sequence based on the given condition.
1054
1054
*
1055
1055
* ```
1056
- * filter(range(0,10), x => x%2 == 0) // [2,4,6,8]
1056
+ * filter(range(0,10), x => x%2 === 0) // [2,4,6,8]
1057
1057
* ```
1058
1058
*
1059
1059
* @function
@@ -1448,7 +1448,7 @@ const zipLongest2 = curry('zipLongest2', (a, b, fallback) => zipLongest([a, b],
1448
1448
* Will throw IteratorEnded if the sequence is shorter than
1449
1449
* the given window.
1450
1450
*
1451
- * Returns an empty sequence if `no == 0`.
1451
+ * Returns an empty sequence if `no === 0`.
1452
1452
*
1453
1453
* @function
1454
1454
* @param {Sequence } seq A sequence of sequences
@@ -1483,7 +1483,7 @@ const slidingWindow = curry('slidingWindow', (seq, no) => {
1483
1483
* Like slidingWindow, but returns an empty sequence if the given
1484
1484
* sequence is too short.
1485
1485
*
1486
- * Returns an empty sequence if `no == 0`.
1486
+ * Returns an empty sequence if `no === 0`.
1487
1487
*
1488
1488
* @function
1489
1489
* @param {Sequence } seq A sequence of sequences
@@ -1529,6 +1529,8 @@ const trySlidingWindow = curry('trySlidingWindow', function* trySlidingWindow(se
1529
1529
* Try sliding window would yield an empty array in each of the examples
1530
1530
* above.
1531
1531
*
1532
+ * Returns an empty sequence if `no === 0`.
1533
+ *
1532
1534
* @function
1533
1535
* @param {Sequence } seq
1534
1536
* @param {Number } no Number of elements to look ahead to.
@@ -1540,6 +1542,91 @@ const lookahead = curry('lookahead', (seq, no, filler) => {
1540
1542
return trySlidingWindow ( filled , no + 1 ) ;
1541
1543
} ) ;
1542
1544
1545
+ /**
1546
+ * Split the given input sequence into chunks of a specific length.
1547
+ * The last chunk may be shorter than the given chunk size if the input
1548
+ * sequence is not long enough.
1549
+ *
1550
+ * ```
1551
+ * const { list, chunkifyShort } = require('ferrum');
1552
+ * list(chunkifyShort([1,2,3,4,5], 2)); // => [[1,2], [3,4], [5]]
1553
+ * ```
1554
+ *
1555
+ * Returns an empty sequence if `no === 0`.
1556
+ *
1557
+ * @function
1558
+ * @param {Sequence } seq A sequence of sequences
1559
+ * @param {Number } len The length of the chunk
1560
+ * @returns {Iterator } Sequence of lists
1561
+ */
1562
+ const chunkifyShort = curry ( 'chunkifyShort' , ( seq , len ) => {
1563
+ if ( len === 0 ) {
1564
+ return iter ( [ ] ) ;
1565
+ }
1566
+
1567
+ const it = iter ( seq ) ;
1568
+ return pipe (
1569
+ repeatFn ( ( ) => takeShort ( it , len ) ) ,
1570
+ takeWhile ( chunk => ! empty ( chunk ) ) ,
1571
+ ) ;
1572
+ } ) ;
1573
+
1574
+ /**
1575
+ * Split the given input sequence into chunks of a specific length.
1576
+ * If the length of the sequence is not divisible by the chunk length
1577
+ * IteratorEnded will be thrown.
1578
+ *
1579
+ * ```
1580
+ * const { list, chunkify } = require('ferrum');
1581
+ * list(chunkify([1,2,3,4], 2)); // => [[1,2], [3,4]]
1582
+ * ```
1583
+ *
1584
+ * Returns an empty sequence if `no === 0`.
1585
+ *
1586
+ * @function
1587
+ * @param {Sequence } seq A sequence of sequences
1588
+ * @param {Number } len The length of the chunk
1589
+ * @throws {IteratorEnded }
1590
+ * @returns {Iterator } Sequence of lists
1591
+ */
1592
+ const chunkify = curry ( 'chunkify' , ( seq , len ) => pipe (
1593
+ chunkifyShort ( seq , len ) ,
1594
+ map ( ( chunk ) => {
1595
+ if ( size ( chunk ) === len ) {
1596
+ return chunk ;
1597
+ } else {
1598
+ throw new IteratorEnded ( 'chunkify() needs sequences of the correct length!' ) ;
1599
+ }
1600
+ } ) ,
1601
+ ) ) ;
1602
+
1603
+ /**
1604
+ * Split the given input sequence into chunks of a specific length.
1605
+ * If the input sequence is not long enough, the last chunk will be filled
1606
+ * with the given fallback value.
1607
+ *
1608
+ * ```
1609
+ * const { list, chunkifyWithFallback } = require('ferrum');
1610
+ * list(chunkifyWithFallback([1,2,3,4,5], 2), 99); // => [[1,2], [3,4], [5, 99]]
1611
+ * ```
1612
+ *
1613
+ * @function
1614
+ * @param {Sequence } seq A sequence of sequences
1615
+ * @param {Number } len The length of the chunk
1616
+ * @param {Any } fallback The value to use if the input sequence is too short.
1617
+ * @returns {Iterator } Sequence of lists
1618
+ */
1619
+ const chunkifyWithFallback = curry ( 'chunkifyWithFallback' , ( seq , len , fallback ) => pipe (
1620
+ chunkifyShort ( seq , len ) ,
1621
+ map ( ( chunk ) => {
1622
+ if ( size ( chunk ) === len ) {
1623
+ return chunk ;
1624
+ } else {
1625
+ return take ( concat ( chunk , repeat ( fallback ) ) , len ) ;
1626
+ }
1627
+ } ) ,
1628
+ ) ) ;
1629
+
1543
1630
/**
1544
1631
* Calculate the cartesian product of the given sequences.
1545
1632
*
@@ -1728,6 +1815,9 @@ module.exports = {
1728
1815
slidingWindow,
1729
1816
trySlidingWindow,
1730
1817
lookahead,
1818
+ chunkifyShort,
1819
+ chunkify,
1820
+ chunkifyWithFallback,
1731
1821
cartesian,
1732
1822
cartesian2,
1733
1823
mod,
0 commit comments