Handling iterables like lazy arrays.

Overview

Iterum

travis ci npm version Coverage Status Dependency Status

iterum library provides a class for handling iterable transformations inspired in Array methods and lodash/fp functions. This library also supplies combinatorial functions like permutations, combinations, variations, product, power and powerSet that has a high computational cost but this library is able to support taking advantage of lazy evaluation.

Install

$ npm install iterum --save

Usage

const Iterum = require('iterum')
const {range} = Iterum

const iterable = range(1, Infinity) // (1 2 3 4 5 6 7 8...)
    .map(value => 2 * value) // (2 4 6 8 10 12 14 16...)
    .filter(value => value % 3 === 0 || value % 3 === 1) // (4 6 10 12 16...)
    .take(5) // (4 6 10 12 16)
    .concat([1, 2, 3]) // (4 6 10 12 16 1 2 3)

// converting to array:
[...iterable] // [4, 6, 10, 12, 16, 1, 2, 3]

// traversing values:
for (const val of iterable) {
    // ...
}

// creating an iterator that traverses the values
let iterator = iterable[Symbol.iterator]()
iterator.next() // {value: 4, done: false}
iterator.next() // {value: 6, done: false}
iterator.next() // {value: 10, done: false}
iterator.next() // {value: 12, done: false}
iterator.next() // {value: 16, done: false}
iterator.next() // {value: 1, done: false}
iterator.next() // {value: 2, done: false}
iterator.next() // {value: 3, done: false}
iterator.next() // {value: undefined, done: true}

Why Iterum?

Iterable interface has been introduced by ES2015. An object that implements this interface has a Symbol.iterator property with a generator value which arity is 0. For example we can create an obj variable that implements Iterable interface:

let obj = {
    [Symbol.iterator]: function* () {
        for (let i = 0; i <= 10; ++i) {
            yield i
        }
    }
}

Any object that implements the Iterable interface can use for..of statement and the spread operator. For example:

[...obj] // returns [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

for (let x of obj) {
  // x traverses all values between 0 and 10
}

Then, obj can be processed as an ordered list of values. However, unlike built-in iterables (Array, Set, Map, String, etc), obj is a lazy iterable. It means that, thanks to generators, obj does not store the computed values in memory and its values are computed just when are required. These are the essential features for implementing lazy evaluation.

It is even possible to create a new iterable without computing or storing obj values in memory. This is an example of creating a new iterable that iterates over the double of values generated by obj:

let doubleObj = {
    [Symbol.iterator]: function* () {
        for (const value of obj) {
            yield 2 * value
        }
    }
}

[...doubleObj] // returns [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20]

iterum is a library that takes advantage of these techniques to provide a collection of functions and methods that apply iterable transformations without traversing values. Then, using iterum, the previous example could be expressed thus:

const Iterum = require('iterum')

const obj = Iterum.range(0, 10) // (0 1 2 3 4 5 6 7 8 9 10)

const doubleObj = obj.map(e => 2 * e) // (0 2 4 6 8 10 12 14 16 18 20)

Features

API Documentation

Support

  • Node.js >=6
  • ES2015 transpilers

Customized builds

Iterum allows to build just what you need. Read customized build section for more information.

License

MIT

Comments
  • Release 1.0

    Release 1.0

    I would like to implement a new release using ES2015 features.

    The current package just allows manipulate generators with 0-arity. There are some similar data structure in es2015: iterables

    Iterables are objects that wraps a generator of 0-arity in Symbol.iterator property. Then:

    • Iterum(generator) constructor will be deprecated and Iterum(iterable) will be the new constructor.
    • Iterum instance will also be an iterable.
    • new ES2015 Array inmutable methods will be implemented in Iterum prototype.
    • Support for node >=6

    Then, new branch dev-1.0 has been created and I will be happy to receive any suggestions and constributions.

    Thanks.

    enhancement 
    opened by xgbuils 8
  • Iterum methods don't create a standalone iterator [0.4.0]

    Iterum methods don't create a standalone iterator [0.4.0]

    Example:

    var Iterum = require('iterum')
    var Range = Iterum.Range
    
    var it = Iterum(Range(3, 5))
    var mapIt = it.map(function (value) {
        return 2 * value
    })
    
    mapIt.next() // {value: 6, done: false}
    mapIt.next() // {value: 8, done: false}
    it.next() // {value: 5, done: false}
    it.next() // {value: undefined, done: false}
    

    If next method of mapIt is called, next method of it is affected.

    bug enhancement 
    opened by xgbuils 7
  • release 2

    release 2

    I would like to do some breaking changes.

    First of all, I prefer that methods that returns lazy iterables of arrays (zip, cartesian, permutations) return iterables of iterables to achieve a real laziness. For example, is not possible to do permutations or cartesian product of potentially infinite iterables.

    At second point, I want to use memoization. For example, if we have:

    const iterable = Iterum([1, 2, 4, 8, 16, 32, 64, 128])
    

    and then we need to do:

    const slice = iterable.slice(6)
    const result = slice.concat(slice)
    [...result]
    

    The first iterable is traversed twice.

    I don't want this behaviour.

    Finally, there are some methods like map, reduce, every, etc. that I would like to simplify thus:

    1. removing the context parameter and,
    2. Allowing the value parameter on callback functions.

    The reason is that point 1 can be acomplished using bind and point 2 can be achieved using zip combined with other methods. For example:

    const iterable = Iterum([1, 3, 5, 7]).map((e, i) => 2 * e + i)
    // would be implemented thus:
    const {range} = Iterum
    const iterable = Iterum([1, 3, 5, 7]).zip(range(0, Infinity)).map(([e, i]) => 2 * e + i)
    
    opened by xgbuils 4
  • adapt functions to functional style (iterable last)

    adapt functions to functional style (iterable last)

    Currently, iterum library use functions inspired by lodash. However, after reading more about functional programming and knowing another libraries like imlazy, ramda or lodash/fp, I prefer iterables in the last parameter.

    enhancement 
    opened by xgbuils 3
  • Implement lazy cartesian product

    Implement lazy cartesian product

    Now, cartesian method does not allow to be used with infinite iterables. If cartesian returns an iterable of iterables, this method will be able to be lazy.

    enhancement 
    opened by xgbuils 3
  • removing `fromIndex` arguments.

    removing `fromIndex` arguments.

    includes and indexOf has fromIndex arguments which don't make sense.

    In Array approach it is useful to have better performance. But in lazy approach doing iterum.drop(fromIndex).includes(...) is the same.

    opened by xgbuils 2
  • Implement groupBy method for potentially infinite iterables

    Implement groupBy method for potentially infinite iterables

    If a groupBy method returns a lazy iterable of lazy iterables, it is possible call this method on potentially infinite iterable:

    range(0, Infinity).groupBy(e => e % 5) /* [
        [0, 5, 10, ... ],
        [1, 6, 11, ... ],
        [2, 7, 12, ... ],
        ...
    ]
    
    enhancement 
    opened by xgbuils 2
  • cartesian performance

    cartesian performance

    I found es-iter library that implements similar methods to this library. But there are mutables. For example:

    const Iter = require('es-iter')
    var a = new Iter([1, 2, 3])
    var b = a.map(e => 2 * e)
    [...a] // returns [1, 2, 3]
    [...b] // however it returns [] because has been consumed previously
    

    However, I found some performance problems in Iterum#cartesian method vs Iter#product method. When a cartesian product of large number of iterables is calculated, it takes a lot of time:

    I will try to solve this issue.

    https://jsperf.com/iterum-vs-es-iter-cartesian-product

    opened by xgbuils 2
  • implement `combinations` method

    implement `combinations` method

    I would like to implement a method that returns the combinations of n values that produces an iterable. For example:

    Iterum([1, 2, 3, 4]).combinations(2) /*
        (1 2)
        (1 3)
        (1 4)
        (2 3)
        (2 4)
        (3 4)
    ) */
    

    I don't know what is the best order to produce values yet

    enhancement 
    opened by xgbuils 1
  • 0.9.0 features

    0.9.0 features

    All methods except build and toArray (and forEach?) should return Iterum instances. .every, .indexOf, .reduce, .reduceRight and .some that return other type of values. They should return a generator that builds an iterator with one iteration with the same value that returns in [email protected]

    enhancement 
    opened by xgbuils 1
  • 0.7.0 features

    0.7.0 features

    • add Cartesian constructor
    • add How to customize Iterum build (just import what you need!).
    • change from value methods/transform methods to eager methods/lazy methods
    enhancement 
    opened by xgbuils 1
Owner
Xavier Garcia Buils
Xavier Garcia Buils
An extension to Async adding better handling of mixed Series / Parallel tasks via object chaining

async-chainable Flow control for NodeJS applications. This builds on the foundations of the Async library while adding better handling of mixed Series

Matt Carter 25 Jul 15, 2019
CSP channels for Javascript (like Clojurescript's core.async, or Go) THIS IS AN UPSTREAM FORK

js-csp Communicating sequential processes for Javascript (like Clojurescript core.async, or Go). Examples var csp = require("js-csp"); Pingpong (porte

James Long 283 Sep 22, 2022
A small (~600B gzip), useful set of methods for lazy iteration of iterables.

@ricokahler/lazy · A small (~600B gzip*), useful set of methods for lazy iteration of iterables. Why this lazy lib? Do I even need a lazy lib? Install

Rico Kahler 11 Sep 10, 2022
Fast and lightweight dependency-free vanilla JavaScript polyfill for native lazy loading / the awesome loading='lazy'-attribute.

loading="lazy" attribute polyfill Fast and lightweight vanilla JavaScript polyfill for native lazy loading, meaning the behaviour to load elements rig

Maximilian Franzke 571 Dec 30, 2022
Well-tested utility functions dealing with async iterables

aitertools This library provides a well-tested collection of small utility functions dealing with async iterables. You can think of it as LINQ or aite

Hong Minhee (洪 民憙) 11 Aug 15, 2022
Iterables, streams for typescript

Iterable for Typescript Similar to what we know from C#, Dart or any other language which supports them, we use Iterables to stream over collections.

null 3 Oct 15, 2022
Lightweight WebSocket lib with socket.io-like event handling, requests, and channels

ws-wrapper Lightweight and isomorphic Web Socket lib with socket.io-like event handling, Promise-based requests, and channels. What? Much like Socket.

Blake Miner 70 Dec 23, 2022
Node.js object hash library with properties/arrays sorting to provide constant hashes. It also provides a method that returns sorted object strings that can be used for object comparison without hashes.

node-object-hash Tiny and fast node.js object hash library with properties/arrays sorting to provide constant hashes. It also provides a method that r

Alexander 73 Oct 7, 2022
Sorting Arrays as simple as it gets.

Sort Sorting Arrays as simple as it gets. This module is published at: https://deno.land/x/sort. Simple Usage Example import { SortService, Direction

null 11 May 12, 2022
Input a list of Handshake top-level domains, outputs names sorted into 4 arrays: available, registered, reserved, or invalid.

name-check A simple NodeJS package that, given a flat list of top-level domain names, queries the Handshake (HNS) blockchain in order to classify each

Neel Yadav 2 Jan 8, 2022
Lightweight (< 2.3kB gzipped) and performant natural sorting of arrays and collections by differentiating between unicode characters, numbers, dates, etc.

fast-natural-order-by Lightweight (< 2.3kB gzipped) and performant natural sorting of arrays and collections by differentiating between unicode charac

Shelf 5 Nov 14, 2022
sum-safe rounding for number arrays

saferound About saferound is a simplified typescript adaption of the python library Iteround, a sum-safe rounding library. The library solves the prob

Jonas Strassel 6 Oct 31, 2022
This project will be a basic website that allows users to add/remove books from a list. The main objective is to understand how to use JavaScript objects and arrays and dynamically modify the DOM and add basic events.

Awesome-books Awesome Books This project will be a basic website that allows users to add/remove books from a list. This project is part of the Microv

Aleksandra Ujvari 10 Oct 3, 2022
Serialize an HTML Form to a JavaScript Object, supporting nested attributes and arrays.

jquery.serializeJSON Adds the method .serializeJSON() to jQuery to serializes a form into a JavaScript Object. Supports the same format for nested par

Mario Izquierdo 1.7k Dec 12, 2022
Repositorio sobre arrays con información y ejemplos.

Arrays Repositorio sobre arrays con información y ejemplos. 1)Para poder utilizar el repositorio correctamente es necesario que se eliminen los coment

EzequielJumilla 10 Sep 14, 2022
jQuery-plugin for add/remove dom-items with renaming form-elements (arrays)

dynamicrows jQuery-plugin for add/remove rows by cloning existing row / renaming form-elements (arrays). Requirements jQuery >=2.0 if move-action used

Dennis Dohle 0 Nov 9, 2020
Convert arrays to bitmask representations to quickly operate with them through bitwise operations. (uses JSBI)

Bitmask Set (JSBI) Convert arrays to bitmask representations to quickly operate with them through bitwise operations. In general, this approach can be

codescrum 4 Sep 7, 2022
Simple utils to pack arrays, objects and strings to a flat object (and back again).

packrup Simple utils to pack (and unpack) arrays and strings to a flat object. Status: In Development Please report any issues ?? Made possible by my

Harlan Wilton 15 Dec 23, 2022
Basic website that allows users to add/remove books from a list. Achieved using JavaScript objects and arrays, dynamically modifying the DOM and adding basic events.

Awesome Books Basic website that allows users to add/remove books from a list. Achieved using JavaScript objects and arrays, dynamically modifying the

Didier Peran Ganthier 6 Dec 20, 2022