skew_list (skew_list v1.0.1)
View SourceOkasaki's skew binary random-access list.
A persistent sequence with O(1) worst-case cons, hd and tl, and
O(log n) nth, update and nthtail. Positions are 1-based and argument
order follows lists. On skew lists, each function returns or raises what
the lists or erlang function its docs name does, on the pinned OTP release.
Requires Erlang/OTP 27 or later.
Examples
1> S = skew_list:from_list([a, b, c]).
2> skew_list:nth(3, skew_list:cons(z, S)).
b
3> skew_list:to_list(skew_list:update(1, x, S)).
[x,b,c]
Summary
Types
A skew list of any elements.
A skew list of elements of type T. Use this module's functions, not its term.
Building
Returns a skew list holding the elements of S1 followed by those of S2,
as lists:append/2 does for plain lists. O(length(S1)).
Adds X at the front, as [X | L] does. O(1).
Returns a skew list of N copies of X, as lists:duplicate/2 does.
O(log n) time and memory: equal subtrees are shared. Sending the result to
another process or storing it in ETS copies it flat, at O(n).
Builds a skew list holding the elements of L in order. O(length(L)).
Returns an empty skew list, the counterpart of []. O(1).
Folding
Folds F over the elements of S from first to last, as lists:foldl/3
does: F(X1, Acc), then F(X2, Acc1), and so on. O(n) calls to F.
Folds F over the elements of S from last to first, as lists:foldr/3
does: F(Xn, Acc), then F(Xn-1, Acc1), and so on. O(n) calls to F.
Maps and folds in one pass from the first element to the last, as
lists:mapfoldl/3 does: F(X, Acc) returns {Y, Acc1}. The tree shapes are
kept. O(n) calls to F.
Maps and folds in one pass from the last element to the first, as
lists:mapfoldr/3 does. The tree shapes are kept. O(n) calls to F.
Reading
Returns true if Pred(X) is true for every element X of S, as
lists:all/2 does, calling Pred from the first element and stopping at
the first false. O(n) calls to Pred.
Returns true if Pred(X) is true for some element X of S, as
lists:any/2 does, calling Pred from the first element and stopping at
the first true. O(n) calls to Pred.
Returns the first element. O(1).
Returns the last element. O(log n).
Returns the number of elements, as erlang:length/1 does. O(1).
Returns true if X is =:= to some element of S, as lists:member/2
does. Stops at the first match. O(n).
Returns the element at 1-based position N. O(min(N, log n)).
Returns the elements as a plain list, in order. O(n).
Slicing
Returns S without its first N elements. O(log n).
Splits S after its first N elements, as lists:split/2 does: returns
{Prefix, Suffix}. O(N + log n).
Returns the first Len elements of S, or all of them when S is shorter,
as lists:sublist/2 does. O(Len + log n).
Returns up to Len elements of S starting at 1-based position Start, as
lists:sublist/3 does. O(Len + log n).
Returns the list without its first element. O(1).
Transforming
Returns a skew list holding the elements X of S for which Pred(X) is
true, in order, as lists:filter/2 does, calling Pred from the first
element to the last. O(n) calls to Pred.
Returns a skew list holding F(X) for each element X of S, as
lists:map/2 does, calling F from the first element to the last. The tree
shapes are kept, so the result is canonical. O(n) calls to F.
Returns a skew list holding the elements of S in reverse order, as
lists:reverse/1 does for plain lists. O(n).
Splits a skew list of pairs into the list of first and the list of second
elements, as lists:unzip/1 does. Both keep the tree shapes. O(n).
Returns S with the element at 1-based position N replaced by X.
O(min(N, log n)).
Pairs the elements of S1 and S2 position by position, as lists:zip/2
does. Lists of equal length have the same tree shapes, so the trees are
paired directly. O(n).
Types
Building
Returns a skew list holding the elements of S1 followed by those of S2,
as lists:append/2 does for plain lists. O(length(S1)).
Examples
1> skew_list:to_list(skew_list:append(skew_list:from_list([a, b]), skew_list:from_list([c]))).
[a,b,c]
Adds X at the front, as [X | L] does. O(1).
Examples
1> skew_list:to_list(skew_list:cons(a, skew_list:from_list([b, c]))).
[a,b,c]
-spec duplicate(non_neg_integer(), T) -> skew_list(T).
Returns a skew list of N copies of X, as lists:duplicate/2 does.
O(log n) time and memory: equal subtrees are shared. Sending the result to
another process or storing it in ETS copies it flat, at O(n).
Raises function_clause when N is not an integer >= 0.
Examples
1> skew_list:to_list(skew_list:duplicate(3, x)).
[x,x,x]
-spec from_list([T]) -> skew_list(T).
Builds a skew list holding the elements of L in order. O(length(L)).
Raises badarg when L is not a proper list, as erlang:length/1 does.
Examples
1> S = skew_list:from_list([a, b, c]).
2> {skew_list:length(S), skew_list:last(S)}.
{3,c}
Returns an empty skew list, the counterpart of []. O(1).
Examples
1> skew_list:to_list(skew_list:new()).
[]
Folding
-spec foldl(fun((T, A) -> A), A, skew_list(T)) -> A.
Folds F over the elements of S from first to last, as lists:foldl/3
does: F(X1, Acc), then F(X2, Acc1), and so on. O(n) calls to F.
Raises function_clause when F is not a fun of arity 2, even for an empty
list. An exception raised by F passes through unchanged.
Examples
1> skew_list:foldl(fun(X, Acc) -> [X | Acc] end, [], skew_list:from_list([a, b, c])).
[c,b,a]
-spec foldr(fun((T, A) -> A), A, skew_list(T)) -> A.
Folds F over the elements of S from last to first, as lists:foldr/3
does: F(Xn, Acc), then F(Xn-1, Acc1), and so on. O(n) calls to F.
Raises function_clause when F is not a fun of arity 2, even for an empty
list. An exception raised by F passes through unchanged.
Examples
1> skew_list:foldr(fun(X, Acc) -> [X | Acc] end, [], skew_list:from_list([a, b, c])).
[a,b,c]
Maps and folds in one pass from the first element to the last, as
lists:mapfoldl/3 does: F(X, Acc) returns {Y, Acc1}. The tree shapes are
kept. O(n) calls to F.
Raises function_clause when F is not a fun of arity 2, even for an empty
list, and {badmatch, V} when F returns a V that is not a 2-tuple.
Examples
1> F = fun(X, Sum) -> {X * 2, Sum + X} end.
2> {S, Sum} = skew_list:mapfoldl(F, 0, skew_list:from_list([1, 2, 3])).
3> {skew_list:to_list(S), Sum}.
{[2,4,6],6}
Maps and folds in one pass from the last element to the first, as
lists:mapfoldr/3 does. The tree shapes are kept. O(n) calls to F.
Raises function_clause when F is not a fun of arity 2, even for an empty
list, and {badmatch, V} when F returns a V that is not a 2-tuple.
Examples
1> F = fun(X, Sum) -> {Sum, Sum + X} end.
2> {S, Sum} = skew_list:mapfoldr(F, 0, skew_list:from_list([1, 2, 3])).
3> {skew_list:to_list(S), Sum}.
{[5,3,0],6}
Reading
Returns true if Pred(X) is true for every element X of S, as
lists:all/2 does, calling Pred from the first element and stopping at
the first false. O(n) calls to Pred.
Raises function_clause when Pred is not a fun of arity 1, even for an
empty list, and {case_clause, V} when Pred returns a non-boolean V.
Examples
1> skew_list:all(fun(X) -> X > 0 end, skew_list:from_list([1, 2, 3])).
true
Returns true if Pred(X) is true for some element X of S, as
lists:any/2 does, calling Pred from the first element and stopping at
the first true. O(n) calls to Pred.
Raises function_clause when Pred is not a fun of arity 1, even for an
empty list, and {case_clause, V} when Pred returns a non-boolean V.
Examples
1> skew_list:any(fun(X) -> X > 2 end, skew_list:from_list([1, 2, 3])).
true
-spec hd(skew_list(T)) -> T.
Returns the first element. O(1).
Raises badarg on an empty list, as erlang:hd/1 does.
Examples
1> skew_list:hd(skew_list:from_list([a, b, c])).
a
-spec last(skew_list(T)) -> T.
Returns the last element. O(log n).
Raises function_clause on an empty list, as lists:last/1 does.
Examples
1> skew_list:last(skew_list:from_list([a, b, c])).
c
-spec length(skew_list()) -> non_neg_integer().
Returns the number of elements, as erlang:length/1 does. O(1).
Examples
1> skew_list:length(skew_list:from_list([a, b, c])).
3
Returns true if X is =:= to some element of S, as lists:member/2
does. Stops at the first match. O(n).
Examples
1> skew_list:member(b, skew_list:from_list([a, b, c])).
true
-spec nth(pos_integer(), skew_list(T)) -> T.
Returns the element at 1-based position N. O(min(N, log n)).
Raises function_clause when N is not an integer between 1 and
length(S), as lists:nth/2 does.
Examples
1> skew_list:nth(2, skew_list:from_list([a, b, c])).
b
-spec to_list(skew_list(T)) -> [T].
Returns the elements as a plain list, in order. O(n).
Examples
1> skew_list:to_list(skew_list:from_list([a, b, c])).
[a,b,c]
Slicing
-spec nthtail(non_neg_integer(), skew_list(T)) -> skew_list(T).
Returns S without its first N elements. O(log n).
nthtail(0, S) returns S. Raises function_clause when N is not an
integer between 0 and length(S), as lists:nthtail/2 does.
Examples
1> skew_list:to_list(skew_list:nthtail(2, skew_list:from_list([a, b, c]))).
[c]
-spec split(non_neg_integer(), skew_list(T)) -> {skew_list(T), skew_list(T)}.
Splits S after its first N elements, as lists:split/2 does: returns
{Prefix, Suffix}. O(N + log n).
Raises badarg when N is not an integer between 0 and length(S).
Examples
1> {P, Q} = skew_list:split(1, skew_list:from_list([a, b, c])).
2> {skew_list:to_list(P), skew_list:to_list(Q)}.
{[a],[b,c]}
-spec sublist(skew_list(T), non_neg_integer()) -> skew_list(T).
Returns the first Len elements of S, or all of them when S is shorter,
as lists:sublist/2 does. O(Len + log n).
Raises function_clause when Len is not an integer >= 0.
Examples
1> skew_list:to_list(skew_list:sublist(skew_list:from_list([a, b, c, d]), 2)).
[a,b]
-spec sublist(skew_list(T), pos_integer(), non_neg_integer()) -> skew_list(T).
Returns up to Len elements of S starting at 1-based position Start, as
lists:sublist/3 does. O(Len + log n).
When Start - 1 is past the end the result is empty, whatever Len is.
Otherwise raises function_clause when Start is not an integer >= 1 or
Len is not an integer >= 0, as lists:sublist/3 does.
Examples
1> skew_list:to_list(skew_list:sublist(skew_list:from_list([a, b, c, d]), 2, 2)).
[b,c]
Returns the list without its first element. O(1).
Raises badarg on an empty list, as erlang:tl/1 does.
Examples
1> skew_list:to_list(skew_list:tl(skew_list:from_list([a, b, c]))).
[b,c]
Transforming
Returns a skew list holding the elements X of S for which Pred(X) is
true, in order, as lists:filter/2 does, calling Pred from the first
element to the last. O(n) calls to Pred.
Raises function_clause when Pred is not a fun of arity 1, even for an
empty list, and {bad_filter, V} when Pred returns a non-boolean V. An
exception raised by Pred passes through unchanged.
Examples
1> Odd = fun(X) -> X rem 2 =:= 1 end.
2> skew_list:to_list(skew_list:filter(Odd, skew_list:from_list([1, 2, 3, 4, 5]))).
[1,3,5]
Returns a skew list holding F(X) for each element X of S, as
lists:map/2 does, calling F from the first element to the last. The tree
shapes are kept, so the result is canonical. O(n) calls to F.
Raises function_clause when F is not a fun of arity 1, even for an empty
list. An exception raised by F passes through unchanged.
Examples
1> skew_list:to_list(skew_list:map(fun(X) -> X * 2 end, skew_list:from_list([1, 2, 3]))).
[2,4,6]
Returns a skew list holding the elements of S in reverse order, as
lists:reverse/1 does for plain lists. O(n).
Examples
1> skew_list:to_list(skew_list:reverse(skew_list:from_list([a, b, c]))).
[c,b,a]
Splits a skew list of pairs into the list of first and the list of second
elements, as lists:unzip/1 does. Both keep the tree shapes. O(n).
Raises function_clause when an element is not a 2-tuple.
Examples
1> {L, R} = skew_list:unzip(skew_list:from_list([{a, 1}, {b, 2}])).
2> {skew_list:to_list(L), skew_list:to_list(R)}.
{[a,b],[1,2]}
-spec update(pos_integer(), T, skew_list(T)) -> skew_list(T).
Returns S with the element at 1-based position N replaced by X.
O(min(N, log n)).
Raises function_clause when N is not an integer between 1 and
length(S), as nth/2 does.
Examples
1> skew_list:to_list(skew_list:update(2, x, skew_list:from_list([a, b, c]))).
[a,x,c]
Pairs the elements of S1 and S2 position by position, as lists:zip/2
does. Lists of equal length have the same tree shapes, so the trees are
paired directly. O(n).
Raises function_clause when the lengths differ.
Examples
1> skew_list:to_list(skew_list:zip(skew_list:from_list([a, b]), skew_list:from_list([1, 2]))).
[{a,1},{b,2}]