Enumerable.slice
slice
, go back to Enumerable module for more information.
Specs
slice(t()) :: {:ok, size :: non_neg_integer(), slicing_fun()} | {:error, module()}
Returns a function that slices the data structure contiguously.
It should return {:ok, size, slicing_fun}
if the enumerable
has
a known bound and can access a position in the enumerable
without
traversing all previous elements.
Otherwise it should return {:error, __MODULE__}
and a default
algorithm built on top of reduce/3
that runs in linear time will be
used.
Differences to count/1
The size
value returned by this function is used for boundary checks,
therefore it is extremely important that this function only returns :ok
if retrieving the size
of the enumerable
is cheap, fast and takes constant
time. Otherwise the simplest of operations, such as Enum.at(enumerable, 0)
,
will become too expensive.
On the other hand, the count/1
function in this protocol should be
implemented whenever you can count the number of elements in the collection without
traversing it.