<A>(predicate: (a: A, i: number) => boolean): (
self: Iterable<A>
) => boolean
<A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanChecks whether a predicate holds true for some Iterable element.
Example (Checking whether some element matches)
import { Iterable } from "effect"
const numbers = [1, 3, 5, 7, 8]
const hasEven = Iterable.some(numbers, (x) => x % 2 === 0)
console.log(hasEven) // true (because of 8)
const allOdd = [1, 3, 5, 7]
const hasEvenInAllOdd = Iterable.some(allOdd, (x) => x % 2 === 0)
console.log(hasEvenInAllOdd) // false
// With index
const letters = ["a", "b", "c"]
const hasElementAtIndex2 = Iterable.some(letters, (_, i) => i === 2)
console.log(hasElementAtIndex2) // true
// Early termination - stops at first match
const infiniteOdds = Iterable.filter(Iterable.range(1), (x) => x % 2 === 1)
const hasEvenInInfiniteOdds = Iterable.some(
Iterable.take(infiniteOdds, 1000),
(x) => x % 2 === 0
)
console.log(hasEvenInInfiniteOdds) // false (quickly, doesn't check all 1000)
// Type guard usage
const mixed: Array<string | number> = [1, 2, "hello"]
const hasString = Iterable.some(
mixed,
(x): x is string => typeof x === "string"
)
console.log(hasString) // trueexport const const some: {
<A>(predicate: (a: A, i: number) => boolean): (
self: Iterable<A>
) => boolean
<A>(
self: Iterable<A>,
predicate: (a: A, i: number) => boolean
): boolean
}
Checks whether a predicate holds true for some Iterable element.
Example (Checking whether some element matches)
import { Iterable } from "effect"
const numbers = [1, 3, 5, 7, 8]
const hasEven = Iterable.some(numbers, (x) => x % 2 === 0)
console.log(hasEven) // true (because of 8)
const allOdd = [1, 3, 5, 7]
const hasEvenInAllOdd = Iterable.some(allOdd, (x) => x % 2 === 0)
console.log(hasEvenInAllOdd) // false
// With index
const letters = ["a", "b", "c"]
const hasElementAtIndex2 = Iterable.some(letters, (_, i) => i === 2)
console.log(hasElementAtIndex2) // true
// Early termination - stops at first match
const infiniteOdds = Iterable.filter(Iterable.range(1), (x) => x % 2 === 1)
const hasEvenInInfiniteOdds = Iterable.some(
Iterable.take(infiniteOdds, 1000),
(x) => x % 2 === 0
)
console.log(hasEvenInInfiniteOdds) // false (quickly, doesn't check all 1000)
// Type guard usage
const mixed: Array<string | number> = [1, 2, "hello"]
const hasString = Iterable.some(
mixed,
(x): x is string => typeof x === "string"
)
console.log(hasString) // true
some: {
<function (type parameter) A in <A>(predicate: (a: A, i: number) => boolean): (self: Iterable<A>) => booleanA>(predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(predicate: (a: A, i: number) => boolean): (self: Iterable<A>) => booleanA, i: numberi: number) => boolean): (self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(predicate: (a: A, i: number) => boolean): (self: Iterable<A>) => booleanA>) => boolean
<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA>, predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA, i: numberi: number) => boolean): boolean
} = dual<(...args: Array<any>) => any, <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => boolean>(arity: 2, body: <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => boolean): ((...args: Array<any>) => any) & (<A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean) => 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,
<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA>(self: Iterable<A>self: interface Iterable<T, TReturn = any, TNext = any>Iterable<function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA>, predicate: (a: A, i: number) => booleanpredicate: (a: Aa: function (type parameter) A in <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): booleanA, i: numberi: number) => boolean): boolean => {
let let i: numberi = 0
for (const const a: Aa of self: Iterable<A>self) {
if (predicate: (a: A, i: number) => booleanpredicate(const a: Aa, let i: numberi++)) {
return true
}
}
return false
}
)