Hyperlinkv0.8.0-beta.28

Iterable

Iterable.headUnsafeconsteffect/Iterable.ts:546
<A>(self: Iterable<A>): A

Gets the first element of an Iterable without returning an Option.

When to use

Use when the Iterable is known to be non-empty and direct access to the first element is preferred over handling Option.none.

Gotchas

Throws if the Iterable is empty.

Example (Getting the first element unsafely)

import { Iterable } from "effect"

const numbers = [1, 2, 3]
console.log(Iterable.headUnsafe(numbers)) // 1

const letters = "hello"
console.log(Iterable.headUnsafe(letters)) // "h"

// Iterable.headUnsafe(Iterable.empty<number>())
// throws Error: "headUnsafe: empty iterable"

// Use only when you're certain the iterable is non-empty
const nonEmpty = Iterable.range(1, 10)
console.log(Iterable.headUnsafe(nonEmpty)) // 1
getters
export const headUnsafe = <A>(self: Iterable<A>): A => {
  const iterator = self[Symbol.iterator]()
  const result = iterator.next()
  if (result.done) throw new Error("headUnsafe: empty iterable")
  return result.value
}
Referenced by 3 symbols