Skip to content

Latest commit

 

History

History
57 lines (40 loc) · 1.68 KB

README.md

File metadata and controls

57 lines (40 loc) · 1.68 KB

Iter classes

This module provides two classes: SyncIter for working with Iterable objects and AsyncIter for working with AsyncIterable objects

These classes use combinators and collectors to do operations

What they actually do is they take the same signature from a collector or combinator, removes the first parameter (AnyIterble) and passes current instance of itself instead

For example, the map combinator method looks as following

import { map } from 'combinators';

class SyncIter<T> {
  map<R>(cb: (val: T) => R): SyncIter<R> {
    return new SyncIter(map(this, cb));
  }
}

The main idea of these classes is to get rid of compositions like this map(filter(map(i, f1), f2), f3) to write them like this map(f1).filter(f2).map(f3)

To create an instance of a class static method from should be used

SyncIter.from([1, 2, 3])
  .map(f1);
  .filter(f2);
  .map(f3)
  .collect([]);

Note that SyncIter accepts only Iterable objects when AsyncIter accepts both Iterable and AsyncIterable objects

const 
  iterable: Iterable<number> = [],
  asyncIterable: AsyncIterable<number> = (async function* () {})();

AsyncIter.from(iterable);
AsyncIter.from(asyncIterable);

Each class has corresponding Iterator to loop through them

for (const value of SyncIter.from([])) {
}


for await (const value of AsyncIter.from([])) {
}

Most likely, you rarely will create instances of these classes directly. Instead the intoIter function will do this