<B>(last: B): <A>(self: Iterable<A>) => NonEmptyArray<A | B>
<A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>Adds a single element to the end of an iterable, returning a NonEmptyArray.
When to use
Use when you need to guarantee a non-empty result after adding a required trailing value.
Example (Appending an element)
import { Array } from "effect"
const result = Array.append([1, 2, 3], 4)
console.log(result) // [1, 2, 3, 4]export const const append: {
<B>(last: B): <A>(
self: Iterable<A>
) => NonEmptyArray<A | B>
<A, B>(
self: Iterable<A>,
last: B
): NonEmptyArray<A | B>
}
Adds a single element to the end of an iterable, returning a NonEmptyArray.
When to use
Use when you need to guarantee a non-empty result after adding a required
trailing value.
Example (Appending an element)
import { Array } from "effect"
const result = Array.append([1, 2, 3], 4)
console.log(result) // [1, 2, 3, 4]
append: {
<function (type parameter) B in <B>(last: B): <A>(self: Iterable<A>) => NonEmptyArray<A | B>B>(last: Blast: function (type parameter) B in <B>(last: B): <A>(self: Iterable<A>) => NonEmptyArray<A | B>B): <function (type parameter) A in <A>(self: Iterable<A>): NonEmptyArray<A | B>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>): NonEmptyArray<A | B>A>) => type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A>(self: Iterable<A>): NonEmptyArray<A | B>A | function (type parameter) B in <B>(last: B): <A>(self: Iterable<A>) => NonEmptyArray<A | B>B>
<function (type parameter) A in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>A, function (type parameter) B in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>A>, last: Blast: function (type parameter) B in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>B): type NonEmptyArray<A> = [A, ...A[]]A mutable array guaranteed to have at least one element.
When to use
Use when mutation is acceptable and non-emptiness must be tracked at the type
level.
Details
This is the mutable counterpart of
NonEmptyReadonlyArray
. Most Array
module functions return NonEmptyArray when the result is guaranteed
non-empty.
Example (Typing a mutable non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyArray<number> = [1, 2, 3]
nonEmpty.push(4)
NonEmptyArray<function (type parameter) A in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>A | function (type parameter) B in <A, B>(self: Iterable<A>, last: B): NonEmptyArray<A | B>B>
} = dual<(...args: Array<any>) => any, <A, B>(self: Iterable<A>, last: B) => Array<A | B>>(arity: 2, body: <A, B>(self: Iterable<A>, last: B) => Array<A | B>): ((...args: Array<any>) => any) & (<A, B>(self: Iterable<A>, last: B) => Array<A | B>) (+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, <function (type parameter) A in <A, B>(self: Iterable<A>, last: B): Array<A | B>A, function (type parameter) B in <A, B>(self: Iterable<A>, last: B): Array<A | B>B>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B>(self: Iterable<A>, last: B): Array<A | B>A>, last: Blast: function (type parameter) B in <A, B>(self: Iterable<A>, last: B): Array<A | B>B): interface Array<T>Array<function (type parameter) A in <A, B>(self: Iterable<A>, last: B): Array<A | B>A | function (type parameter) B in <A, B>(self: Iterable<A>, last: B): Array<A | B>B> => [...self: Iterable<A>self, last: Blast])