View Source shards_enum (shards v1.1.1)

Provides a set of utilities to work with enumerables.

Summary

Functions

Returns a list where each element is the result of invoking Fun on each corresponding element of Enumerable.

Similar to shards_enum:map/2 but it runs in parallel.

Invokes Fun for each element in the Enumerable with the accumulator.

Reduces enumerable until Fun returns {halt, AccOut}.

Functions

-spec map(Fun, Enumerable) -> [Result]
             when
                 Fun :: fun((Elem) -> Result),
                 Elem :: term(),
                 Result :: term(),
                 Enumerable :: [term()] | map() | non_neg_integer().

Returns a list where each element is the result of invoking Fun on each corresponding element of Enumerable.

For maps, the function expects a key-value tuple.

Equivalent to pmap(Fun, infinity, Enumerable).

Link to this function

pmap(Fun, Timeout, Enumerable)

View Source
-spec pmap(Fun, Timeout, Enumerable) -> [Result]
              when
                  Fun :: fun((Elem) -> Result),
                  Elem :: term(),
                  Timeout :: timeout(),
                  Result :: term(),
                  Enumerable :: [term()] | map() | non_neg_integer().
Similar to shards_enum:map/2 but it runs in parallel.
Link to this function

reduce(Fun, Acc0, Enumerable)

View Source
-spec reduce(Fun, Acc0, Enumerable) -> Acc1
                when
                    Fun :: fun((Elem, AccIn) -> AccOut),
                    Elem :: term(),
                    AccIn :: term(),
                    AccOut :: term(),
                    Acc0 :: term(),
                    Acc1 :: term(),
                    Enumerable :: [term()] | map() | non_neg_integer().

Invokes Fun for each element in the Enumerable with the accumulator.

The initial value of the accumulator is Acc0. The function is invoked for each element in the enumerable with the accumulator. The result returned by the function is used as the accumulator for the next iteration. The function returns the last accumulator.
Link to this function

reduce_while(Fun, Acc0, Enumerable)

View Source
-spec reduce_while(Fun, Acc0, Enumerable) -> Acc1
                      when
                          Fun :: fun((Elem, AccIn) -> FunRes),
                          FunRes :: {cont | halt, AccOut},
                          Elem :: term(),
                          AccIn :: term(),
                          AccOut :: term(),
                          Acc0 :: term(),
                          Acc1 :: term(),
                          Enumerable :: [term()] | map() | non_neg_integer().

Reduces enumerable until Fun returns {halt, AccOut}.

The return value for Fun is expected to be

  • {cont, AccOut} to continue the reduction with AccOut as the new accumulator or
  • {halt, AccOut} to halt the reduction
If fun returns {halt, AccOut} the reduction is halted and the function returns AccOut. Otherwise, if the enumerable is exhausted, the function returns the accumulator of the last {cont, AccOut}.