Design Question: fmap with two lists

I have a class that is basically: data TwoLists = TL {doubleList :: [Double], intList :: [Int]} Almost all of the operations of this type map, fold, etc on one of the lists or the other, and then return a new instance of the type. So I am implementing to functions dFmap, and iFmap. I actually would like all of the list functions, but I don't want to reimplement them. So my question is, what is the haskell way of handling a type like this. Is there some sort of TwoTypeTraversable I should be deriving from? Essential is there a Haskell idiom for this situation. -Jonathan

On Thu, 2009-12-24 at 22:21 +0000, jonathanGfischoff@gmail.com wrote:
I have a class that is basically:
Hmm. Class in haskell means something similar to interface in OOP. Yes it is confusing.
data TwoLists = TL {doubleList :: [Double], intList :: [Int]}
Almost all of the operations of this type map, fold, etc on one of the lists or the other, and then return a new instance of the type.
It may be my level of English bit I cannot understend this sentence.
So I am implementing to functions dFmap, and iFmap.
What is dFmap and iFmap?
I actually would like all of the list functions, but I don't want to reimplement them.
Hmm. If you need to combine the lists I'd suggest zip/zipWith. If simply map/fold over one then just write (or similar): foldr f i . doubleList If you mean if there is class: class BiFunctor f where fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d fmap2 f g = fmap2b g . fmap2a f fmap2a :: (a -> c) -> f a b -> f c b fmap2a f = fmap2 f id fmap2b :: (b -> d) -> f a b -> f a d fmap2b = fmap2 id To write: data TwoLists a b = TwoLists [a] [b] instance BiFunctor TwoLists where fmap2 f g (TwoLists a b) = TwoLists (map f a) (map g b) then as far as I know - no - but you can always write your own. Regards

On Fri, Dec 25, 2009 at 01:48:25AM +0100, Maciej Piechotka wrote:
On Thu, 2009-12-24 at 22:21 +0000, jonathanGfischoff@gmail.com wrote:
I have a class that is basically:
Hmm. Class in haskell means something similar to interface in OOP. Yes it is confusing.
data TwoLists = TL {doubleList :: [Double], intList :: [Int]}
Almost all of the operations of this type map, fold, etc on one of the lists or the other, and then return a new instance of the type.
It may be my level of English bit I cannot understend this sentence.
So I am implementing to functions dFmap, and iFmap.
What is dFmap and iFmap?
I actually would like all of the list functions, but I don't want to reimplement them.
Hmm. If you need to combine the lists I'd suggest zip/zipWith. If simply map/fold over one then just write (or similar):
foldr f i . doubleList
If you mean if there is class: class BiFunctor f where fmap2 :: (a -> c) -> (b -> d) -> f a b -> f c d fmap2 f g = fmap2b g . fmap2a f fmap2a :: (a -> c) -> f a b -> f c b fmap2a f = fmap2 f id fmap2b :: (b -> d) -> f a b -> f a d fmap2b = fmap2 id
There is such a class in the category-extras package, although it is so general that it is probably not worth the effort of trying to understand it unless you really need the extra abstraction. -Brent

On Fri, 2009-12-25 at 21:10 -0500, Brent Yorgey wrote:
There is such a class in the category-extras package, although it is so general that it is probably not worth the effort of trying to understand it unless you really need the extra abstraction.
-Brent
For the record - what is the name of it? I tried to search through category-extras twice. Regards

On Sat, Dec 26, 2009 at 06:12:00PM +0100, Maciej Piechotka wrote:
On Fri, 2009-12-25 at 21:10 -0500, Brent Yorgey wrote:
There is such a class in the category-extras package, although it is so general that it is probably not worth the effort of trying to understand it unless you really need the extra abstraction.
-Brent
For the record - what is the name of it? I tried to search through category-extras twice.
Control.Functor.Bifunctor. It took me a while to find it, too (I've had to re-find it several times); I didn't actually say where to find it in my first email because I knew it would take me a long time. Unfortunately, coherent organization is not among category-extras' strengths. ;-) -Brent
participants (3)
-
Brent Yorgey
-
jonathanGfischoff@gmail.com
-
Maciej Piechotka