export async function reduce < TElement > ( source , callback ) ; export async function reduce < TElement , TAccumulator > ( source , callback , initialValue ) ; export async function reduce < TElement > ( source , callback , initialValue ? ) { let [ index , accumulator ] = [ - 1 , initialValue ] ; for await ( const element of source ) { accumulator = ++ index === 0 && initialValue === undefined ? element : callback ( accumulator , element , index ) ; } if ( initialValue === undefined && index === - 1 ) { throw new Error ( 'Sequence contains no elements.' ) ; } return accumulator ; }