A limited-size deque (double-ended queue).
A deque is a data type for which elements can be efficiently added or removed from either the front or the back. In this limited-size variant, when the deque is full, an insertion on the front will drop an element at the back, and vice versa.
#Type
#Build
Create an empty deque.
Create a deque with one element.
Add an element to the front of the deque.
If the deque has reached its maximum capacity, an item is dropped at the back.
Add an element to the back of the deque.
If the deque has reached its maximum capacity, an item is dropped at the front.
Concatenate two deques into one.
This function is written in pipeline style, so
firstBoundedDeque
|> BoundedDeque.append secondBoundedDeque
|> BoundedDeque.toListis the same as
(BoundedDeque.toList firstBoundedDeque)
|> List.append (BoundedDeque.toList secondBoundedDeque)The maxSize is set to the sum of the two sizes.
#Lists
Create a bounded deque from a maximum size and a list.
Convert a deque to a list.
#Deques
Create a bounded deque from an unbounded one. If there is insufficient space, elements are dropped from the back.
Convert a bounded deque to a normal deque.
#Bound
Get the maximum number of elements this deque can hold.
Sets a bound to the number of elements the deque can hold. a maxSize of Nothing means the deque's size is unbound, Just a value bounds the deque's size at that value.
If the deque is larger than the bound, items are dropped from the back.
BoundedDeque.fromList 10 (List.range 0 9)
|> resize (\_ -> 5)
-- toList would give [ 0, 1, 2, 3, 4 ]
|> pushFront 42
-- toList would give [ 42, 0, 1, 2, 3 ]
|> pushBack -1
-- toList would give [ 0, 1, 2, 3, -1 ]
|> setMaxSize Nothing
|> pushFront 73
-- toList would give [ 73, 0, 1, 2, 3 -1 ]#Query
Determine if a deque is empty.
Figure out whether a deque contains a value.
Determine the length of a list.
Extract the first element of a deque
Extract the last element of a deque.
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.
BoundedDeque.fromList [2..10]
|> BoundedDeque.takeBack 3
-- == [ 2, 3, 4 ]Take the last n members of a deque.
BoundedDeque.fromList [2..10]
|> BoundedDeque.takeBack 3
-- == [ 10, 9, 8 ]#Transform
Apply a function to all elements in a deque.
Keep an element when it satisfies a predicate.
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.
#Composition
Like List.map2; apply a function pairwise to two deques.
Handy function for constructing maps.
to extend to map3 and beyond:
map3 f a b c =
map f a
|> andMap b
|> andMap c
The deque datatype
BoundedDeque equality with
(==)is unreliable (equivalent deques can have a different distribution of elements between the back and the front) and should not be used.