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

difflist()
difflist() :: (list -> list)

Functions

append(difflist_a, difflist_b)
append(difflist, difflist) :: difflist

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]
concat(difflists)
concat([difflist]) :: difflist

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]
cons(difflist, x)
cons(difflist, any) :: difflist

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]
empty()
empty() :: difflist

Returns an empty difference list.

from_list(xs)
from_list(list) :: difflist

Converts a list into a difference list.

head(difflist)
head(difflist) :: any

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
singleton(x)
singleton(any) :: difflist

Returns a difference list of one item.

Example

iex> DiffList.singleton(1) |> DiffList.to_list
[1]
snoc(difflist, x)
snoc(difflist, any) :: difflist

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]
tail(difflist)
tail(difflist) :: list

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]
to_list(difflist)
to_list(difflist) :: list

Convert a difference list into a list.

Examples

iex> DiffList.from_list([1, 2, 3]) |> DiffList.to_list
[1, 2, 3]