import { iter , IterableOrArrayLike } from './iter' ; param object param fn param initial returns export function reduce < T > ( object , fn ) ; export function reduce < T , U > ( object , fn , initial ) ; export function reduce < T > ( object , fn , initial ? ) { let index = 0 ; let it = iter ( object ) ; let first = it . next ( ) ; if ( first === undefined && initial === undefined ) { throw new TypeError ( 'Reduce of empty iterable with no initial value.' ) ; } if ( first === undefined ) { return initial ; } let second = it . next ( ) ; if ( second === undefined && initial === undefined ) { return first ; } if ( second === undefined ) { return fn ( initial , first , index ++ ) ; } let accumulator ; if ( initial === undefined ) { accumulator = fn ( first , second , index ++ ) ; } else { accumulator = fn ( fn ( initial , first , index ++ ) , second , index ++ ) ; } let next ; while ( ( next = it . next ( ) ) !== undefined ) { accumulator = fn ( accumulator , next , index ++ ) ; } return accumulator ; }