[Haskell-beginners] Return a Foldable instance
Federico Mastellone
fmaste at gmail.com
Wed May 4 08:32:51 CEST 2011
Sorry, the function type was incorrect, is for a part of the multimap,
not the entire multimap. But it's the same problem, a function that
returns a Foldable.
getValues :: Foldable f => k -> MultiMap k v -> f v
When the user asks for the values that a certain key contains I want
to return something that is generic and easy to play with while also
taking advantages of the underlying structure functions to avoid
repeated code.
On Wed, May 4, 2011 at 2:51 AM, Chaddaï Fouché <chaddai.fouche at gmail.com> wrote:
> On Wed, May 4, 2011 at 7:18 AM, Federico Mastellone <fmaste at gmail.com> wrote:
>> Yes, I got confused between haskell type classes and OO classes.
>>
>> I want to create different implementations of a multimap, for example
>> using lists and using Data.Set, and instead of providing functions
>> getValuesList and getValuesSet that return a [] and a Set respectively
>> I want to provide a more generic one, getValues that returns a
>> Foldable and avoid unnecessary conversions to and from lists. This way
>> the user folds directly the underlying structure, without having to
>> worry about which function is the best to fold the values. But I don't
>> know how to do this without an extra intermediate data type.
>>
>> One implementation returns a list and the other a set, both Foldables.
>> But I can't make a function like the one below for this two
>> getValues :: Foldable f => MultiMap k v -> f v
>>
>
> Writing this function given your intention (doing different thing
> depending on the type) is equivalent to writing an instance of
> Foldable for your (MultiMap k). If the underlying structure of your
> multimaps is always foldable, it doesn't make sense to make a function
> like this, the multimaps themselves should be Foldable. This is not
> hard to do, refer to the doc (
> http://www.haskell.org/ghc/docs/7.0-latest/html/libraries/base-4.3.1.0/Data-Foldable.html
> ) to see how to write a minimal instance of Foldable.
>
> --
> Jedaï
>
--
Federico Mastellone
Computer Science Engineer - ITBA
".. there are two ways of constructing a software design: One way is
to make it so simple that there are obviously no deficiencies, and the
other way is to make it so complicated that there are no obvious
deficiencies. The first method is far more difficult."
Tony Hoare, 1980 ACM Turing Award Lecture.
More information about the Beginners
mailing list