gleam/iterator

Types

Iterator

An iterator is a lazily evaluated sequence of element.

Iterators are useful when working with collections that are too large to fit in memory (or those that are infinite in size) as they only require the elements currently being processed to be in memory.

As a lazy data structure no work is done when an iterator is filters, mapped, etc, instead a new iterator is returned with these transformations applied to the stream. Once the stream has all the required transformations applied it can be evaluated using functions such as fold and take.

pub opaque type Iterator(element)

Step

pub type Step(element, accumulator) {
  Next(element: element, accumulator: accumulator)
  Done
}

Constructors

  • Next(element: element, accumulator: accumulator)
  • Done

Functions

cycle

pub fn cycle(iterator: Iterator(a)) -> Iterator(a)

Create an iterator that repeats a given iterator infinitely.

Examples

> [1, 2] |> from_list |> cycle |> take(6)
[1, 2, 1, 2, 1, 2]

drop

pub fn drop(
  from iterator: Iterator(a),
  up_to desired: Int,
) -> Iterator(a)

Evaluate and discard the first N elements in an iterator, returning a new iterator.

If the iterator does not have enough elements an empty iterator is returned.

This function does not evaluate the elements of the iterator, the computation is performed when the iterator is later run.

Examples

> [1, 2, 3, 4, 5] |> from_list |> drop(up_to: 3) |> to_list
[4, 5]

> [1, 2] |> from_list |> drop(up_to: 3) |> to_list
[]

filter

pub fn filter(
  iterator: Iterator(a),
  for predicate: fn(a) -> Bool,
) -> Iterator(a)

Create an iterator from an existing iterator and a predicate function.

The new iterator will contain elements from the first iterator for which the given function returns True.

This function does not evaluate the elements of the iterator, the computation is performed when the iterator is later run.

Examples

> import gleam/int
> [1, 2, 3, 4] |> from_list |> filter(int.is_even) |> to_list
[2, 4]

fold

pub fn fold(
  over iterator: Iterator(a),
  from initial: b,
  with f: fn(a, b) -> b,
) -> b

Reduce an iterator of elements into a single value by calling a given function on each element in turn.

If called on an iterator of infinite length then this function will never return.

If you do not care about the end value and only wish to evaluate the iterator for side effects consider using the run function instead.

Examples

> [1, 2, 3, 4]
> |> from_list
> |> fold(from: 0, with: fn(element, acc) { element + acc })
10

from_list

pub fn from_list(list: List(a)) -> Iterator(a)

Create an iterator the yields each element in a given list.

Examples

> from_list([1, 2, 3, 4]) |> to_list
[1, 2, 3, 4]

map

pub fn map(
  over iterator: Iterator(a),
  with f: fn(a) -> b,
) -> Iterator(b)

Create an iterator from an existing iterator and a transformation function.

Each element in the new iterator will be the result of calling the given function on the elements in the given iterator.

This function does not evaluate the elements of the iterator, the computation is performed when the iterator is later run.

Examples

> [1, 2, 3] |> from_list |> map(fn(x) { x * 2 }) |> to_list
[2, 4, 6]

range

pub fn range(from start: Int, to stop: Int) -> Iterator(Int)

Create an iterator of ints, starting at a given start int and stepping by one to a given end int.

Examples

> range(from: 1, to: 5) |> to_list
[1, 2, 3, 4]

> range(from: 1, to: -2) |> to_list
[1, 0, -1]

> range(from: 0, to: 0) |> to_list
[]

repeat

pub fn repeat(x: a) -> Iterator(a)

Create an iterator that returns the same value infinitely.

Examples

> repeat(10)
> |> take(4)
> |> to_list
[10, 10, 10, 10]

repeatedly

pub fn repeatedly(f: fn() -> a) -> Iterator(a)

Create an iterator that yields values created by calling a given function repeatedly.

run

pub fn run(iterator: Iterator(a)) -> Nil

Evaluate all elements in a given stream. This function is useful for when you wish to trigger any side effects that would occur when evaluating the iterator.

take

pub fn take(
  from iterator: Iterator(a),
  up_to desired: Int,
) -> List(a)

Evaluate a desired number of elements from an iterator and return them in a list.

If the iterator does not have enough elements all of them are returned.

Examples

> [1, 2, 3, 4, 5] |> from_list |> take(up_to: 3)
[1, 2, 3]

> [1, 2] |> from_list |> take(up_to: 3)
[1, 2]

to_list

pub fn to_list(iterator: Iterator(a)) -> List(a)

Evaluate an iterator and return all the elements as a list.

If called on an iterator of infinite length then this function will never return.

Examples

[1, 2, 3] |> from_list |> map(fn(x) { x * 2 }) |> to_list [2, 4, 6]

unfold

pub fn unfold(
  from initial: a,
  with f: fn(a) -> Step(b, a),
) -> Iterator(b)

Create an iterator from a given function and accumulator.

The function is called on the accumulator and return either Done, indicating the iterator has no more elements, or Next which contains a new element and accumulator. The element is yielded by the iterator and the new accumulator is used with the function to compute the next element in the sequence.

Examples

> unfold(from: 5, with: fn(n) {
>  case n {
>    0 -> Done
>    n -> Next(element: n, accumulator: n - 1)
>  }
> })
> |> to_list
[5, 4, 3, 2, 1]