<A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>
(self: Iterable<A>, that: Iterable<A>): Array<A>
}Computes the intersection of two arrays using a custom equivalence. Order is determined by the first array.
When to use
Use when you need to keep only values present in both arrays and equality must be defined by a custom comparator, such as matching objects by id.
Example (Computing intersections with custom equality)
import { Array } from "effect"
const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
const array2 = [{ id: 3 }, { id: 4 }, { id: 1 }]
const isEquivalent = (a: { id: number }, b: { id: number }) => a.id === b.id
console.log(Array.intersectionWith(isEquivalent)(array2)(array1)) // [{ id: 1 }, { id: 3 }]export const const intersectionWith: <A>(
isEquivalent: (self: A, that: A) => boolean
) => {
(that: Iterable<A>): (
self: Iterable<A>
) => Array<A>
(self: Iterable<A>, that: Iterable<A>): Array<A>
}
Computes the intersection of two arrays using a custom equivalence. Order is
determined by the first array.
When to use
Use when you need to keep only values present in both arrays and equality
must be defined by a custom comparator, such as matching objects by id.
Example (Computing intersections with custom equality)
import { Array } from "effect"
const array1 = [{ id: 1 }, { id: 2 }, { id: 3 }]
const array2 = [{ id: 3 }, { id: 4 }, { id: 1 }]
const isEquivalent = (a: { id: number }, b: { id: number }) => a.id === b.id
console.log(Array.intersectionWith(isEquivalent)(array2)(array1)) // [{ id: 1 }, { id: 3 }]
intersectionWith = <function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A>(isEquivalent: (self: A, that: A) => booleanisEquivalent: (self: Aself: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A, that: Athat: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A) => boolean): {
(that: Iterable<A>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<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>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A>) => interface Array<T>Array<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<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>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<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>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A>): interface Array<T>Array<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A>
} => {
const const has: {
(a: A): (self: Iterable<A>) => boolean
(self: Iterable<A>, a: A): boolean
}
has = const containsWith: <A>(
isEquivalent: (self: A, that: A) => boolean
) => {
(a: A): (self: Iterable<A>) => boolean
(self: Iterable<A>, a: A): boolean
}
Returns a membership-test function using a custom equivalence.
When to use
Use when checking membership with caller-provided equality instead of
Equal.equivalence().
Example (Checking with custom equality)
import { Array, pipe } from "effect"
const containsNumber = Array.containsWith((a: number, b: number) => a === b)
console.log(pipe([1, 2, 3, 4], containsNumber(3))) // true
containsWith(isEquivalent: (self: A, that: A) => booleanisEquivalent)
return dual<(...args: Array<any>) => any, (self: Iterable<A>, that: Iterable<A>) => Array<A>>(arity: 2, body: (self: Iterable<A>, that: Iterable<A>) => Array<A>): ((...args: Array<any>) => any) & ((self: Iterable<A>, that: Iterable<A>) => Array<A>) (+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,
(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<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>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A>): interface Array<T>Array<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(that: Iterable<A>): (self: Iterable<A>) => Array<A>;
(self: Iterable<A>, that: Iterable<A>): Array<A>;
}
A> => {
const const thatArray: A[]thatArray = const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(that: Iterable<A>that)
return const fromIterable: <A>(
collection: Iterable<A>
) => A[]
Converts an Iterable to an Array.
When to use
Use to convert any Iterable (Set, Generator, etc.) into an array.
Details
If the input is already an array, this returns it by reference without
copying. Otherwise, it creates a new array from the iterable. Use copy if
you need a fresh array even when the input is already an array.
Example (Converting a Set to an array)
import { Array } from "effect"
const result = Array.fromIterable(new Set([1, 2, 3]))
console.log(result) // [1, 2, 3]
fromIterable(self: Iterable<A>self).Array<A>.filter(predicate: (value: A, index: number, array: A[]) => unknown, thisArg?: any): A[] (+1 overload)Returns the elements of an array that meet the condition specified in a callback function.
filter((a: Aa) => const has: (self: Iterable<A>, a: A) => boolean (+1 overload)has(const thatArray: A[]thatArray, a: Aa))
}
)
}