fivetentech / elm-bounded-list / BoundedList

BoundedList are just list with a set maximum size. If you've reached the max size and add more elements, then some will be dropped!

Definition


type BoundedList itemType

A list of itemType that will have a maximum number of items within.

The type is opaque so refer to the next section to see how to create a BoundedList

Creating BoundedList

empty : Basics.Int -> BoundedList a

Create an empty BoundedList with a specific maxSize

(empty 4 |> tolist) == []

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

Create a BoundedList from a given list and maximum size. If the given list doesn't respect the size constraint, elements from the begining of the list will be drop to fit in.

(fromList 2 [ 1, 2, 3, 4 ] |> toList) == [ 3, 4 ]

Manipulating BoundedLists

appendEnd : BoundedList a -> List a -> BoundedList a

take a List and adds it at the end of an existing BoundedList. If the resulting list is too large, elements from the top of the list are dropped until it fits

(appendEnd (fromList 2 [ 1, 2, 3 ]) [ 4, 5, 6 ] |> toList) == [ 5, 6 ]

(appendEnd (fromList 6 [ 1, 2, 3 ]) [ 4, 5, 6 ] |> toList) == [ 1, 2, 3, 4, 5, 6 ]

(appendEnd (fromList 4 [ 1, 2 ]) [ 3, 4, 5 ] |> toList) == [ 2, 3, 4, 5 ]

appendStart : List a -> BoundedList a -> BoundedList a

take a List and adds it at the start of an existing BoundedList. If the resulting list is too large, elements from the end of the list are dropped until it fits.

(appendStart [ 1, 2, 3 ] (fromList 2 [ 4, 5, 6 ]) |> toList) == [ 1, 2 ]

(appendStart [ 1, 2, 3 ] (fromList 6 [ 4, 5, 6 ]) |> toList) == [ 1, 2, 3, 4, 5, 6 ]

(appendStart [ 1, 2, 3 ] (fromList 4 [ 4, 5 ]) |> toList) == [ 1, 2, 3, 4 ]

toList : BoundedList a -> List a

extract the List part from a BoundedList

(empty 4 |> tolist) == []

(fromList 4 [ 1, 2 ] |> tolist) == [ 1, 2 ]

cons : a -> BoundedList a -> BoundedList a

Adds an item at the start of the list and drops one at the end if needed

(fromList 2 [ 1, 2, 3 ] |> cons 4 |> toList) == [ 4, 1 ]

(fromList 4 [ 1, 2, 3 ] |> addLast 4 |> toList) == [ 4, 1, 2, 3 ]

(fromList 4 [ 1, 2, 3 ] |> addLast 4 |> addLast 5 |> toList) == [ 5, 4, 1, 2 ]

addLast : a -> BoundedList a -> BoundedList a

Adds an item at the end of the list dropping one from the start if the list is already at max capacity

(fromList 2 [ 1, 2, 3 ] |> addLast 4 |> toList) == [ 3, 4 ]

(fromList 4 [ 1, 2, 3 ] |> addLast 4 |> toList) == [ 1, 2, 3, 4 ]

(fromList 4 [ 1, 2, 3 ] |> addLast 4 |> addLast 5 |> toList) == [ 2, 3, 4, 5 ]