Skip to content

Commit

Permalink
Reimplement map<T>() with native interators
Browse files Browse the repository at this point in the history
  • Loading branch information
afshin committed Aug 11, 2022
1 parent 1041655 commit 8c3b2c0
Showing 1 changed file with 9 additions and 62 deletions.
71 changes: 9 additions & 62 deletions packages/algorithm/src/map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,85 +7,32 @@
|
| The full license is in the file LICENSE, distributed with this software.
|----------------------------------------------------------------------------*/
import { IIterator, iter, IterableOrArrayLike } from './iter';

/**
* Transform the values of an iterable with a mapping function.
*
* @param object - The iterable or array-like object of interest.
* @param object - The iterable object of interest.
*
* @param fn - The mapping function to invoke for each value.
*
* @returns An iterator which yields the transformed values.
*
* #### Example
* ```typescript
* import { map, toArray } from '@lumino/algorithm';
* import { map } from '@lumino/algorithm';
*
* let data = [1, 2, 3];
*
* let stream = map(data, value => value * 2);
*
* toArray(stream); // [2, 4, 6]
* Array.from(stream); // [2, 4, 6]
* ```
*/
export function map<T, U>(
object: IterableOrArrayLike<T>,
export function* map<T, U>(
object: Iterable<T>,
fn: (value: T, index: number) => U
): IIterator<U> {
return new MapIterator<T, U>(iter(object), fn);
}

/**
* An iterator which transforms values using a mapping function.
*/
export class MapIterator<T, U> implements IIterator<U> {
/**
* Construct a new map iterator.
*
* @param source - The iterator of values of interest.
*
* @param fn - The mapping function to invoke for each value.
*/
constructor(source: IIterator<T>, fn: (value: T, index: number) => U) {
this._source = source;
this._fn = fn;
}

/**
* Get an iterator over the object's values.
*
* @returns An iterator which yields the object's values.
*/
iter(): IIterator<U> {
return this;
}

/**
* Create an independent clone of the iterator.
*
* @returns A new independent clone of the iterator.
*/
clone(): IIterator<U> {
let result = new MapIterator<T, U>(this._source.clone(), this._fn);
result._index = this._index;
return result;
}

/**
* Get the next value from the iterator.
*
* @returns The next value from the iterator, or `undefined`.
*/
next(): U | undefined {
let value = this._source.next();
if (value === undefined) {
return undefined;
}
return this._fn.call(undefined, value, this._index++);
) {
let index = 0;
for (const value of object) {
yield fn(value, index++);
}

private _index = 0;
private _source: IIterator<T>;
private _fn: (value: T, index: number) => U;
}

0 comments on commit 8c3b2c0

Please sign in to comment.