It implements a slice API allowing you to emulate the Python index notation in Erlang and Elixir. It's made of the following two functions.
slice:index/2slice:range/2
Consider a list like ['a', 'b', 'c', 'd', 'e'] in Python. With its index
notation you can easily retrieve the last element, the last three elements, or
let's say, the whole list without the first and last element.
l = ['a', 'b', 'c', 'd', 'e']
print(l[-1]) # 'e'
print(l[-3:]) # ['c', 'd', 'e']
print(l[1:-1]) # ['b', 'c', 'd']With the slice API, you can easily compute those elements and sub-lists using the same values.
Let's consider the very same list in Erlang.
List = [a, b, c, d, e].To compute a single element, use the slice:index/2 function. For instance,
here is how to compute the last element.
Index = slice:index(length(List), -1).
e = lists:nth(List, Index).The
slice:index/2function uses a 0-based index and returns the value that must be passed to thelists:nth/2function to compute the element.
To compute a sub-list, use the slice:range/2 function. For instance, here is
how to compute the last 3 elements, and the whole list without the first and
last element.
{Start, Length} = slice:range(length(List), {-3, undefined}).
[c, d, e] = lists:sublist(List, Start, Length).{Start, Length} = slice:range(length(List), {1, -1}).
[b, c, d] = lists:sublist(List, Start, Length).The
slice:range/2function uses 0-based indexes and returns the values that must be passed to thelists:sublist/3function to compute the sub-list.
It's essentially just that. For the details of the API, refer to the documentation for more information.