FIXME export type Seq < ValueT > = IterableIterator < ValueT > ; export function * fold ( f , seq , acc ? ) { let v = seq . next ( ) ; while ( ! v . done ) { yield f ( v . value , acc ) ; v = seq . next ( ) ; } ; } export function * filter ( f , seq ) { let v = seq . next ( ) ; while ( ! v . done ) { if ( f ( v . value ) ) yield v . value ; v = seq . next ( ) ; } } export function * map ( f , seq ) { return fold ( f , seq ) ; } export function take ( seq , n ) { const result = [ ] ; let i = 1 ; let v = seq . next ( ) ; while ( i <= n && ! v . done ) { result . push ( v . value ) ; v = seq . next ( ) ; i ++ ; } return result ; } export function * fromArray ( arr ) { for ( let i = 0 ; i < arr . length ; i ++ ) { yield arr [ i ] ; } } export function toArray ( seq ) { return take ( seq , Infinity ) ; } ; export function * join ( seqs ) { while ( true ) { let values = Array ( seqs . length ) ; for ( let i = 0 ; i < seqs . length ; ++ i ) { let v = take ( seqs [ i ] , 1 ) ; if ( v . length === 0 ) { return ; } values [ i ] = v [ 0 ] ; } yield values ; } }