difflist v1.0.0 DiffList
Difference lists are a way of encoding a list as the action of preappending them.
Instead of a list being [1, 2, 3]
, it is the anonymous function fn(ys) -> [1, 2, 3] ++ ys end
.
Difference lists are fast for left-associated appends (list ++ [x]
) as they are represented as function composition.
Refer to this excellent blog post for more information.
Summary
Functions
Append a difference list to another difference list
Concatenates a list of difference lists into one difference list
Prepends an item to a difference list
Returns an empty difference list
Converts a list into a difference list
Gets the first element of a difference list
Returns a difference list of one item
Appends an item to a difference list
Gets the tail a difference list
Convert a difference list into a list
Types
Functions
Append a difference list to another difference list.
Example
iex> x = DiffList.from_list([1, 2, 3])
iex> y = DiffList.from_list([4, 5, 6])
iex> DiffList.append(x, y) |> DiffList.to_list
[1, 2, 3, 4, 5, 6]
Concatenates a list of difference lists into one difference list.
Example
iex> x = DiffList.from_list([1, 2, 3])
iex> y = DiffList.from_list([4, 5, 6])
iex> z = [x, y]
iex> DiffList.concat(z) |> DiffList.to_list
[1, 2, 3, 4, 5, 6]
Prepends an item to a difference list.
The difference list equivalent of [x] ++ list
.
Example
iex> x = DiffList.from_list([2, 3])
iex> DiffList.cons(x, 1) |> DiffList.to_list
[1, 2, 3]
Returns an empty difference list.
Converts a list into a difference list.
Gets the first element of a difference list.
Essentially the same as hd(list)
.
Example
iex> x = DiffList.from_list([1, 2, 3])
iex> DiffList.head(x)
1
Appends an item to a difference list.
The difference list equivalent of list ++ [x]
.
Example
iex> x = DiffList.from_list([1, 2])
iex> DiffList.snoc(x, 3) |> DiffList.to_list
[1, 2, 3]
Gets the tail a difference list.
Essentially the same as tl(list)
.
Example
iex> x = DiffList.from_list([1, 2, 3])
iex> DiffList.tail(x) |> DiffList.to_list
[2, 3]