<A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (
self: Iterable<A>
) => Iterable<C>
<A, B, C>(
self: Iterable<A>,
that: Iterable<B>,
f: (a: A, b: B) => C
): Iterable<C>Zips this Iterable crosswise with the specified Iterable using the specified combiner.
Example (Combining cartesian products)
import { Iterable } from "effect"
// Create coordinate pairs
const xs = [1, 2]
const ys = ["a", "b", "c"]
const coordinates = Iterable.cartesianWith(xs, ys, (x, y) => `(${x},${y})`)
console.log(Array.from(coordinates)) // ["(1,a)", "(1,b)", "(1,c)", "(2,a)", "(2,b)", "(2,c)"]
// Generate all combinations of options
const sizes = ["S", "M", "L"]
const colors = ["red", "blue"]
const products = Iterable.cartesianWith(
sizes,
colors,
(size, color) => ({ size, color })
)
console.log(Array.from(products))
// [
// { size: "S", color: "red" }, { size: "S", color: "blue" },
// { size: "M", color: "red" }, { size: "M", color: "blue" },
// { size: "L", color: "red" }, { size: "L", color: "blue" }
// ]
// Mathematical operations on all pairs
const a = [1, 2, 3]
const b = [10, 20]
const mathProducts = Iterable.cartesianWith(a, b, (x, y) => x * y)
console.log(Array.from(mathProducts)) // [10, 20, 20, 40, 30, 60]
// Create test data combinations
const userTypes = ["admin", "user"]
const features = ["read", "write", "delete"]
const testCases = Iterable.cartesianWith(
userTypes,
features,
(user, feature) => `${user}_can_${feature}`
)
console.log(Array.from(testCases))
// ["admin_can_read", "admin_can_write", "admin_can_delete", "user_can_read", "user_can_write", "user_can_delete"]export const const cartesianWith: {
<A, B, C>(
that: Iterable<B>,
f: (a: A, b: B) => C
): (self: Iterable<A>) => Iterable<C>
<A, B, C>(
self: Iterable<A>,
that: Iterable<B>,
f: (a: A, b: B) => C
): Iterable<C>
}
Zips this Iterable crosswise with the specified Iterable using the specified combiner.
Example (Combining cartesian products)
import { Iterable } from "effect"
// Create coordinate pairs
const xs = [1, 2]
const ys = ["a", "b", "c"]
const coordinates = Iterable.cartesianWith(xs, ys, (x, y) => `(${x},${y})`)
console.log(Array.from(coordinates)) // ["(1,a)", "(1,b)", "(1,c)", "(2,a)", "(2,b)", "(2,c)"]
// Generate all combinations of options
const sizes = ["S", "M", "L"]
const colors = ["red", "blue"]
const products = Iterable.cartesianWith(
sizes,
colors,
(size, color) => ({ size, color })
)
console.log(Array.from(products))
// [
// { size: "S", color: "red" }, { size: "S", color: "blue" },
// { size: "M", color: "red" }, { size: "M", color: "blue" },
// { size: "L", color: "red" }, { size: "L", color: "blue" }
// ]
// Mathematical operations on all pairs
const a = [1, 2, 3]
const b = [10, 20]
const mathProducts = Iterable.cartesianWith(a, b, (x, y) => x * y)
console.log(Array.from(mathProducts)) // [10, 20, 20, 40, 30, 60]
// Create test data combinations
const userTypes = ["admin", "user"]
const features = ["read", "write", "delete"]
const testCases = Iterable.cartesianWith(
userTypes,
features,
(user, feature) => `${user}_can_${feature}`
)
console.log(Array.from(testCases))
// ["admin_can_read", "admin_can_write", "admin_can_delete", "user_can_read", "user_can_write", "user_can_delete"]
cartesianWith: {
<function (type parameter) A in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>A, function (type parameter) B in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>B, function (type parameter) C in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>C>(that: Iterable<B>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>A, b: Bb: function (type parameter) B in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>B) => function (type parameter) C in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>C): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>A>) => interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) C in <A, B, C>(that: Iterable<B>, f: (a: A, b: B) => C): (self: Iterable<A>) => Iterable<C>C>
<function (type parameter) A in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A, function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B, function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A>, that: Iterable<B>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A, b: Bb: function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B) => function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C>
} = dual<(...args: Array<any>) => any, <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C) => Iterable<C>>(arity: 3, body: <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C) => Iterable<C>): ((...args: Array<any>) => any) & (<A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C) => Iterable<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: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A, function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B, function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A>, that: Iterable<B>that: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B>, f: (a: A, b: B) => Cf: (a: Aa: function (type parameter) A in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>A, b: Bb: function (type parameter) B in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>B) => function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C): interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) C in <A, B, C>(self: Iterable<A>, that: Iterable<B>, f: (a: A, b: B) => C): Iterable<C>C> =>
const flatMap: {
<A, B>(
f: (a: NoInfer<A>, i: number) => Iterable<B>
): (self: Iterable<A>) => Iterable<B>
<A, B>(
self: Iterable<A>,
f: (a: NoInfer<A>, i: number) => Iterable<B>
): Iterable<B>
}
flatMap(self: Iterable<A>self, (a: NoInfer<A>a) => const map: {
<A, B>(f: (a: NoInfer<A>, i: number) => B): (
self: Iterable<A>
) => Iterable<B>
<A, B>(
self: Iterable<A>,
f: (a: NoInfer<A>, i: number) => B
): Iterable<B>
}
map(that: Iterable<B>that, (b: NoInfer<B>b) => f: (a: A, b: B) => Cf(a: NoInfer<A>a, b: NoInfer<B>b)))
)