Reputation: 415
Is there a good standard (or just stable) implementation of (ordered) Container class?
Container class should provide basic manipulation operations with it's instance
s. Typical instances should be: Array, List, e.t.c.
Operations are:
n
'th element from container;n
first element's of container;Why do I need this? With the help of this class I can, for example, define some function for processing something in container, and use it with any container that I need.
Upvotes: 3
Views: 288
Reputation: 5172
Data.Key.Indexed has several instances, including List, IntMap, and more
http://hackage.haskell.org/package/keys-0.2.2/docs/Data-Key.html#g:5
Upvotes: 2
Reputation: 949
It sounds like you may find ListLike interesting. index
and take
, respectively, cover the two operations you mentioned and it includes some 'typical instances'.
Upvotes: 3
Reputation: 53881
Kind of. This isn't Java so typeclass hierarchies are pretty rare. However, I think foldable is what you want
fold :: Monoid m => t m -> m
foldMap :: Monoid m => (a -> m) -> t a -> m
foldr :: (a -> b -> b) -> b -> t a -> b
foldl :: (b -> a -> b) -> b -> t a -> b
An example index function,
index i = snd . foldl' choose (0, Nothing)
where choose (j, Just x) _ = (j, Just x)
choose (j, Nothing) x | i == j = (i, Just x)
| otherwise = (j + 1, Nothing)
Now using this you can trivially flatten any collection to a list and treat it as such, and there are more intelligent ways to fold over a collection and keep the ith entry.
To modify it, you could use traversable. It's another class with a few functions based around
traverse :: Applicative f => (a -> f b) -> t a -> f (t a)
Upvotes: 2