RobinMalfait / lazy-collections

Collection of fast and lazy operations
MIT License
407 stars 24 forks source link

Add `at` feature #30

Closed RobinMalfait closed 1 year ago

RobinMalfait commented 1 year ago

This PR adds a new at feature as a continuation of the existing PR #25.

Returns the value at the given index.

import { pipe, at } from 'lazy-collections'

let program = pipe(at(2))

program([1, 2, 3, 4])

// 3

You can also pass a negative index to at to count back from the end of the array or iterator.

Warning: Performance may be degraded because it has to exhaust the full iterator before it can count backwards!

import { pipe, at } from 'lazy-collections'

let program = pipe(at(-2))

program([1, 2, 3, 4])

// 3

If a value can not be found at the given index, then undefined will be returned.

import { pipe, at } from 'lazy-collections'

let program = pipe(at(12))

program([1, 2, 3, 4])

// undefined

Closes: #25 Thanks, @AlexVipond!