import { IAsyncParallel , IParallelEnumerable , ParallelGeneratorType } from "../../types" ; import { BasicParallelEnumerable } from "../BasicParallelEnumerable" ; type ZipFunc = { param first param second returns < TFirst , TSecond > ( first , second ) : IParallelEnumerable < [ TFirst , TSecond ] > ; param first param second param resultSelector returns < TFirst , TSecond , TResult > ( first , second , resultSelector ) : IParallelEnumerable < TResult > ; } ; export const zip = < TFirst , TSecond , TResult > ( first , second , resultSelector ? ) : IParallelEnumerable < TResult > | IParallelEnumerable < [ TFirst , TSecond ] > => { if ( resultSelector ) { return zip2 ( first , second , resultSelector ) ; } else { return zip1 ( first , second ) ; } } ; const zip1 = < T , Y > ( source , second ) : IParallelEnumerable < [ T , Y ] > => { const generator = async ( ) => { const [ left , right ] = await Promise . all ( [ source . toArray ( ) , second . toArray ( ) ] ) ; const maxLength = left . length > right . length ? left . length : right . length ; const results = new Array < [ T , Y ] > ( maxLength ) ; for ( let i = 0 ; i < maxLength ; i ++ ) { const a = left [ i ] ; const b = right [ i ] ; results [ i ] = [ a , b ] ; } return results ; } ; return new BasicParallelEnumerable ( { generator , type : ParallelGeneratorType . PromiseToArray , } ) ; } ; const zip2 = < T , Y , OUT > ( source , second , resultSelector ) : IParallelEnumerable < OUT > => { const generator = async ( ) => { const [ left , right ] = await Promise . all ( [ source . toArray ( ) , second . toArray ( ) ] ) ; const maxLength = left . length > right . length ? left . length : right . length ; const results = new Array < OUT > ( maxLength ) ; for ( let i = 0 ; i < maxLength ; i ++ ) { const a = left [ i ] ; const b = right [ i ] ; results [ i ] = resultSelector ( a , b ) ; } return results ; } ; return new BasicParallelEnumerable ( { generator , type : ParallelGeneratorType . PromiseToArray , } ) ; } ;