Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh
Last active January 5, 2020 14:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OliverJAsh/14eb754f7e2eea7a5632405de8146191 to your computer and use it in GitHub Desktop.
Save OliverJAsh/14eb754f7e2eea7a5632405de8146191 to your computer and use it in GitHub Desktop.
Single pass array transformations with IxJS (iterables + generators)
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