Skip to content

fx-flow v0.7.0 / pipe/lazy / flat

Function: flat()

flat<A, B>(iterable, depth?): FlatReturnValue<A, B>

Return flattened Iterable/AsyncIterable. If first argument is number, more perform flatten flat(2, [[[1,2]]]) // [1,2].

Type parameters

A extends UniversalIterable

B extends number = 1

Parameters

iterable: A

depth?: B

Returns

FlatReturnValue<A, B>

Example

typescript
const iter = flat([1,[2,3],[[4,5]]]);
iter.next() // {done:false, value: 1}
iter.next() // {done:false, value: 2}
iter.next() // {done:false, value: 3}
iter.next() // {done:false, value: [4, 5]}
iter.next() // {done:true, value: undefined}

pipe(
 [1,[2, 3],[[4, 5]]],
 flat,
 toArray,
); // [1, 2, 3, [4, 5]]

await pipe(
 Promise.resolve([1,[2, 3],[[4, 5]]]),
 flat,
 toArray,
); // [1, 2, 3, [4, 5]]

More examples

Source

packages/core/src/pipe/lazy/flat.ts:197

Released under the MIT License.