1 | var _curry2 =
|
---|
2 | /*#__PURE__*/
|
---|
3 | require("./internal/_curry2.js");
|
---|
4 | /**
|
---|
5 | * Takes a list and returns a list of lists where each sublist's elements are
|
---|
6 | * all satisfied pairwise comparison according to the provided function.
|
---|
7 | * Only adjacent elements are passed to the comparison function.
|
---|
8 | *
|
---|
9 | * @func
|
---|
10 | * @memberOf R
|
---|
11 | * @since v0.21.0
|
---|
12 | * @category List
|
---|
13 | * @sig ((a, a) → Boolean) → [a] → [[a]]
|
---|
14 | * @param {Function} fn Function for determining whether two given (adjacent)
|
---|
15 | * elements should be in the same group
|
---|
16 | * @param {Array} list The array to group. Also accepts a string, which will be
|
---|
17 | * treated as a list of characters.
|
---|
18 | * @return {List} A list that contains sublists of elements,
|
---|
19 | * whose concatenations are equal to the original list.
|
---|
20 | * @example
|
---|
21 | *
|
---|
22 | * R.groupWith(R.equals, [0, 1, 1, 2, 3, 5, 8, 13, 21])
|
---|
23 | * //=> [[0], [1, 1], [2], [3], [5], [8], [13], [21]]
|
---|
24 | *
|
---|
25 | * R.groupWith((a, b) => a + 1 === b, [0, 1, 1, 2, 3, 5, 8, 13, 21])
|
---|
26 | * //=> [[0, 1], [1, 2, 3], [5], [8], [13], [21]]
|
---|
27 | *
|
---|
28 | * R.groupWith((a, b) => a % 2 === b % 2, [0, 1, 1, 2, 3, 5, 8, 13, 21])
|
---|
29 | * //=> [[0], [1, 1], [2], [3, 5], [8], [13, 21]]
|
---|
30 | *
|
---|
31 | * const isVowel = R.test(/^[aeiou]$/i);
|
---|
32 | * R.groupWith(R.eqBy(isVowel), 'aestiou')
|
---|
33 | * //=> ['ae', 'st', 'iou']
|
---|
34 | */
|
---|
35 |
|
---|
36 |
|
---|
37 | var groupWith =
|
---|
38 | /*#__PURE__*/
|
---|
39 | _curry2(function (fn, list) {
|
---|
40 | var res = [];
|
---|
41 | var idx = 0;
|
---|
42 | var len = list.length;
|
---|
43 |
|
---|
44 | while (idx < len) {
|
---|
45 | var nextidx = idx + 1;
|
---|
46 |
|
---|
47 | while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
|
---|
48 | nextidx += 1;
|
---|
49 | }
|
---|
50 |
|
---|
51 | res.push(list.slice(idx, nextidx));
|
---|
52 | idx = nextidx;
|
---|
53 | }
|
---|
54 |
|
---|
55 | return res;
|
---|
56 | });
|
---|
57 |
|
---|
58 | module.exports = groupWith; |
---|