<B>(that: ReadonlyArray<B>): <A>(self: ReadonlyArray<A>) => Array<[A, B]>
<A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>Computes the cartesian product of two arrays, returning all pairs as tuples.
When to use
Use when you need every [a, b] pair from two arrays as tuples.
Details
Produces every [a, b] combination of an element from self with an element
from that, so the result length is self.length * that.length.
Example (Generating all pairs from two arrays)
import { Array } from "effect"
const result = Array.cartesian([1, 2], ["a", "b"])
console.log(result) // [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]export const const cartesian: {
<B>(that: ReadonlyArray<B>): <A>(
self: ReadonlyArray<A>
) => Array<[A, B]>
<A, B>(
self: ReadonlyArray<A>,
that: ReadonlyArray<B>
): Array<[A, B]>
}
Computes the cartesian product of two arrays, returning all pairs as tuples.
When to use
Use when you need every [a, b] pair from two arrays as tuples.
Details
Produces every [a, b] combination of an element from self with an element
from that, so the result length is self.length * that.length.
Example (Generating all pairs from two arrays)
import { Array } from "effect"
const result = Array.cartesian([1, 2], ["a", "b"])
console.log(result) // [[1, "a"], [1, "b"], [2, "a"], [2, "b"]]
cartesian: {
<function (type parameter) B in <B>(that: ReadonlyArray<B>): <A>(self: ReadonlyArray<A>) => Array<[A, B]>B>(that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <B>(that: ReadonlyArray<B>): <A>(self: ReadonlyArray<A>) => Array<[A, B]>B>): <function (type parameter) A in <A>(self: ReadonlyArray<A>): Array<[A, B]>A>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A>(self: ReadonlyArray<A>): Array<[A, B]>A>) => interface Array<T>Array<[function (type parameter) A in <A>(self: ReadonlyArray<A>): Array<[A, B]>A, function (type parameter) B in <B>(that: ReadonlyArray<B>): <A>(self: ReadonlyArray<A>) => Array<[A, B]>B]>
<function (type parameter) A in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A, function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A>, that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B>): interface Array<T>Array<[function (type parameter) A in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A, function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B]>
} = dual<(...args: Array<any>) => any, <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>) => Array<[A, B]>>(arity: 2, body: <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>) => Array<[A, B]>): ((...args: Array<any>) => any) & (<A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<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: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A, function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A>, that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B>): interface Array<T>Array<[function (type parameter) A in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>A, function (type parameter) B in <A, B>(self: ReadonlyArray<A>, that: ReadonlyArray<B>): Array<[A, B]>B]> => const cartesianWith: <A, B, [A, B]>(self: readonly A[], that: readonly B[], f: (a: A, b: B) => [A, B]) => [A, B][] (+1 overload)cartesianWith(self: readonly A[]self, that: readonly B[]that, (a: Aa, b: Bb) => [a: Aa, b: Bb])
)