<A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean
(self: Iterable<A>, a: A): boolean
}Returns a function that checks if an Iterable contains a given value using a provided isEquivalent function.
Example (Checking membership with custom equivalence)
import { Iterable } from "effect"
// Custom equivalence for objects
const byId = (a: { id: number }, b: { id: number }) => a.id === b.id
const containsById = Iterable.containsWith(byId)
const users = [{ id: 1 }, { id: 2 }]
const hasUser1 = containsById(users, { id: 1 })
console.log(hasUser1) // true (same id)
// Case-insensitive string comparison
const caseInsensitive = (a: string, b: string) =>
a.toLowerCase() === b.toLowerCase()
const containsCaseInsensitive = Iterable.containsWith(caseInsensitive)
const words = ["Hello", "World"]
const hasHello = containsCaseInsensitive(words, "hello")
console.log(hasHello) // true
// Approximate number comparison
const approxEqual = (a: number, b: number) => Math.abs(a - b) < 0.1
const containsApprox = Iterable.containsWith(approxEqual)
const values = [1.0, 2.0, 3.0]
const hasAlmostTwo = containsApprox(values, 2.05)
console.log(hasAlmostTwo) // trueexport const const containsWith: <A>(
isEquivalent: (self: A, that: A) => boolean
) => {
(a: A): (self: Iterable<A>) => boolean
(self: Iterable<A>, a: A): boolean
}
Returns a function that checks if an Iterable contains a given value using a provided isEquivalent function.
Example (Checking membership with custom equivalence)
import { Iterable } from "effect"
// Custom equivalence for objects
const byId = (a: { id: number }, b: { id: number }) => a.id === b.id
const containsById = Iterable.containsWith(byId)
const users = [{ id: 1 }, { id: 2 }]
const hasUser1 = containsById(users, { id: 1 })
console.log(hasUser1) // true (same id)
// Case-insensitive string comparison
const caseInsensitive = (a: string, b: string) =>
a.toLowerCase() === b.toLowerCase()
const containsCaseInsensitive = Iterable.containsWith(caseInsensitive)
const words = ["Hello", "World"]
const hasHello = containsCaseInsensitive(words, "hello")
console.log(hasHello) // true
// Approximate number comparison
const approxEqual = (a: number, b: number) => Math.abs(a - b) < 0.1
const containsApprox = Iterable.containsWith(approxEqual)
const values = [1.0, 2.0, 3.0]
const hasAlmostTwo = containsApprox(values, 2.05)
console.log(hasAlmostTwo) // true
containsWith = <function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A>(isEquivalent: (self: A, that: A) => booleanisEquivalent: (self: Aself: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A, that: Athat: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A) => boolean): {
(a: Aa: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
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): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A>) => boolean
(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A>, a: Aa: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A): boolean
} =>
dual<(...args: Array<any>) => any, (self: Iterable<A>, a: A) => boolean>(arity: 2, body: (self: Iterable<A>, a: A) => boolean): ((...args: Array<any>) => any) & ((self: Iterable<A>, a: A) => boolean) (+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): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A>, a: Aa: function (type parameter) A in <A>(isEquivalent: (self: A, that: A) => boolean): {
(a: A): (self: Iterable<A>) => boolean;
(self: Iterable<A>, a: A): boolean;
}
A): boolean => {
for (const const i: Ai of self: Iterable<A>self) {
if (isEquivalent: (self: A, that: A) => booleanisEquivalent(a: Aa, const i: Ai)) {
return true
}
}
return false
})