Last active
May 15, 2017 00:45
-
-
Save mattpodwysocki/34e76e199442971885a4f48c0730aa40 to your computer and use it in GitHub Desktop.
Implementations of `flatten` and `flatMap` as described in the [ES Proposal](https://bterlson.github.io/proposal-flatMap/)
This file contains 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
'use strict'; | |
import { bindCallback } from '../internal/bindcallback'; | |
export async function *flatMapAsync<TSource, TResult>( | |
source: AsyncIterable<TSource>, | |
fn: (value: TSource) => AsyncIterable<TResult>, | |
thisArg?: any): AsyncIterable<TResult> { | |
let sel = bindCallback(fn, thisArg, 1); | |
for await (let outerItem of source) { | |
for await (let innerItem of sel(outerItem)) { | |
yield innerItem; | |
} | |
} | |
} |
This file contains 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
'use strict'; | |
import { bindCallback } from '../internal/bindcallback'; | |
export async function *flatMapAsync<TSource, TResult>( | |
source: AsyncIterable<TSource>, | |
fn: (value: TSource) => AsyncIterable<TResult>, | |
thisArg?: any): AsyncIterable<TResult> { | |
let sel = bindCallback(fn, thisArg, 1); | |
for await (let outerItem of source) { | |
for await (let innerItem of sel(outerItem)) { | |
yield innerItem; | |
} | |
} | |
} |
This file contains 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
'use strict'; | |
import { bindCallback } from '../internal/bindcallback'; | |
export async function *flatMapAsync<TSource, TResult>( | |
source: AsyncIterable<TSource>, | |
fn: (value: TSource) => AsyncIterable<TResult>, | |
thisArg?: any): AsyncIterable<TResult> { | |
let sel = bindCallback(fn, thisArg, 1); | |
for await (let outerItem of source) { | |
for await (let innerItem of sel(outerItem)) { | |
yield innerItem; | |
} | |
} | |
} |
This file contains 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
'use strict'; | |
import { isAsyncIterable } from '../internal/isasynciterable'; | |
export async function *flattenAsync<T>(iterable: AsyncIterable<T>, depth: number = Infinity): AsyncIterable<T> { | |
if (depth === 0) { return iterable; } | |
for await (let item of iterable) { | |
if (isAsyncIterable(item)) { | |
yield *flattenAsync(item, depth - 1); | |
} else { | |
yield item; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment