1 | var _curry2 =
|
2 | /*#__PURE__*/
|
3 | require("./internal/_curry2");
|
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 | * R.groupWith(R.eqBy(isVowel), 'aestiou')
|
32 | * //=> ['ae', 'st', 'iou']
|
33 | */
|
34 |
|
35 |
|
36 | var groupWith =
|
37 | /*#__PURE__*/
|
38 | _curry2(function (fn, list) {
|
39 | var res = [];
|
40 | var idx = 0;
|
41 | var len = list.length;
|
42 |
|
43 | while (idx < len) {
|
44 | var nextidx = idx + 1;
|
45 |
|
46 | while (nextidx < len && fn(list[nextidx - 1], list[nextidx])) {
|
47 | nextidx += 1;
|
48 | }
|
49 |
|
50 | res.push(list.slice(idx, nextidx));
|
51 | idx = nextidx;
|
52 | }
|
53 |
|
54 | return res;
|
55 | });
|
56 |
|
57 | module.exports = groupWith; |
\ | No newline at end of file |