Janiczek/elm-priority-queuePriority queue is a collection that gives an easy access to its smallest (or largest) element.
This package is a fork of fifth-postulate/priority-queue that:
singleton, filter, length, any, etc.)import MaxPriorityQueue exposing (MaxPriorityQueue)
type alias BuyOffer =
{ amount : Int
, unitPrice : Int
}
offersByPrice : MaxPriorityQueue BuyOffer
offersByPrice =
MaxPriorityQueue.fromList .unitPrice
[ { amount = 10, unitPrice = 3000 }
, { amount = 15, unitPrice = 10000 }
, { amount = 100, unitPrice = 2000 }
]
bestOffer =
MaxPriorityQueue.largest offersByPrice
--> Just { amount = 1, unitPrice = 10000 }
bestAndRest =
MaxPriorityQueue.dequeue offersByPrice
--> Just
-- ( { amount = 15, unitPrice = 10000 }
-- , MaxPriorityQueue.fromList .unitPrice
-- [ { amount = 10, unitPrice = 3000 }
-- , { amount = 100, unitPrice = 2000 }
-- ]
-- )
nextBest =
bestAndRest
|> Maybe.andThen (\(_, rest) -> MaxPriorityQueue.largest rest)
--> Just { amount = 10, unitPrice = 3000 }MinPriorityQueue works similarly, just has smallest instead of largest in its functions:
import MinPriorityQueue exposing (MinPriorityQueue)
type alias Person =
{ name : String
, age : Int
}
peopleByAge : MinPriorityQueue Person
peopleByAge =
MinPriorityQueue.fromList .age
[ Person "Martin" 31
, Person "Xavier" 13
, Person "Joanne" 54
]
youngest : Maybe Person
youngest =
MinPriorityQueue.smallest peopleByAge
--> Just { name = "Xavier", age = 13 }