Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 (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).

like image 846
justin Avatar asked Jan 13 '11 22:01

justin


2 Answers

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

like image 58
sclv Avatar answered Nov 02 '22 15:11

sclv


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.

like image 20
dino Avatar answered Nov 02 '22 16:11

dino