A Deque (double-ended queue) in Elm.
A deque is a data type for which elements can be efficiently added or removed from either the front or the back.
Internally, this is a head-tail linked list, modeled after this deque in Haskell which
in turn is based on Chris Okasaki's Purely Functional Data Structures. A head-tail linked list is based on two lists: one for the head and one for the tail.
This means that pop and push on either side are operations on the front portion of an elm list, which is very efficient (O(1)).
The deque rebalances (moves elements from the front to the rear or vice versa) when either one is 4 times as large as the other. This is a costly operation and therefore used as little as possible.
For a deque with a limited size, see BoundedDeque.
Concatenate two deques into one.
This function is written in pipeline style, so
firstDeque
|> Deque.append secondDeque
|> Deque.toListis the same as
Deque.toList firstDeque
|> List.append (Deque.toList secondDeque)Gives Maybe the first element, and the deque without the first element. If there are no elements, the empty deque is returned.
Gives Maybe the last element, and the deque without the last element. If there are no elements, the empty deque is returned.
Take the first n members of a deque.
Deque.fromList [2..10]
|> Deque.takeBack 3
-- == [ 2, 3, 4 ]Take the last n members of a deque.
Deque.fromList [2..10]
|> Deque.takeBack 3
-- == [ 10, 9, 8 ]Fold over the deque from left to right (highest priority to lowest priority).
Fold over the deque from right to left (lowest priority to highest priority).
Partition a deque according to a predicate. The first deque contains all elements that satisfy the predicate, and the second contains the rest.
Like List.map2; apply a function pairwise to two deques.
The deque datatype
Deque equality with
(==)is unreliable (equivalent deques can have a different distribution of elements between the back and the front) and should not be used.