0.0.1 • Published 5 years ago

js-xiterator v0.0.1

Weekly downloads
-
License
MIT
Repository
github
Last release
5 years ago

build status

js-xiterator

Make ES6 Iterators Functional Again

Synopsis

Suppose we have an iterator…

function* count(n) {
    let i = 0;
    while (i < n) yield i++;
};

We make it more function simply by wrapping it.

import {Xiterator, xiterator as $} from './xiterator.js';
const n = [...  count(10)];    // [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
const o = [...$(count(10)).filter(v=>v%2).map(v=>v*v)]; // [1, 9, 25, 49, 81]
[...Xiterator.zip(n, o)]       // [[0, 1], [1, 9], [2, 25], [3, 49], [4, 81]]

Description

Install

npm install js-xiterator

Usage

locally

import {Xiterator, xiterator} from './xiterator.js`;

remotely

import * as _X from 'https://cdn.jsdelivr.net/npm/js-xiterator@0.0.1/xiterator.min.js';

commonjs (node.js)

use babel or esm.

% node -r esm
Welcome to Node.js v14.5.0.
Type ".help" for more information.
> import * as _X from './xiterator.js'
undefined
> _X
[Module] {
  Xiterator: [Function: Xiterator],
  isIterable: [Function: isIterable],
  repeat: [Function: repeat],
  xiterator: [Function: xiterator],
  xrange: [Function: xrange],
  zip: [Function: zip],
  zipWith: [Function: zipWith]
}
> [..._X.xrange(10).filter(v=>v%2).map(v=>v*v)]
[ 1, 9, 25, 49, 81 ]
> 

methods found in Array.prototype

The following methods in Array.prototype are supported as follows. For any method meth, [...iter.meth(arg)] deeply equals to [...iter].meth(arg).

methodavailable?Comment
concat✔︎
copyWithinmutating
entries✔︎
every✔︎
fillmutating ; see repeat
filter✔︎
find✔︎
findIndex✔︎
flat✔︎
flatMap✔︎
forEach✔︎
includes✔︎** throws RangeError if the 2nd arg is negative
indexOf✔︎
join✔︎
keys✔︎
lastIndexOfneed to iterate backwards
map✔︎
popmutating
pushmutating
reduce✔︎
reduceRightneed to iterate backwards
reversemutating; see reversed
shiftmutating
slice✔︎** throws RangeError if any of the arg is negative
some✔︎
sortmutating
splicemutating
unshiftmutating
filter✔︎

Unavalable methods either:

  • mutating. that is, change the invoking object. e.g. pop, push
  • need to iterate backwards. e.g. lastIndexOf(), reduceRight()

other instance methods

.toArray()

Returns [...this].

.take

.take(n) returns the iterator that takes first n elements of the original iterator.

.drop

.drop(n)returns the iterator that drops firstn` elements of the original iterator.

.zip

.zip(...args) zips iterators in the args. Static version also available.

[...Xiterator.xrange().zip('abcd')]   // [[0,"a"],[1,"b"],[2,"c"],[3,"d"]]

.filled

.filled(value) returns an iterator with all elements replaced with value.

.reversed

Reversed iterator .reversed(). Simply new Xiterator([...iter].reverse()).

static methods

They are also exported so you can import {zip,zipWith} from 'xiterator.js'

Xiterator.zip

Zips iterators in the argument.

[...zip([0,1,2,3], 'abcd')]   // [[0,"a"],[1,"b"],[2,"c"],[3,"d"]]

Xiterator.zipWith

Zips iterators and then feed it to the function.

[...Xiterator.zipWith((a,b)=>a+b, 'bcdfg', 'aeiou')]    // ["ba","ce","di","fo","gu"]

Xiterator.xrange

xrange() as Python 2 (or range() of Python 3).

for (const i of Xiterator.xrange()) // infinite stream of 0, 1, ...
    console.log(i)
}
[...Xiterator.xrange(4)]        // [0, 1, 2, 3]
[...Xiterator.xrange(1,5)]      // [1, 2, 3, 4]
[...Xiterator.xrange(1,5,2)]    // [1, 3] 

Xiterator.repeat

Returns an iterator with all elements are the same.

  • Xiterator.repeat(value) returns an infinite stream of value
  • Xiterator.repeat(value, n) returns repeats value for n times.
0.0.1

5 years ago