开发者

Is it possible to create a collection api like Scala 2.8's in Haskell?

开发者 https://www.devze.com 2023-02-04 10:43 出处:网络
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 genera

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消