effect-io-ai

Package: effect
Module: Iterable

Iterable.take

Keeps only a max number of elements from the start of an Iterable, creating a new Iterable.

Details

n is normalized to a non-negative integer.

Example (Taking from the start)

import { Iterable } from "effect"

const numbers = [1, 2, 3, 4, 5]
const firstThree = Iterable.take(numbers, 3)
console.log(Array.from(firstThree)) // [1, 2, 3]

// Taking more than available returns all elements
const firstTen = Iterable.take(numbers, 10)
console.log(Array.from(firstTen)) // [1, 2, 3, 4, 5]

// Taking 0 or negative returns empty
const none = Iterable.take(numbers, 0)
console.log(Array.from(none)) // []

// Useful with infinite iterables
const naturals = Iterable.range(1)
const firstFive = Iterable.take(naturals, 5)
console.log(Array.from(firstFive)) // [1, 2, 3, 4, 5]

Signature

declare const take: { (n: number): <A>(self: Iterable<A>) => Iterable<A>; <A>(self: Iterable<A>, n: number): Iterable<A>; }

Source

Since v2.0.0