The (a -> Int) function given to singleton, insert, enqueue and fromList
is how you teach the queue to get the priority of an item. MinPriorityQueue will
prioritize items with smaller Ints.
Note: Some functions in this module return lists of values in perhaps a slightly
unexpected reversed order (toSortedList, dequeueMany, take). This is done
in name of efficiency: there's no trick to efficiently return it in the more
expected reverse order, so we let the user do the List.reverse themselves if
needed and make the cost more apparent.
toSortedList (fromList identity [ 100, 3, 1, 4, 1, 5, 9, 200 ])
--> [ 200, 100, 9, 5, 4, 3, 1, 1 ]
List.reverse (toSortedList (fromList identity [ 100, 3, 1, 4, 1, 5, 9, 200 ]))
--> [ 1, 1, 3, 4, 5, 9, 100, 200 ]Anyways, let's continue with the rest of the docs!
Create an empty MinPriorityQueue.
Create a MinPriorityQueue with a single element.
singleton identity 5
|> toList
--> [ 5 ]Create a MinPriorityQueue from a list of elements.
fromList identity [ 3, 1, 4, 1, 5, 9 ]
|> toSortedList
--> [ 1, 1, 3, 4, 5, 9 ]Convert a MinPriorityQueue to a list.
The order of items in the resulting list is unspecified.
If you need a sorted list, use toSortedList.
toList (fromList identity [ 100, 3, 1, 4, 1, 5, 9, 200 ])
--> [ 1, 1, 3, 100, 4, 5, 9, 200 ]Convert a MinPriorityQueue to a sorted list.
The order of items in the resulting list is lowest-priority-first, thus may seem reversed from what you want. See note at the top.
toSortedList (fromList identity [ 100, 3, 1, 4, 1, 5, 9, 200 ])
--> [ 200, 100, 9, 5, 4, 3, 1, 1 ]Fold over the elements in the MinPriorityQueue, highest priority first.
fromList identity [ 3, 1, 4 ]
|> fold (\x acc -> x :: acc) []
--> [ 4, 3, 1 ]
fromList identity [ 3, 1, 4 ]
|> fold (+) 0
--> 8
fromList Tuple.first [ (10, "World"), (1, "Hello"), (5, "Elm") ]
|> fold (\(_, word) acc -> acc ++ " " ++ word) ""
--> " Hello Elm World"Insert an element into a MinPriorityQueue. O(log n).
empty
|> insert identity 3
|> insert identity 1
|> insert identity 4
|> smallest
--> Just 1Insert an element into a MinPriorityQueue. O(log n).
This is an alias for insert.
empty
|> enqueue identity 3
|> enqueue identity 1
|> enqueue identity 4
|> smallest
--> Just 1Keep only the elements that satisfy the predicate.
fromList identity [ 1, 2, 3, 4, 5 ]
|> filter (\x -> modBy 2 x == 0)
|> toSortedList
--> [ 4, 2 ]Remove and return the element with the highest priority from the MinPriorityQueue, along with the updated queue. Returns Nothing if the queue is empty. O(log n).
dequeue (fromList identity [ 3, 1, 4 ])
--> Just ( 1, fromList identity [ 3, 4 ] )
dequeue empty
--> NothingRetrieve the N items with highest priority, alongside the queue without them.
The order of items in the resulting list is lowest-priority-first, thus may seem reversed from what you want. See note at the top.
fromList identity [ 3, 1, 5, 2, 4 ]
|> dequeueMany 3
--> ( [ 3, 2, 1 ], fromList identity [ 5, 4 ] )If you take more items than are in the queue, you will get all the items in the queue.
fromList identity [ 3, 1, 2 ]
|> dequeueMany 5
--> ( [ 3, 2, 1 ], empty )Get the item with the highest priority without removing it from the queue. Returns Nothing if the queue is empty. O(1).
This is an alias for head.
smallest (fromList identity [ 3, 1, 4 ])
--> Just 1
smallest empty
--> NothingGet the item with the highest priority without removing it from the queue. Returns Nothing if the queue is empty. O(1).
head (fromList identity [ 3, 1, 4 ])
--> Just 1
head empty
--> NothingGet a new queue with the highest priority item removed. Returns Nothing if the queue is empty. O(log n).
tail (fromList identity [ 3, 1, 4 ])
|> Maybe.map toSortedList
--> Just [ 4, 3 ]
tail empty
--> NothingRetrieve the N items with highest priority. O(log n).
The order of items in the resulting list is lowest-priority-first, thus may seem reversed from what you want. See note at the top.
fromList identity [ 3, 1, 5, 2, 4 ]
|> take 3
--> [ 3, 2, 1 ]If you take more items than are in the queue, you will get all the items in the queue.
take 1000 (singleton identity 1)
--> [ 1 ]Drop the N items with highest priority from the queue. O(log n).
fromList identity [ 3, 1, 5, 2, 4 ]
|> drop 3
|> toSortedList
--> [ 5, 4 ]If you drop more items than are in the queue, you will get an empty queue.
drop 1000 (singleton identity 1)
--> emptyDetermine if all elements satisfy the predicate.
fromList identity [ 1, 2, 3 ]
|> all (\x -> x > 10)
== False
fromList identity [ 9, 11 ]
|> all (\x -> x > 10)
== False
fromList identity [ 15, 16, 17 ]
|> all (\x -> x > 10)
== True
all (\_ -> True) empty --> True
all (\_ -> True) (singleton identity 1) --> True
all (\_ -> False) empty --> True
all (\_ -> False) (singleton identity 1) --> FalseDetermine if any elements satisfy the predicate.
fromList identity [ 1, 2, 3 ]
|> any (\x -> x > 10)
== False
fromList identity [ 9, 11 ]
|> any (\x -> x > 10)
== True
fromList identity [ 15, 16, 17 ]
|> any (\x -> x > 10)
== True
any (\_ -> True) empty --> False
any (\_ -> True) (singleton identity 1) --> True
any (\_ -> False) empty --> False
any (\_ -> False) (singleton identity 1) --> FalseCheck if a MinPriorityQueue is empty.
isEmpty empty
--> True
isEmpty (singleton identity 1)
--> FalseGet the number of elements in the queue. O(1).
length (fromList identity [ 1, 2, 3 ])
--> 3
length empty
--> 0
A priority queue giving the highest priority to smallest Ints given by your
(a -> Int)function.Note that MinPriorityQueue is not
(==)-safe.