
On Friday 06 June 2008, Andrew Coppin wrote:
It's really quite frustrating that it is 100% impossible to write a single function that will process lists, arrays, sets, maps, byte strings, etc. You have to write several different versions. OK, so some functions really don't make sense for a set because it's unordered, and some functions don't make sense for a map, and so forth. But for example, if I write some complicated algorithm that uses a list to store data and I change that to a set instead, I now have to wade through the function changing every operation from a list-op into a set-op. It's really very annoying!
It's not 100% impossible, depending on what exactly you're doing. For instance... Set a, ByteStrings, Seq a, Map k a and [a] are Monoids Set, Array i, Map k, Tree, Seq and [] are Foldable functors Array i, Map k, Tree, Seq, Tree and [] are Traversable functors Those can get you lots of operations (folds, maps, unions...) although there may be gaps that could be filled (Foldable is sufficient to define null, for instance, but it isn't in Data.Foldable). There's also the collections library (or Edison, if that's more your style) on hackage that takes such things a lot further. It seems it needs to be tweaked a bit to run on 6.8 according to the log, though. And there's also the issue of whether you can make all such generic operations perform well enough for every instance without making gigantic type classes that allow you to override every single operation (or if you can't, which way do you sacrifice). Of course, the prelude isn't a very good example of this sort of thing, so if that's the only place one looks (or in any module specific to one particular data structure), there's not much to find. -- Dan