[Haskell-cafe] Keeping an indexed collection of values?

Sebastian Fischer sebf at informatik.uni-kiel.de
Wed Aug 19 03:47:45 EDT 2009


On Aug 18, 2009, at 9:19 PM, Job Vranish wrote:

> data IndexedCollection a = IndexedCollection {
>     nextKey            :: Int,
>     availableKeys :: [Int],
>     items                :: (IntMap Int a)
> } deriving (Show)
>
> emptyIndexedCollection :: IndexedCollection a
> emptyIndexedCollection = IndexedCollection 0 [] empty
> [...]
> Does anyone know of a better/already existent data structure for  
> handling this problem?
> Or perhaps a better way of keeping a "key pool" than my  
> availableKeys solution?

just a slight simplification: you could drop the nextKey field and  
initialise availableKeys with [0..] in emptyIndexCollection. The add  
function would consume the head of this list, the remove function add  
the deleted key as new head.

-- 
Underestimating the novelty of the future is a time-honored tradition.
(D.G.)





More information about the Haskell-Cafe mailing list