locutus
Version:
Locutus other languages' standard libraries to JavaScript for fun and educational purposes
16 lines (15 loc) • 846 B
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.reduce = reduce;
const _functools_ts_1 = require("../_helpers/_functools.js");
function reduce(func, iterable, initializer) {
// discuss at: https://locutus.io/python/functools/reduce/
// parity verified: Python 3.12
// original by: Kevin van Zonneveld (https://kvz.io)
// note 1: Reduces Python iterables to one plain value using a caller-provided combining function.
// example 1: reduce((a, b) => a + b, [1, 2, 3, 4])
// returns 1: 10
// example 2: reduce((a, b) => a + b, 'abc')
// returns 2: 'abc'
return arguments.length >= 3 ? (0, _functools_ts_1.pythonReduce)(func, iterable, initializer) : (0, _functools_ts_1.pythonReduce)(func, iterable, _functools_ts_1.noInitializer);
}