Hyperlinkv0.8.0-beta.28

Iterable

Iterable.someconsteffect/Iterable.ts:2043
<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
elements
export const some: {
  <A>(predicate: (a: A, i: number) => boolean): (self: Iterable<A>) => boolean
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): boolean
} = dual(
  2,
  <A>(self: Iterable<A>, predicate: (a: A, i: number) => boolean): boolean => {
    let i = 0
    for (const a of self) {
      if (predicate(a, i++)) {
        return true
      }
    }
    return false
  }
)