UNPKG

locutus

Version:

Locutus other languages' standard libraries to JavaScript for fun and educational purposes

13 lines (12 loc) 704 B
import { noInitializer, pythonReduce } from "../_helpers/_functools.js"; export 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 ? pythonReduce(func, iterable, initializer) : pythonReduce(func, iterable, noInitializer); }