folkertdev/elm-deque/3.0.1

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

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 : Int -> BoundedDeque a

Create an empty deque.

singleton : Int -> a -> BoundedDeque a

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.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 : 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 : Int -> 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 -> Deque a

Convert a bounded deque to a normal deque.

Bound

Get the maximum number of elements this deque can hold.

resize : (Int -> 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 -> Bool

Determine if a deque is empty.

member : a -> BoundedDeque a -> Bool

Figure out whether a deque contains a value.

length : BoundedDeque a -> 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 : Int -> BoundedDeque a -> List a

Take the first n members of a deque.

BoundedDeque.fromList [2..10]
    |> BoundedDeque.takeBack 3
    -- == [ 2, 3, 4 ]
takeBack : 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 -> 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 -> 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.

Handy function for constructing maps.

to extend to map3 and beyond:

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