This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result = numbers | |
.filter(v => v % 2 === 0) // retain only even numbers | |
.map(v => v + 1) // increase these numbers | |
.slice(0, 3); // get the first 3 items |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result2 = _(numbers) | |
.filter(v => v % 2 === 0) // retain only even numbers | |
.map(v => v + 1) // increase these numbers | |
.slice(0, 3) // get the first 3 items | |
.value(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* map(src, op) { | |
for (const value of src) { | |
yield op(value); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* filter(src, op) { | |
for (const value of src) { | |
if (op(value)) { | |
yield value; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result2 = Array.from( | |
slice( | |
map( | |
filter(numbers, v => v % 2 === 0), | |
v => v + 1 | |
), | |
0, 3 | |
) | |
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function* slice(src, start, finish = Number.MAX_SAFE_INTEGER) { | |
let index = 0; | |
for (const value of src) { | |
if (index >= finish) { | |
break; | |
} | |
if (index >= start) { | |
yield value; | |
} | |
++index; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const result3 = numbers | |
|> filter(#, v => v % 2 === 0) | |
|> map(#, v => v + 1) | |
|> slice(#, 0, 3) | |
|> Array.from; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const retainOnlyNumeric = obj | |
|> Object.entries | |
|> filter(#, ([, value]) => typeof value === 'number') | |
|> Object.fromEntries; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const largeNumber = numbers | |
|> filter(#, v => v > 42) | |
|> slice(#, 0, 1) | |
|> Array.from | |
|> (a => a[0])(#); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function head(src) { | |
for (const value of src) { | |
return value; | |
} | |
} |
OlderNewer