Last active
January 5, 2020 14:28
-
-
Save OliverJAsh/14eb754f7e2eea7a5632405de8146191 to your computer and use it in GitHub Desktop.
Single pass array transformations with IxJS (iterables + generators)
This file contains hidden or 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 add1 = n => n + 1; | |
const square = n => n * n; | |
// | |
// Array methods | |
// | |
// Each time a method is called, a new array is immediately constructed. | |
[1, 2, 3, 4] | |
.map(add1) // [2,3,4,5] | |
.map(square); // [4,9,16,25] | |
// | |
// IxJS functions (built with iterables + generators) | |
// | |
// Each time a method is called, a new Iterable is returned, but no computation is done. | |
import * as Ix from "ix"; | |
Ix.Iterable.from([1, 2, 3, 4]) | |
.map(add1) // Iterable (no computation has been done yet) | |
.map(square) // Iterable (no computation has been done yet) | |
.toArray(); // [4,9,16,25] | |
// Related reading: http://raganwald.com/2017/04/19/incremental.html#the-stream-approach |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment