folkertdev / elm-deque / BoundedDeque

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


type BoundedDeque a

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.

Build

empty : Basics.Int -> BoundedDeque a

Create an empty deque.

singleton : Basics.Int -> a -> BoundedDeque a

Create a deque with one element.

pushFront : a -> BoundedDeque a -> BoundedDeque a

Add an element to the front of the deque.

If the deque has reached its maximum capacity, an item is dropped at the back.

pushBack : a -> BoundedDeque a -> BoundedDeque a

Add an element to the back of the deque.

If the deque has reached its maximum capacity, an item is dropped at the front.

append : BoundedDeque a -> BoundedDeque a -> BoundedDeque a

Concatenate two deques into one.

This function is written in pipeline style, so

firstBoundedDeque
    |> BoundedDeque.append secondBoundedDeque
    |> BoundedDeque.toList

is the same as

BoundedDeque.toList firstBoundedDeque
    |> List.append (BoundedDeque.toList secondBoundedDeque)

The maxSize is set to the sum of the two sizes.

Lists

fromList : Basics.Int -> List a -> BoundedDeque a

Create a bounded deque from a maximum size and a list.

toList : BoundedDeque a -> List a

Convert a deque to a list.

Deques

fromDeque : Basics.Int -> Internal.Deque a -> BoundedDeque a

Create a bounded deque from an unbounded one. If there is insufficient space, elements are dropped from the back.

toDeque : BoundedDeque a -> Internal.Deque a

Convert a bounded deque to a normal deque.

Bound

getMaxSize : BoundedDeque a -> Basics.Int

Get the maximum number of elements this deque can hold.

resize : (Basics.Int -> Basics.Int) -> BoundedDeque a -> BoundedDeque a

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

isEmpty : BoundedDeque a -> Basics.Bool

Determine if a deque is empty.

member : a -> BoundedDeque a -> Basics.Bool

Figure out whether a deque contains a value.

length : BoundedDeque a -> Basics.Int

Determine the length of a list.

first : BoundedDeque a -> Maybe a

Extract the first element of a deque

last : BoundedDeque a -> Maybe a

Extract the last element of a deque.

popFront : BoundedDeque a -> ( Maybe a, BoundedDeque a )

Gives Maybe the first element, and the deque without the first element. If there are no elements, the empty deque is returned.

popBack : BoundedDeque a -> ( Maybe a, BoundedDeque a )

Gives Maybe the last element, and the deque without the last element. If there are no elements, the empty deque is returned.

takeFront : Basics.Int -> BoundedDeque a -> List a

Take the first n members of a deque.

BoundedDeque.fromList [2..10]
    |> BoundedDeque.takeBack 3
    -- == [ 2, 3, 4 ]

takeBack : Basics.Int -> BoundedDeque a -> List a

Take the last n members of a deque.

BoundedDeque.fromList [2..10]
    |> BoundedDeque.takeBack 3
    -- == [ 10, 9, 8 ]

Transform

map : (a -> b) -> BoundedDeque a -> BoundedDeque b

Apply a function to all elements in a deque.

filter : (a -> Basics.Bool) -> BoundedDeque a -> BoundedDeque a

Keep an element when it satisfies a predicate.

foldl : (a -> b -> b) -> b -> BoundedDeque a -> b

Fold over the deque from left to right (highest priority to lowest priority).

foldr : (a -> b -> b) -> b -> BoundedDeque a -> b

Fold over the deque from right to left (lowest priority to highest priority).

partition : (a -> Basics.Bool) -> BoundedDeque a -> ( BoundedDeque a, BoundedDeque a )

Partition a deque according to a predicate. The first deque contains all elements that satisfy the predicate, and the second contains the rest.

Composition

map2 : (a -> b -> c) -> BoundedDeque a -> BoundedDeque b -> BoundedDeque c

Like List.map2; apply a function pairwise to two deques.

andMap : BoundedDeque a -> BoundedDeque (a -> b) -> BoundedDeque b

Handy function for constructing maps.

to extend to map3 and beyond:

map3 f a b c =
    map f a
        |> andMap b
        |> andMap c