Arrays Sync Iter Async Iter Copy to clipboard function chunks ( arr, size ) {
const output = [ ] ;
for ( let i = 0 ; i < arr. length; i += size) {
output. push ( arr. slice ( i, i + size) ) ;
}
return output;
}
chunks ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] , 3 ) ;
Copy to clipboard function * chunks ( it, size ) {
let buffer = [ ] ;
for ( let v of it) {
buffer. push ( v) ;
if ( buffer. length === size) {
yield buffer;
buffer = [ ] ;
}
}
if ( buffer. length > 0 ) yield buffer;
}
chunks ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] , 3 ) ;
Copy to clipboard async function * chunks ( it, size ) {
let buffer = [ ] ;
for await ( let v of it) {
buffer. push ( v) ;
if ( buffer. length === size) {
yield buffer;
buffer = [ ] ;
}
}
if ( buffer. length > 0 ) yield buffer;
}
chunks ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] , 3 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 ] . concat ( [ 3 , 4 ] ) ;
Copy to clipboard function * concat ( ... its) {
for ( let it of its) yield * it;
}
concat ( [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ) ;
Copy to clipboard async function * concat ( ... its) {
for await ( let it of its) yield * it;
}
concat ( [ 1 , 2 ] , [ 3 , 4 ] , [ 5 , 6 ] ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function dropWhile ( arr, f ) {
let ok = false ;
return arr. filter ( e => ok || ( ok = ! f ( e) ) ) ;
} ;
dropWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Copy to clipboard function * dropWhile ( it, f ) {
it = it[ Symbol. iterator] ( ) ;
for ( let v of it)
if ( ! f ( v) ) {
yield v;
break ;
}
yield * it;
}
dropWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Copy to clipboard async function * dropWhile ( it, f ) {
it = it[ Symbol. iterator] ( ) ;
for await ( let v of it)
if ( ! f ( v) ) {
yield v;
break ;
}
yield * it;
}
dropWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function equals ( arr1, arr2 ) {
if ( arr1. length !== arr2. length) return false ;
for ( let i in arr1) {
if ( arr1[ i] !== arr2[ i] ) return false ;
}
return true ;
}
equals ( [ 1 , 2 , 3 , 'hello' ] , [ 1 , 2 , 3 , 'hello' ] ) ;
Copy to clipboard function equals ( it1, it2 ) {
it1 = it1[ Symbol. iterator] ( ) ;
it2 = it2[ Symbol. iterator] ( ) ;
while ( true ) {
let i1 = it1. next ( ) , i2 = it2. next ( ) ;
if ( i1. value !== i2. value) return false ;
if ( i1. done || i2. done) return i1. done && i2. done;
}
}
equals ( [ 1 , 2 , 3 , 'hello' ] , [ 1 , 2 , 3 , 'hello' ] ) ;
Copy to clipboard async function equals ( it1, it2 ) {
it1 = it1[ Symbol. iterator] ( ) ;
it2 = it2[ Symbol. iterator] ( ) ;
while ( true ) {
let i1 = await it1. next ( ) , i2 = await it2. next ( ) ;
if ( i1. value !== i2. value) return false ;
if ( i1. done || i2. done) return i1. done && i2. done;
}
}
equals ( [ 1 , 2 , 3 , 'hello' ] , [ 1 , 2 , 3 , 'hello' ] ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 , 4 ] . every ( e => e < 5 ) ;
Copy to clipboard function every ( it, f ) {
let ok = true ;
for ( let v of it) ok = ok && f ( v) ;
return ok;
}
every ( [ 1 , 2 , 3 , 4 ] , e => e < 5 ) ;
Copy to clipboard async function every ( it, f ) {
let ok = true ;
for await ( let v of it) ok = ok && f ( v) ;
return ok;
}
every ( [ 1 , 2 , 3 , 4 ] , e => e < 5 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 ] . fill ( 0 ) ;
Copy to clipboard function * fill ( it, v ) {
for ( let _ of it) yield v;
}
fill ( [ 1 , 2 , 3 ] , 0 ) ;
Copy to clipboard async function * fill ( it, v ) {
for await ( let _ of it) yield v;
}
fill ( [ 1 , 2 , 3 ] , 0 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 , 4 ] . filter ( e => e % 2 == 0 ) ;
Copy to clipboard function * filter ( it, f ) {
for ( let v of it) {
if ( ! f ( v) ) continue ;
yield v;
}
}
filter ( [ 1 , 2 , 3 , 4 ] , e => e % 2 == 0 ) ;
Copy to clipboard async function * filter ( it, f ) {
for await ( let v of it) {
if ( ! f ( v) ) continue ;
yield v;
}
}
filter ( [ 1 , 2 , 3 , 4 ] , e => e % 2 == 0 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 , 4 ] . find ( e => e > 2 ) ;
Copy to clipboard function find ( it, f ) {
for ( let v of it)
if ( f ( v) ) return v;
}
find ( [ 1 , 2 , 3 , 4 ] , e => e > 2 ) ;
Copy to clipboard async function find ( it, f ) {
for await ( let v of it)
if ( f ( v) ) return v;
}
find ( [ 1 , 2 , 3 , 4 ] , e => e > 2 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function flatten ( arr ) {
return Array . prototype. concat . apply ( [ ] , arr) ;
}
flatten ( [ 1 , [ 2 , 3 ] , [ [ 4 ] ] ] ) ;
Copy to clipboard function * flatten ( it ) {
for ( let v of it) {
if ( v[ Symbol. iterator] )
yield * v;
else
yield v;
}
}
flatten ( [ 1 , [ 2 , 3 ] , [ [ 4 ] ] ] ) ;
Copy to clipboard async function * flatten ( it ) {
for await ( let v of it) {
if ( v[ Symbol. iterator] )
yield * v;
else
yield v;
}
}
flatten ( [ 1 , [ 2 , 3 ] , [ [ 4 ] ] ] ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 ] . forEach ( i => console. log ( i) ) ;
Copy to clipboard function forEach ( it, f ) {
for ( let v of it) f ( v) ;
}
forEach ( [ 1 , 2 , 3 ] , i => console. log ( i) ) ;
Copy to clipboard async function forEach ( it, f ) {
for await ( let v of it) f ( v) ;
}
forEach ( [ 1 , 2 , 3 ] , i => console. log ( i) ) ;
Back to top Copy to clipboard function intersection ( a1, a2, eq = ( a, b ) => a === b) {
return a1. filter ( e1 => a2. some ( e2 => eq ( e1, e2) ) ) ;
}
intersection ( [ 1 , 2 , 3 ] , [ 2 , 4 , 6 ] , ( a, b ) => a === b) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 ] . map ( e => e* e)
Copy to clipboard function * map ( it, f ) {
for ( let v of it)
yield f ( v) ;
}
map ( [ 1 , 2 , 3 ] , e => e* e)
Copy to clipboard async function * map ( it, f ) {
for await ( let v of it)
yield f ( v) ;
}
map ( [ 1 , 2 , 3 ] , e => e* e)
Back to top Arrays Sync Iter Async Iter Copy to clipboard function max ( arr, gt = ( a, b ) => a > b) {
return arr. slice ( 1 ) . reduce ( ( max, cur ) => gt ( max, cur) ? max: cur, arr[ 0 ] ) ;
}
max ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Copy to clipboard function max ( it, gt = ( a, b ) => a > b) {
let max = undefined ;
for ( let v of it) {
if ( ! max) {
max = v;
continue ;
}
max = gt ( max, v) ? max: v;
}
return max;
}
max ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Copy to clipboard async function max ( it, gt = ( a, b ) => a > b) {
let max = undefined ;
for await ( let v of it) {
if ( ! max) {
max = v;
continue ;
}
max = gt ( max, v) ? max: v;
}
return max;
}
max ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function min ( arr, gt = ( a, b ) => a > b) {
return arr. slice ( 1 ) . reduce ( ( min, cur ) => gt ( min, cur) ? cur: min, arr[ 0 ] ) ;
}
min ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Copy to clipboard function min ( it, gt = ( a, b ) => a > b) {
let min = undefined ;
for ( let v of it) {
if ( ! min) {
min = v;
continue ;
}
min = gt ( min, v) ? v: min;
}
return min;
}
min ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Copy to clipboard async function min ( it, gt = ( a, b ) => a > b) {
let min = undefined ;
for await ( let v of it) {
if ( ! min) {
min = v;
continue ;
}
min = gt ( min, v) ? v: min;
}
return min;
}
min ( [ { i: 0 , v: 1 } , { i: 1 , v: 9 } , { i: 2 , v: - 2 } ] , ( a, b ) => a. v > b. v) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function range ( start, len ) {
return new Array ( len) . fill ( ) . map ( ( _, i ) => start + i) ;
} ;
range ( - 5 , 10 ) ;
Copy to clipboard function * range ( start, len ) {
for ( ; len > 0 ; len-- ) yield start++ ;
}
range ( - 5 , 10 ) ;
Copy to clipboard async function * range ( start, len ) {
for await ( ; len > 0 ; len-- ) yield start++ ;
}
range ( - 5 , 10 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 ] . reduce ( ( acc, cur ) => acc + cur, 0 ) ;
Copy to clipboard function reduce ( it, f, v0 ) {
for ( let v of it) v0 = f ( v0, v) ;
return v0;
}
reduce ( [ 1 , 2 , 3 ] , ( acc, cur ) => acc + cur, 0 ) ;
Copy to clipboard async function reduce ( it, f, v0 ) {
for await ( let v of it) v0 = f ( v0, v) ;
return v0;
}
reduce ( [ 1 , 2 , 3 ] , ( acc, cur ) => acc + cur, 0 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] . slice ( 5 , 9 ) ;
Copy to clipboard function * slice ( it, start = 0 , end = Number. POSITIVE_INFINITY ) {
it = it[ Symbol. iterator] ( ) ;
for ( ; start > 0 ; start-- , end-- ) it. next ( ) ;
for ( let v of it)
if ( end-- > 0 )
yield v;
else
break ;
}
slice ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] , 5 , 9 ) ;
Copy to clipboard function * slice ( it, start = 0 , end = Number. POSITIVE_INFINITY ) {
it = it[ Symbol. iterator] ( ) ;
for ( ; start > 0 ; start-- , end-- ) await it. next ( ) ;
for await ( let v of it)
if ( end-- > 0 )
yield v;
else
break ;
}
slice ( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] , 5 , 9 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard
[ 1 , 2 , 3 , 4 ] . some ( e => e % 3 === 0 ) ;
Copy to clipboard function some ( it, f ) {
for ( let v of it)
if ( f ( v) ) return true ;
return false ;
}
some ( [ 1 , 2 , 3 , 4 ] , e => e % 3 === 0 ) ;
Copy to clipboard async function some ( it, f ) {
for await ( let v of it)
if ( f ( v) ) return true ;
return false ;
}
some ( [ 1 , 2 , 3 , 4 ] , e => e % 3 === 0 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function takeWhile ( arr, f ) {
let ok = true ;
return arr. filter ( e => ok && ( ok = f ( e) ) ) ;
}
takeWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Copy to clipboard function * takeWhile ( it, f ) {
for ( let v of it) {
if ( ! f ( v) ) return ;
yield v;
}
}
takeWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Copy to clipboard async function * takeWhile ( it, f ) {
for await ( let v of it) {
if ( ! f ( v) ) return ;
yield v;
}
}
takeWhile ( [ 1 , 2 , 3 , 4 ] , e => e < 3 ) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function unique ( arr, f = id => id) {
const vArr = arr. map ( f) ;
return arr. filter ( ( _, i ) => vArr. indexOf ( vArr[ i] ) === i) ;
}
unique ( [ { i: 0 , v: 2 } , { i: 1 , v: 3 } , { i: 2 , v: 2 } ] , e => e. v) ;
Copy to clipboard function * unique ( it, f = id => id) {
const buffer = [ ] ;
for ( let v of it) {
const fv = f ( v) ;
if ( buffer. indexOf ( fv) !== - 1 ) continue ;
buffer. push ( fv) ;
yield v;
}
}
unique ( [ { i: 0 , v: 2 } , { i: 1 , v: 3 } , { i: 2 , v: 2 } ] , e => e. v) ;
Copy to clipboard async function * unique ( it, f = id => id) {
const buffer = [ ] ;
for await ( let v of it) {
const fv = f ( v) ;
if ( buffer. indexOf ( fv) !== - 1 ) continue ;
buffer. push ( fv) ;
yield v;
}
}
unique ( [ { i: 0 , v: 2 } , { i: 1 , v: 3 } , { i: 2 , v: 2 } ] , e => e. v) ;
Back to top Arrays Sync Iter Async Iter Copy to clipboard function zip ( ... arrs) {
const resultLength = Math. min ( ... arrs. map ( a => a. length) ) ;
return new Array ( resultLength)
. fill ( 0 )
. map ( ( _, i ) => arrs. map ( a => a[ i] ) ) ;
}
zip ( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ) ;
Copy to clipboard function * zip ( ... its) {
its = its. map ( it => it[ Symbol. iterator] ( ) ) ;
while ( true ) {
const vs = its. map ( it => it. next ( ) ) ;
if ( vs. some ( v => v. done) ) return ;
yield vs. map ( v => v. value) ;
}
}
zip ( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ) ;
Copy to clipboard async function * zip ( ... its) {
its = its. map ( it => it[ Symbol. iterator] ( ) ) ;
while ( true ) {
const vs = await Promise. all ( its. map ( it => it. next ( ) ) ) ;
if ( vs. some ( v => v. done) ) return ;
yield vs. map ( v => v. value) ;
}
}
zip ( [ 1 , 2 , 3 ] , [ 4 , 5 , 6 ] , [ 7 , 8 , 9 ] ) ;
Back to top