<const S extends ReadonlyArray<ReadonlyArray<any>>>(
self: S
): ReadonlyArray.Flatten<S>Flattens a nested array of arrays into a single array.
When to use
Use to collapse one level of nested arrays when no per-element mapping is needed.
Example (Flattening nested arrays)
import { Array } from "effect"
console.log(Array.flatten([[1, 2], [], [3, 4], [], [5, 6]])) // [1, 2, 3, 4, 5, 6]sequencingflatMap
Source effect/Array.ts:35712 lines
export const const flatten: <
const S extends ReadonlyArray<
ReadonlyArray<any>
>
>(
self: S
) => ReadonlyArray.Flatten<S>
Flattens a nested array of arrays into a single array.
When to use
Use to collapse one level of nested arrays when no per-element mapping is
needed.
Example (Flattening nested arrays)
import { Array } from "effect"
console.log(Array.flatten([[1, 2], [], [3, 4], [], [5, 6]])) // [1, 2, 3, 4, 5, 6]
flatten: <const function (type parameter) S in <const S extends ReadonlyArray<ReadonlyArray<any>>>(self: S): ReadonlyArray.Flatten<S>S extends interface ReadonlyArray<T>ReadonlyArray<interface ReadonlyArray<T>ReadonlyArray<any>>>(self: const S extends ReadonlyArray<ReadonlyArray<any>>self: function (type parameter) S in <const S extends ReadonlyArray<ReadonlyArray<any>>>(self: S): ReadonlyArray.Flatten<S>S) => ReadonlyArray.type ReadonlyArray.Flatten<T extends ReadonlyArray<ReadonlyArray<any>>> = T extends readonly [readonly [any, ...any[]], ...(readonly [any, ...any[]])[]] ? [T[number][number], ...T[number][number][]] : T[number][number][]Flattens a nested array type.
Example (Flattening nested array types)
import type { Array } from "effect"
type Nested = ReadonlyArray<ReadonlyArray<number>>
type Flattened = Array.ReadonlyArray.Flatten<Nested>
// Flattened is Array<number>
Flatten<function (type parameter) S in <const S extends ReadonlyArray<ReadonlyArray<any>>>(self: S): ReadonlyArray.Flatten<S>S> =
const flatMap: {
<
S extends ReadonlyArray<any>,
T extends ReadonlyArray<any>
>(
f: (a: ReadonlyArray.Infer<S>, i: number) => T
): (
self: S
) => ReadonlyArray.AndNonEmpty<
S,
T,
ReadonlyArray.Infer<T>
>
<A, B>(
self: NonEmptyReadonlyArray<A>,
f: (
a: A,
i: number
) => NonEmptyReadonlyArray<B>
): NonEmptyArray<B>
<A, B>(
self: ReadonlyArray<A>,
f: (a: A, i: number) => ReadonlyArray<B>
): Array<B>
}
flatMap(const identity: <A>(a: A) => AReturns its input argument unchanged.
When to use
Use to return a value unchanged where a function is required.
Example (Returning the same value)
import { identity } from "effect"
import * as assert from "node:assert"
assert.deepStrictEqual(identity(5), 5)
identity) as any