Skip to content

Instantly share code, notes, and snippets.

@OliverJAsh

OliverJAsh/foo.js

Last active Jan 5, 2020
Embed
What would you like to do?
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
You can’t perform that action at this time.