View Source Iter.Iterable protocol (iterex v0.1.1)

This is the main iterable protocol.

It is intentionally huge, however rte only function you have to implement is next/1, for the remainder you can rely on the default implementations from Iter.Impl unless your data structure can provide a more efficient method of generating the correct answer.

Summary

Functions

Tests if every element in the iterable matches predicate.

Tests if any element in the iterable matches predicate.

Append an element onto the end of the iterable.

Returns the element index items from the beginning of the iterator.

Creates an iterable that only emits elements for which fun returns a new value.

Creates an iterable that chunks into count size elements, where each new chunk starts step elements into the enumerable.

Creates an iterable that chunks based on a chunk function.

Creates an iterable that iterates each iterable in an iterable.

Consumes the iterable, counting the number of iterations remaining.

Consumes the iterable, counting the number of elements for which fun returns a truthy value.

Creates an iterable that cycles it's elements eternally.

Creates an iterable that only emits elements if they are different from the previous element.

Creates an iterable that only emits elements if they are different from the previous element.

Creates an iterable which drops the first how_many elements.

Returns a new iterable with every nth element in the iterable dropped, starting with the first element.

Drops elements at the beginning of the iterable while predicate returns a truthy value.

Consumes the iterable and applies fun to each element.

Determines if the iterable is empty.

Creates an iterable which drops elements for which predicate doesn't return a truthy value.

Searches for the first element in the iterable which matches predicate.

Returns the index of the first element in the iterable which matches predicate.

Returns the first non-falsy result of fun.

Creates an iterable which works like map/2 but flattens nested iterables.

Creates an iterable which flattens nested iterables.

Creates a new iterable which places separator between adjacent items of the original iterable.

Creates a new iterable which applies mapper to each element and using it's result as the new element value.

Creates a new iterable which applies mapper on every nth element of the iterable, starting with the first element.

Returns the maximal element in the iterable according to Erlang's term ordering.

Returns the maximal element in the iterable as calculated by mapper.

Is the element a member of the iterable?

Returns the minimal element in the iterable according to Erlang's term ordering.

Returns the minimal element in the iterable as calculated by mapper.

Return the minimal and maximal element of the iterable.

Advance the iterable and return the next value.

Peeks at the first element of the iterable, without consuming it.

Peeks at the first n elements of the iterable, without consuming it.

Creates an iterable which prepends an element to the beginning of another iterable.

Creates an iterable starting at the same point, but stepping by step_size each iteration.

Collects how_many elements into a chunk and returns it as well as the remaining iterable.

Creates an iterable which takes the first how_many elements.

Creates an iterable which takes the last how_many elements.

Creates an iterable which emits elements until predicate returns false.

Convert the iterable into a list.

Creates an iterable that returns only unique elements.

Creates an iterable which emits the current iteration count as well as the next value.

Zips corresponding elements from a number of iterables into an iterable of results as computed by zipper.

Types

Functions

Link to this function

all?(iterable, predicate)

View Source
@spec all?(t(), predicate()) :: boolean()

Tests if every element in the iterable matches predicate.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

any?(iterable, predicate)

View Source
@spec any?(t(), predicate()) :: boolean()

Tests if any element in the iterable matches predicate.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

append(iterable, element)

View Source
@spec append(t(), element()) :: t()

Append an element onto the end of the iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec at(t(), non_neg_integer()) :: {:ok, non_neg_integer(), t()} | :done

Returns the element index items from the beginning of the iterator.

Note that all preceding elements, as well as the returned element, will be consumed from the iterable.

Return values

  • {:ok, element, new_iterable} - the next element and an updated iterable.
  • :done - the iterable was exhausted before the element was found.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

chunk_by(iterable, chunker)

View Source
@spec chunk_by(t(), (element() -> any())) :: t()

Creates an iterable that only emits elements for which fun returns a new value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

chunk_every(iterable, count, step, leftover)

View Source
@spec chunk_every(t(), pos_integer(), pos_integer(), t() | :discard) :: t()

Creates an iterable that chunks into count size elements, where each new chunk starts step elements into the enumerable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

chunk_while(iterable, acc, chunk_fun, after_fun)

View Source
@spec chunk_while(
  t(),
  acc,
  (element(), acc -> {:cont, chunk, acc} | {:cont, acc} | {:halt, acc}),
  (acc -> {:cont, chunk, acc} | {:cont, acc})
) :: t()
when acc: any(), chunk: any()

Creates an iterable that chunks based on a chunk function.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec concat(t()) :: t()

Creates an iterable that iterates each iterable in an iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec count(t()) :: non_neg_integer()

Consumes the iterable, counting the number of iterations remaining.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec count(t(), (element() -> as_boolean(any()))) :: non_neg_integer()

Consumes the iterable, counting the number of elements for which fun returns a truthy value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec cycle(t()) :: t()

Creates an iterable that cycles it's elements eternally.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec dedup(t()) :: t()

Creates an iterable that only emits elements if they are different from the previous element.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec dedup_by(t(), (element() -> any())) :: t()

Creates an iterable that only emits elements if they are different from the previous element.

The function fun maps every element to a term which is used to determine if two elements are duplicates.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

drop(iterable, how_many)

View Source
@spec drop(t(), non_neg_integer()) :: t()

Creates an iterable which drops the first how_many elements.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

drop_every(iterable, nth)

View Source
@spec drop_every(t(), non_neg_integer()) :: t()

Returns a new iterable with every nth element in the iterable dropped, starting with the first element.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

drop_while(iterable, predicate)

View Source
@spec drop_while(t(), predicate()) :: t()

Drops elements at the beginning of the iterable while predicate returns a truthy value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec each(t(), (element() -> any())) :: :done

Consumes the iterable and applies fun to each element.

Primarily used for side-effects.

Always returns :done.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec empty?(t()) :: boolean()

Determines if the iterable is empty.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

filter(iterable, predicate)

View Source
@spec filter(t(), predicate()) :: t()

Creates an iterable which drops elements for which predicate doesn't return a truthy value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

find(iterable, predicate)

View Source
@spec find(t(), predicate()) :: {:ok, element(), t()} | :done

Searches for the first element in the iterable which matches predicate.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

find_index(iterable, predicate)

View Source
@spec find_index(t(), predicate()) :: {:ok, non_neg_integer(), t()} | :done

Returns the index of the first element in the iterable which matches predicate.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

find_value(iterable, fun)

View Source
@spec find_value(t(), (element() -> result)) :: {:ok, result, t()} | :done
when result: any()

Returns the first non-falsy result of fun.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec flat_map(t(), (element() -> t() | element())) :: t()

Creates an iterable which works like map/2 but flattens nested iterables.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec flatten(t()) :: t()

Creates an iterable which flattens nested iterables.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

intersperse(iterable, separator)

View Source
@spec intersperse(t(), any()) :: t()

Creates a new iterable which places separator between adjacent items of the original iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec map(t(), (element() -> new_element)) :: t() when new_element: any()

Creates a new iterable which applies mapper to each element and using it's result as the new element value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

map_every(iterable, nth, mapper)

View Source
@spec map_every(t(), non_neg_integer(), (element() -> new_element)) :: t()
when new_element: any()

Creates a new iterable which applies mapper on every nth element of the iterable, starting with the first element.

The first element is always mapped unless nth is 0.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec max(t(), (element(), element() -> boolean())) :: {:ok, element()} | :done

Returns the maximal element in the iterable according to Erlang's term ordering.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

max_by(iterable, mapper, sorter)

View Source
@spec max_by(t(), (element() -> new_element), (new_element, new_element -> boolean())) ::
  {:ok, element()} | :done
when new_element: element()

Returns the maximal element in the iterable as calculated by mapper.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

member?(iterable, element)

View Source
@spec member?(t(), element()) :: boolean()

Is the element a member of the iterable?

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec min(t(), (element(), element() -> boolean())) :: {:ok, element()} | :done

Returns the minimal element in the iterable according to Erlang's term ordering.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

min_by(iterable, mapper, sorter)

View Source
@spec min_by(t(), (element() -> new_element), (new_element, new_element -> boolean())) ::
  {:ok, element()} | :done
when new_element: element()

Returns the minimal element in the iterable as calculated by mapper.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec min_max(t()) :: {:ok, element(), element()} | :done

Return the minimal and maximal element of the iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec next(t()) :: {:ok, element(), t()} | :done

Advance the iterable and return the next value.

This is the only required callback in the Iterable protocol.

Return values

  • {:ok, element, new_iterable} - returns the next element and an updated iterable.
  • :done - the iterable is exhausted.
@spec peek(t()) :: {:ok, element(), t()} | :done
@spec peek(t()) :: {:ok, [element()], non_neg_integer(), t()} | :done

Peeks at the first element of the iterable, without consuming it.

Return values

  • {:ok, element, new_iterable} - the next element and an updated iterable.
  • :done - the iterable is exhausted.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

peek(iterable, how_many)

View Source

Peeks at the first n elements of the iterable, without consuming it.

Return values

  • {:ok, [element], how_many, new_iterable} - the peekable elements and an updated iterable. Note that how_many may not be the same as you asked for if the underlying iterable is exhausted.
  • :done - the iterable is exhausted.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

prepend(iterable, element)

View Source
@spec prepend(t(), element()) :: t()

Creates an iterable which prepends an element to the beginning of another iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

step_by(iterable, step_size)

View Source
@spec step_by(t(), pos_integer()) :: t()

Creates an iterable starting at the same point, but stepping by step_size each iteration.

The first element of the iterable will always be returned, regardless of the step given.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

take_chunk(iterable, how_many)

View Source
@spec take_chunk(t(), non_neg_integer()) :: {:ok, t(), t()} | {:done, t()}

Collects how_many elements into a chunk and returns it as well as the remaining iterable.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

take_head(iterable, how_many)

View Source
@spec take_head(t(), non_neg_integer()) :: t()

Creates an iterable which takes the first how_many elements.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

take_tail(iterable, how_many)

View Source
@spec take_tail(t(), non_neg_integer()) :: t()

Creates an iterable which takes the last how_many elements.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

Link to this function

take_while(iterable, predicate)

View Source
@spec take_while(t(), predicate()) :: t()

Creates an iterable which emits elements until predicate returns false.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec to_list(t()) :: [element()]

Convert the iterable into a list.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec uniq(t()) :: t()

Creates an iterable that returns only unique elements.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec with_index(t()) :: t()

Creates an iterable which emits the current iteration count as well as the next value.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.

@spec zip(t(), ([element()] -> any())) :: t()

Zips corresponding elements from a number of iterables into an iterable of results as computed by zipper.

Optional callback

A default implementation of this function exists in the Iter.Impl module.

You can add it to your protocol implementation by adding use Iter.Impl.