1 | var _isArrayLike =
|
2 |
|
3 | require("./_isArrayLike");
|
4 |
|
5 | var _xwrap =
|
6 |
|
7 | require("./_xwrap");
|
8 |
|
9 | var bind =
|
10 |
|
11 | require("../bind");
|
12 |
|
13 | function _arrayReduce(xf, acc, list) {
|
14 | var idx = 0;
|
15 | var len = list.length;
|
16 |
|
17 | while (idx < len) {
|
18 | acc = xf['@@transducer/step'](acc, list[idx]);
|
19 |
|
20 | if (acc && acc['@@transducer/reduced']) {
|
21 | acc = acc['@@transducer/value'];
|
22 | break;
|
23 | }
|
24 |
|
25 | idx += 1;
|
26 | }
|
27 |
|
28 | return xf['@@transducer/result'](acc);
|
29 | }
|
30 |
|
31 | function _iterableReduce(xf, acc, iter) {
|
32 | var step = iter.next();
|
33 |
|
34 | while (!step.done) {
|
35 | acc = xf['@@transducer/step'](acc, step.value);
|
36 |
|
37 | if (acc && acc['@@transducer/reduced']) {
|
38 | acc = acc['@@transducer/value'];
|
39 | break;
|
40 | }
|
41 |
|
42 | step = iter.next();
|
43 | }
|
44 |
|
45 | return xf['@@transducer/result'](acc);
|
46 | }
|
47 |
|
48 | function _methodReduce(xf, acc, obj, methodName) {
|
49 | return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc));
|
50 | }
|
51 |
|
52 | var symIterator = typeof Symbol !== 'undefined' ? Symbol.iterator : '@@iterator';
|
53 |
|
54 | function _reduce(fn, acc, list) {
|
55 | if (typeof fn === 'function') {
|
56 | fn = _xwrap(fn);
|
57 | }
|
58 |
|
59 | if (_isArrayLike(list)) {
|
60 | return _arrayReduce(fn, acc, list);
|
61 | }
|
62 |
|
63 | if (typeof list['fantasy-land/reduce'] === 'function') {
|
64 | return _methodReduce(fn, acc, list, 'fantasy-land/reduce');
|
65 | }
|
66 |
|
67 | if (list[symIterator] != null) {
|
68 | return _iterableReduce(fn, acc, list[symIterator]());
|
69 | }
|
70 |
|
71 | if (typeof list.next === 'function') {
|
72 | return _iterableReduce(fn, acc, list);
|
73 | }
|
74 |
|
75 | if (typeof list.reduce === 'function') {
|
76 | return _methodReduce(fn, acc, list, 'reduce');
|
77 | }
|
78 |
|
79 | throw new TypeError('reduce: list must be array or iterable');
|
80 | }
|
81 |
|
82 | module.exports = _reduce; |
\ | No newline at end of file |