The Scala collections api has some pretty interesting properties and I'm wondering how one would implement it in Haskell; or if it's even possible开发者_StackOverflow中文版 (or a good idea in general). I'm a bit of a haskell newbie so I'd like to hear your thoughts.
The scala map definition looks like this:
def map[B, That](f: A => B)(implicit bf: CanBuildFrom[Repr, B, That]): That
An interesting feature of this API is that if you map over a string and your map function returns a character, the result will be of type string (and not a list of characters).
We have something roughly as general as the Scala API. It's called Foldable
.
class Foldable t where
fold :: Monoid m => t m -> m
foldMap :: Monoid m => (a -> m) -> t a -> m
foldr :: (a -> b -> b) -> b -> t a -> b
foldl :: (a -> b -> a) -> a -> t b -> a
foldr1 :: (a -> a -> a) -> t a -> a
foldl1 :: (a -> a -> a) -> t a -> a
http://www.haskell.org/ghc/docs/6.12.2/html/libraries/base-4.2.0.1/Data-Foldable.html
I want to say this map function in Scala is really closer to this from Haskell:
fmap :: (Functor f) => (a -> b) -> f a -> f b
Where the list type is just another Functor.
精彩评论