<A>(that: Iterable<A>): (self: Iterable<A>) => Array<A>
<A>(self: Iterable<A>, that: Iterable<A>): Array<A>Computes elements in the first array that are not in the second, using
Equal.equivalence().
When to use
Use when you need to keep values from the first array that are absent from
the second and the default Equal.equivalence() comparison is appropriate.
Example (Computing array differences)
import { Array } from "effect"
console.log(Array.difference([1, 2, 3], [2, 3, 4])) // [1]export const const difference: {
<A>(that: Iterable<A>): (
self: Iterable<A>
) => Array<A>
<A>(
self: Iterable<A>,
that: Iterable<A>
): Array<A>
}
Computes elements in the first array that are not in the second, using
Equal.equivalence().
When to use
Use when you need to keep values from the first array that are absent from
the second and the default Equal.equivalence() comparison is appropriate.
Example (Computing array differences)
import { Array } from "effect"
console.log(Array.difference([1, 2, 3], [2, 3, 4])) // [1]
difference: {
<function (type parameter) A in <A>(that: Iterable<A>): (self: Iterable<A>) => Array<A>A>(that: Iterable<A>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(that: Iterable<A>): (self: Iterable<A>) => Array<A>A>): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(that: Iterable<A>): (self: Iterable<A>) => Array<A>A>) => interface Array<T>Array<function (type parameter) A in <A>(that: Iterable<A>): (self: Iterable<A>) => Array<A>A>
<function (type parameter) A in <A>(self: Iterable<A>, that: Iterable<A>): Array<A>A>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, that: Iterable<A>): Array<A>A>, that: Iterable<A>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, that: Iterable<A>): Array<A>A>): interface Array<T>Array<function (type parameter) A in <A>(self: Iterable<A>, that: Iterable<A>): Array<A>A>
} = const differenceWith: <unknown>(
isEquivalent: (
self: unknown,
that: unknown
) => boolean
) => {
(that: Iterable<unknown>): (
self: Iterable<unknown>
) => unknown[]
(
self: Iterable<unknown>,
that: Iterable<unknown>
): unknown[]
}
Computes elements in the first array that are not in the second, using a
custom equivalence.
When to use
Use when you need to keep only values from the first array and equality must
be defined by a custom comparator, such as matching objects by id.
Example (Computing differences with custom equality)
import { Array } from "effect"
const diff = Array.differenceWith<number>((a, b) => a === b)([1, 2, 3], [2, 3, 4])
console.log(diff) // [1]
differenceWith(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())