I want to create data types for accessing and modifying a simple music library, consisting of albums consisting of tracks. For a basic idea, consider the following:
data MusicCollection = MC { albums :: Seq Album }
data Album = Album { albumTitle :: String, tracks :: Seq Track }
data Track = Track { trackTitle :: String, tempo :: Tempo }
data Tempo = Unknown | BPM Int
In addition to tempo, there may be other attributes like style or rating.
The above solution gives me fast access to random albums. Additionally, I'd like to have fast access to all tracks faster than a specified tempo. And again, it would be nice to have fast random access to the returned tracks:
fasterThan :: Int -> MusicCollection -> SomeRandomAccessCollection Track
Updating a track in the collection shouldn't be too slow as well.
Question:
I don't know if adding a Map Tempo (Seq Track) to the MusicCollection is best or if it's possible to immitate relational data bases somehow. Perhaps there are completely different solutions?
I currently don't want to use a data base, but it would be interesting to know criteria when to use them in desktop applications.
Seqfor the collection? Is order important? Why do you need random access to the result offasterThan? Should the result be sorted by increasing tempo, or preserve the order of the collection, or does it even matter? Are you willing to take up more memory in order to provide faster access to these queries?Set, I didn't see how to achieve it with that and lists obviously require O(n) for this. In the meantime, I decided to giveHDBCa try. After ehird's answer, I experimented withIxSetwhich seems nice for my original problem. I still have to learn how to design a database application in a rather pure way and so that it can easily be tested.Setof all things would be exemplary in O(1) random selection. Alas, for the current implementation, it is not so.