-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(3247): split _reduce to _xReduce(for transformers) & _reduce…
…(for reducers) (#3248)
- Loading branch information
Showing
16 changed files
with
118 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
export default function _arrayReduce(reducer, acc, list) { | ||
var index = 0; | ||
var length = list.length; | ||
while (index < length) { | ||
acc = reducer(acc, list[index]); | ||
index += 1; | ||
} | ||
return acc; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import _isArrayLike from './_isArrayLike.js'; | ||
|
||
|
||
var symIterator = (typeof Symbol !== 'undefined') ? Symbol.iterator : '@@iterator'; | ||
|
||
export default function _createReduce(arrayReduce, methodReduce, iterableReduce) { | ||
return function _reduce(xf, acc, list) { | ||
if (_isArrayLike(list)) { | ||
return arrayReduce(xf, acc, list); | ||
} | ||
if (list == null) { | ||
return acc; | ||
} | ||
if (typeof list['fantasy-land/reduce'] === 'function') { | ||
return methodReduce(xf, acc, list, 'fantasy-land/reduce'); | ||
} | ||
if (list[symIterator] != null) { | ||
return iterableReduce(xf, acc, list[symIterator]()); | ||
} | ||
if (typeof list.next === 'function') { | ||
return iterableReduce(xf, acc, list); | ||
} | ||
if (typeof list.reduce === 'function') { | ||
return methodReduce(xf, acc, list, 'reduce'); | ||
} | ||
|
||
throw new TypeError('reduce: list must be array or iterable'); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,63 +1,19 @@ | ||
import _isArrayLike from './_isArrayLike.js'; | ||
import _xwrap from './_xwrap.js'; | ||
import bind from '../bind.js'; | ||
import _arrayReduce from './_arrayReduce.js'; | ||
import _createReduce from './_createReduce.js'; | ||
|
||
|
||
function _arrayReduce(xf, acc, list) { | ||
var idx = 0; | ||
var len = list.length; | ||
while (idx < len) { | ||
acc = xf['@@transducer/step'](acc, list[idx]); | ||
if (acc && acc['@@transducer/reduced']) { | ||
acc = acc['@@transducer/value']; | ||
break; | ||
} | ||
idx += 1; | ||
} | ||
return xf['@@transducer/result'](acc); | ||
} | ||
|
||
function _iterableReduce(xf, acc, iter) { | ||
function _iterableReduce(reducer, acc, iter) { | ||
var step = iter.next(); | ||
while (!step.done) { | ||
acc = xf['@@transducer/step'](acc, step.value); | ||
if (acc && acc['@@transducer/reduced']) { | ||
acc = acc['@@transducer/value']; | ||
break; | ||
} | ||
acc = reducer(acc, step.value); | ||
step = iter.next(); | ||
} | ||
return xf['@@transducer/result'](acc); | ||
return acc; | ||
} | ||
|
||
function _methodReduce(xf, acc, obj, methodName) { | ||
return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc)); | ||
function _methodReduce(reducer, acc, obj, methodName) { | ||
return obj[methodName](reducer, acc); | ||
} | ||
|
||
var symIterator = (typeof Symbol !== 'undefined') ? Symbol.iterator : '@@iterator'; | ||
|
||
export default function _reduce(fn, acc, list) { | ||
if (typeof fn === 'function') { | ||
fn = _xwrap(fn); | ||
} | ||
if (_isArrayLike(list)) { | ||
return _arrayReduce(fn, acc, list); | ||
} | ||
if (list == null) { | ||
return acc; | ||
} | ||
if (typeof list['fantasy-land/reduce'] === 'function') { | ||
return _methodReduce(fn, acc, list, 'fantasy-land/reduce'); | ||
} | ||
if (list[symIterator] != null) { | ||
return _iterableReduce(fn, acc, list[symIterator]()); | ||
} | ||
if (typeof list.next === 'function') { | ||
return _iterableReduce(fn, acc, list); | ||
} | ||
if (typeof list.reduce === 'function') { | ||
return _methodReduce(fn, acc, list, 'reduce'); | ||
} | ||
|
||
throw new TypeError('reduce: list must be array or iterable'); | ||
} | ||
var _reduce = _createReduce(_arrayReduce, _methodReduce, _iterableReduce); | ||
export default _reduce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
export default function _xArrayReduce(xf, acc, list) { | ||
var idx = 0; | ||
var len = list.length; | ||
while (idx < len) { | ||
acc = xf['@@transducer/step'](acc, list[idx]); | ||
if (acc && acc['@@transducer/reduced']) { | ||
acc = acc['@@transducer/value']; | ||
break; | ||
} | ||
idx += 1; | ||
} | ||
return xf['@@transducer/result'](acc); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import _createReduce from './_createReduce.js'; | ||
import _xArrayReduce from './_xArrayReduce.js'; | ||
import bind from '../bind.js'; | ||
|
||
|
||
function _xIterableReduce(xf, acc, iter) { | ||
var step = iter.next(); | ||
while (!step.done) { | ||
acc = xf['@@transducer/step'](acc, step.value); | ||
if (acc && acc['@@transducer/reduced']) { | ||
acc = acc['@@transducer/value']; | ||
break; | ||
} | ||
step = iter.next(); | ||
} | ||
return xf['@@transducer/result'](acc); | ||
} | ||
|
||
function _xMethodReduce(xf, acc, obj, methodName) { | ||
return xf['@@transducer/result'](obj[methodName](bind(xf['@@transducer/step'], xf), acc)); | ||
} | ||
|
||
var _xReduce = _createReduce(_xArrayReduce, _xMethodReduce, _xIterableReduce); | ||
export default _xReduce; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters