Created
November 18, 2019 15:44
-
-
Save jtmthf/7699d25564f7d77c5346041ce55df400 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function* chunk<T>(iterable: Iterable<T>, size = 1): Iterable<Array<T>> { | |
| let buffer: T[] = []; | |
| for (const value of iterable) { | |
| buffer.push(value); | |
| if (buffer.length === size) { | |
| yield buffer; | |
| } | |
| buffer = []; | |
| } | |
| if (buffer.length) { | |
| return buffer; | |
| } | |
| } | |
| function* compact<T>(iterable: Iterable<T>): Iterable<Exclude<T, false | null | 0 | '' | undefined>> { | |
| for (const value of iterable) { | |
| if (value) { | |
| yield value as Exclude<T, false | null | 0 | '' | undefined>; | |
| } | |
| } | |
| } | |
| function concat<T, U>(iterable: Iterable<T>, values: Iterable<U>): Iterable<T | U>; | |
| function concat<T, U>(iterable: Iterable<T>, ...values: U[]): Iterable<T | U> | |
| function* concat<T, U>(iterable: Iterable<T>, ...values: Array<U | Iterable<U>>): Iterable<T | U> { | |
| yield* iterable; | |
| if (isIterable(values[0])) { | |
| yield* values[0] as Iterable<U>; | |
| return; | |
| } | |
| yield* values as U[]; | |
| } | |
| function chunkFP(size?: number): <T>(iterable: Iterable<T>) => Iterable<Array<T>>; | |
| function chunkFP<T>(size: number | undefined, iterable: Iterable<T>): Iterable<Array<T>>; | |
| function chunkFP<T>(size?: number, iterable?: Iterable<T>): ((iterable: Iterable<T>) => Iterable<Array<T>>) | Iterable<Array<T>> { | |
| if (iterable) { | |
| return chunk(iterable, size); | |
| } | |
| return iterable => chunk(iterable, size); | |
| } | |
| function* map<T, U>(iterable: Iterable<T>, mapper: (value: T) => U): Iterable<U> { | |
| for (const value of iterable) { | |
| yield mapper(value); | |
| } | |
| } | |
| function mapFP<T, U>(mapper: (value: T) => U): (iterable: Iterable<T>) => Iterable<U>; | |
| function mapFP<T, U>(mapper: (value: T) => U, iterable: Iterable<T>): Iterable<U>; | |
| function mapFP<T, U>(mapper: (value: T) => U, iterable?: Iterable<T>): ((iterable: Iterable<T>) => Iterable<U>) | Iterable<U> { | |
| if (iterable) { | |
| return map(iterable, mapper); | |
| } | |
| return iterable => map(iterable, mapper); | |
| } | |
| function isIterable<T>(value: any): value is Iterable<T> { | |
| return value && value[Symbol.iterator] | |
| } | |
| class Iter<T> implements Iterable<T> { | |
| private constructor(private readonly source: Iterable<T>) { } | |
| chunk(size?: number): Iter<T[]> { | |
| return new Iter(chunk(this, size)); | |
| } | |
| compact() { | |
| return new Iter(compact(this)); | |
| } | |
| map<U>(mapper: (value: T) => U): Iter<U> { | |
| return new Iter(map(this, mapper)); | |
| } | |
| [Symbol.iterator]() { | |
| return this.source[Symbol.iterator]() | |
| } | |
| static from<T>(iterable: Iterable<T>) { | |
| return new Iter(iterable); | |
| } | |
| static of<T>(...items: T[]) { | |
| return new Iter(items); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment