Skip to content

Instantly share code, notes, and snippets.

@nicholasguyett
nicholasguyett / function-iterator.js
Last active October 15, 2020 02:01
Functional Iterator
/* This class wraps an iterator/iterable and allows the map/filter/reduce methods
* to be used without being forced to make intermediate temporary arrays.
* I've also implemented the flatten and flatmap functions for convenience
*/
class FunctionalIterator {
constructor(source) {
// Get iterator from source, if present. Otherwise, assume that the source is an iterator
this.source = source[Symbol.iterator] ? source[Symbol.iterator]() : source;
}