import { IterableX } from '../iterablex' ; import { toArray } from '../toarray' ; import { OperatorFunction } from '../../interfaces' ; import { ScanOptions } from './scanoptions' ; export class ScanRightIterable < T , R > extends IterableX < R > { private _source : Iterable < T > ; private _fn : ( acc : R , x : T , index : number ) => R ; private _seed ? : T | R ; private _hasSeed : boolean ; constructor ( source , options ) { super ( ) ; this . _source = source ; this . _fn = options [ 'callback' ] ; this . _hasSeed = options . hasOwnProperty ( 'seed' ) ; this . _seed = options [ 'seed' ] ; } * [ Symbol . iterator ] ( ) { let hasValue = false ; let acc = this . _seed ; const source = toArray ( this . _source ) ; for ( let offset = source . length - 1 ; offset >= 0 ; offset -- ) { const item = source [ offset ] ; if ( hasValue || ( hasValue = this . _hasSeed ) ) { acc = this . _fn ( < R > acc , item , offset ) ; yield acc ; } else { acc = item ; hasValue = true ; } } } } template T template R param { ScanOptions < T , R > } options returns { OperatorAsyncFunction < T , R > } export function scanRight < T , R = T > ( options ) ; export function scanRight < T , R = T > ( accumulator , seed ? ) ; export function scanRight < T , R = T > ( optionsOrAccumulator , seed ? ) { const options = typeof optionsOrAccumulator === 'function' ? arguments . length > 1 ? { 'callback' : optionsOrAccumulator , 'seed' : seed } : { 'callback' : optionsOrAccumulator } : optionsOrAccumulator ; return function scanRightOperatorFunction ( source ) : IterableX < R > { return new ScanRightIterable ( source , options ) ; } ; }