<A>(f: (i: number) => A): (n: number) => NonEmptyChunk<A>
<A>(n: number, f: (i: number) => A): NonEmptyChunk<A>Returns a non-empty Chunk of length n with element i initialized by f(i).
Details
n is normalized to an integer greater than or equal to 1.
Example (Generating chunks from indices)
import { Chunk } from "effect"
const chunk = Chunk.makeBy(5, (i) => i * 2)
console.log(Chunk.toArray(chunk)) // [0, 2, 4, 6, 8]export const const makeBy: {
<A>(f: (i: number) => A): (
n: number
) => NonEmptyChunk<A>
<A>(
n: number,
f: (i: number) => A
): NonEmptyChunk<A>
}
Returns a non-empty Chunk of length n with element i initialized by f(i).
Details
n is normalized to an integer greater than or equal to 1.
Example (Generating chunks from indices)
import { Chunk } from "effect"
const chunk = Chunk.makeBy(5, (i) => i * 2)
console.log(Chunk.toArray(chunk)) // [0, 2, 4, 6, 8]
makeBy: {
<function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyChunk<A>A>(f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyChunk<A>A): (n: numbern: number) => interface NonEmptyChunk<out A>A non-empty Chunk guaranteed to contain at least one element.
Example (Working with non-empty chunks)
import { Chunk } from "effect"
const nonEmptyChunk: Chunk.NonEmptyChunk<number> = Chunk.make(1, 2, 3)
console.log(Chunk.headNonEmpty(nonEmptyChunk)) // 1
console.log(Chunk.lastNonEmpty(nonEmptyChunk)) // 3
NonEmptyChunk<function (type parameter) A in <A>(f: (i: number) => A): (n: number) => NonEmptyChunk<A>A>
<function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyChunk<A>A>(n: numbern: number, f: (i: number) => Af: (i: numberi: number) => function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyChunk<A>A): interface NonEmptyChunk<out A>A non-empty Chunk guaranteed to contain at least one element.
Example (Working with non-empty chunks)
import { Chunk } from "effect"
const nonEmptyChunk: Chunk.NonEmptyChunk<number> = Chunk.make(1, 2, 3)
console.log(Chunk.headNonEmpty(nonEmptyChunk)) // 1
console.log(Chunk.lastNonEmpty(nonEmptyChunk)) // 3
NonEmptyChunk<function (type parameter) A in <A>(n: number, f: (i: number) => A): NonEmptyChunk<A>A>
} = dual<(...args: Array<any>) => any, (n: any, f: any) => Chunk<unknown>>(arity: 2, body: (n: any, f: any) => Chunk<unknown>): ((...args: Array<any>) => any) & ((n: any, f: any) => Chunk<unknown>) (+1 overload)Creates a function that can be called in data-first style or data-last
(pipe-friendly) style.
When to use
Use to expose one implementation through both direct and pipe-friendly
call styles.
Details
Pass either the arity of the uncurried function or a predicate that decides
whether the current call is data-first. Arity is the common case. Use a
predicate when optional arguments make arity ambiguous.
Example (Selecting data-first or data-last style by arity)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(2, (self, that) => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Defining overloads with call signatures)
import { Function, pipe } from "effect"
const sum: {
(that: number): (self: number) => number
(self: number, that: number): number
} = Function.dual(2, (self: number, that: number): number => self + that)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
Example (Selecting data-first or data-last style with a predicate)
import { Function, pipe } from "effect"
const sum = Function.dual<
(that: number) => (self: number) => number,
(self: number, that: number) => number
>(
(args) => args.length === 2,
(self, that) => self + that
)
console.log(sum(2, 3)) // 5
console.log(pipe(2, sum(3))) // 5
dual(2, (n: anyn, f: anyf) => const fromIterable: <A>(
self: Iterable<A>
) => Chunk<A>
Creates a new Chunk from an iterable collection of values.
Example (Creating chunks from iterables)
import { Chunk } from "effect"
const chunk = Chunk.fromIterable([1, 2, 3])
console.log(Chunk.toArray(chunk)) // [1, 2, 3]
fromIterable(import RARA.makeBy(n: anyn, f: anyf)))