Hyperlinkv0.8.0-beta.28

Array

Array.cartesianWithconsteffect/Array.ts:4616
<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"]
elementscartesian
Source effect/Array.ts:46168 lines
export 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>
} = dual(
  3,
  <A, B, C>(self: ReadonlyArray<A>, that: ReadonlyArray<B>, f: (a: A, b: B) => C): Array<C> =>
    flatMap(self, (a) => map(that, (b) => f(a, b)))
)
Referenced by 1 symbols