<S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<
infer A
>
? NonEmptyArray<A>
: S extends Iterable<infer A>
? Array<A>
: neverRemoves duplicates using Equal.equivalence(), preserving the order of the
first occurrence.
When to use
Use to remove repeated values from an iterable when Effect's default equality is the right comparison, preserving the first occurrence.
Example (Removing duplicates)
import { Array } from "effect"
console.log(Array.dedupe([1, 2, 1, 3, 2, 4])) // [1, 2, 3, 4]export const const dedupe: <S extends Iterable<any>>(
self: S
) => S extends NonEmptyReadonlyArray<infer A>
? NonEmptyArray<A>
: S extends Iterable<infer A>
? Array<A>
: never
Removes duplicates using Equal.equivalence(), preserving the order of the
first occurrence.
When to use
Use to remove repeated values from an iterable when Effect's default equality
is the right comparison, preserving the first occurrence.
Example (Removing duplicates)
import { Array } from "effect"
console.log(Array.dedupe([1, 2, 1, 3, 2, 4])) // [1, 2, 3, 4]
dedupe = <function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends interface Iterable<T, TReturn = any, TNext = any>Iterable<any>>(
self: S extends Iterable<any>self: function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS
): function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends type NonEmptyReadonlyArray<A> = readonly [
A,
...A[]
]
A readonly array guaranteed to have at least one element.
When to use
Use when non-emptiness must be tracked at the type level while preventing mutation.
Many Array module functions accept or return this type.
Example (Typing a non-empty array)
import type { Array } from "effect"
const nonEmpty: Array.NonEmptyReadonlyArray<number> = [1, 2, 3]
const head: number = nonEmpty[0] // guaranteed to exist
NonEmptyReadonlyArray<infer function (type parameter) AA> ? 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) AA> : function (type parameter) S in <S extends Iterable<any>>(self: S): S extends NonEmptyReadonlyArray<infer A> ? NonEmptyArray<A> : S extends Iterable<infer A> ? Array<A> : neverS extends interface Iterable<T, TReturn = any, TNext = any>Iterable<infer function (type parameter) AA> ? interface Array<T>Array<function (type parameter) AA> : never =>
const dedupeWith: {
<S extends Iterable<any>>(
isEquivalent: (
self: ReadonlyArray.Infer<S>,
that: ReadonlyArray.Infer<S>
) => boolean
): (
self: S
) => ReadonlyArray.With<
S,
ReadonlyArray.Infer<S>
>
<A>(
self: NonEmptyReadonlyArray<A>,
isEquivalent: (self: A, that: A) => boolean
): NonEmptyArray<A>
<A>(
self: Iterable<A>,
isEquivalent: (self: A, that: A) => boolean
): Array<A>
}
dedupeWith(self: S extends Iterable<any>self, import EqualEqual.const asEquivalence: <
A
>() => Equivalence<A>
Wraps
equals
as an Equivalence<A>.
When to use
Use when you want to pass Equal.equals to APIs that require an
Equivalence.
Details
- Returns a function
(a: A, b: A) => boolean that delegates to
equals
.
- Pure; allocates a thin wrapper on each call.
Example (Deduplicating with Equal semantics)
import { Array, Equal } from "effect"
const eq = Equal.asEquivalence<number>()
const result = Array.dedupeWith([1, 2, 2, 3, 1], eq)
console.log(result) // [1, 2, 3]
asEquivalence()) as any