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
[ 1 , 2 , 3 , 4 ] . every ( 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 Copy to clipboard
function intersection ( a1, a2, eq) {
return a1. filter ( e1 = > a2. some ( e2 = > eq ( e1, e2) ) ) ;
}
intersection ( [ 1 , 2 , 3 ] , [ 2 , 4 , 6 ] , ( a, b) = > a === b) ;
Copy to clipboard Copy to clipboard 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) {
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) {
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) {
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) {
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) {
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) {
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
[ 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 ] . 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) {
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) {
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) {
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