Hyperlinkv0.8.0-beta.28

Iterable

Iterable.dropconsteffect/Iterable.ts:693
(n: number): <A>(self: Iterable<A>) => Iterable<A>
<A>(self: Iterable<A>, n: number): Iterable<A>

Drops a max number of elements from the start of an Iterable

Details

n is normalized to a non-negative integer.

Example (Dropping from the start)

import { Iterable } from "effect"

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

// Dropping more than available returns empty
const withoutFirstTen = Iterable.drop(numbers, 10)
console.log(Array.from(withoutFirstTen)) // []

// Dropping 0 or negative returns all elements
const all = Iterable.drop(numbers, 0)
console.log(Array.from(all)) // [1, 2, 3, 4, 5]

// Combine with take for slicing
const slice = Iterable.take(Iterable.drop(numbers, 1), 3)
console.log(Array.from(slice)) // [2, 3, 4]
getters
Source effect/Iterable.ts:69321 lines
export const drop: {
  (n: number): <A>(self: Iterable<A>) => Iterable<A>
  <A>(self: Iterable<A>, n: number): Iterable<A>
} = dual(2, <A>(self: Iterable<A>, n: number): Iterable<A> => ({
  [Symbol.iterator]() {
    const iterator = self[Symbol.iterator]()
    let i = 0
    return {
      next() {
        while (i < n) {
          const result = iterator.next()
          if (result.done) {
            return { done: true, value: undefined }
          }
          i++
        }
        return iterator.next()
      }
    }
  }
}))