<A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (
self: ReadonlyArray<A>
) => Array<C>
<A, B, C>(
self: ReadonlyArray<A>,
that: ReadonlyArray<B>,
f: (a: A, b: B) => C
): Array<C>Computes the cartesian product of two arrays, applying a combiner to each pair.
When to use
Use to compute every combination from two arrays and immediately transform each pair into a custom result.
Details
Produces every combination of an element from self with an element from
that, so the result length is self.length * that.length. Iteration visits
every element of that for each element of self.
Example (Combining numbers and letters)
import { Array } from "effect"
const result = Array.cartesianWith([1, 2], ["a", "b"], (a, b) => `${a}-${b}`)
console.log(result) // ["1-a", "1-b", "2-a", "2-b"]export const const cartesianWith: {
<A, B, C>(
that: ReadonlyArray<B>,
f: (a: A, b: B) => C
): (self: ReadonlyArray<A>) => Array<C>
<A, B, C>(
self: ReadonlyArray<A>,
that: ReadonlyArray<B>,
f: (a: A, b: B) => C
): Array<C>
}
Computes the cartesian product of two arrays, applying a combiner to each pair.
When to use
Use to compute every combination from two arrays and immediately transform
each pair into a custom result.
Details
Produces every combination of an element from self with an element from
that, so the result length is self.length * that.length. Iteration visits
every element of that for each element of self.
Example (Combining numbers and letters)
import { Array } from "effect"
const result = Array.cartesianWith([1, 2], ["a", "b"], (a, b) => `${a}-${b}`)
console.log(result) // ["1-a", "1-b", "2-a", "2-b"]
cartesianWith: {
<function (type parameter) A in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>A, function (type parameter) B in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>B, function (type parameter) C in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>C>(that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>A, b: Bb: function (type parameter) B in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>B) => function (type parameter) C in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>C): (self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>A>) => interface Array<T>Array<function (type parameter) C in <A, B, C>(that: ReadonlyArray<B>, f: (a: A, b: B) => C): (self: ReadonlyArray<A>) => Array<C>C>
<function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A, function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B, function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A>, that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A, b: Bb: function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B) => function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C): interface Array<T>Array<function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C>
} = dual<(...args: Array<any>) => any, <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C) => Array<C>>(arity: 3, body: <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C) => Array<C>): ((...args: Array<any>) => any) & (<A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C) => Array<C>) (+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(
3,
<function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A, function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B, function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C>(self: readonly A[]self: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A>, that: readonly B[]that: interface ReadonlyArray<T>ReadonlyArray<function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>A, b: Bb: function (type parameter) B in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>B) => function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C): interface Array<T>Array<function (type parameter) C in <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C>C> =>
const flatMap: {
<
S extends ReadonlyArray<any>,
T extends ReadonlyArray<any>
>(
f: (a: ReadonlyArray.Infer<S>, i: number) => T
): (
self: S
) => ReadonlyArray.AndNonEmpty<
S,
T,
ReadonlyArray.Infer<T>
>
<A, B>(
self: NonEmptyReadonlyArray<A>,
f: (
a: A,
i: number
) => NonEmptyReadonlyArray<B>
): NonEmptyArray<B>
<A, B>(
self: ReadonlyArray<A>,
f: (a: A, i: number) => ReadonlyArray<B>
): Array<B>
}
flatMap(self: readonly A[]self, (a: Aa) => const map: <readonly B[], C>(self: readonly B[], f: (a: B, i: number) => C) => C[] (+1 overload)map(that: readonly B[]that, (b: Bb) => f: (a: A, b: B) => Cf(a: Aa, b: Bb)))
)