
Hi all, Is it possible to sum a tuple of monoids for example sum (Just 1, Nothing) expected result Just 1 sum (Just 1, Just 2) expected result Just 3 -- Best Regards, Boon Hui

sum a tuple of monoids
here is something, not quite what you are looking for, but close: Monoid http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Monoid.html#t:Mono... a => Monoid http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Monoid.html#t:Mono... (Maybe http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Maybe.html#t:Maybe a) (Monoid http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Monoid.html#t:Mono... a, Monoid http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Monoid.html#t:Mono... b) => Monoid http://hackage.haskell.org/package/base-4.9.0.0/docs/Data-Monoid.html#t:Mono... (a, b) mappend (Just [1], Just [2,3]) (Nothing, Just [5])::(Maybe [Int],Maybe [Int]) (Just [1],Just [2,3,5])

I don't know if there is generally, but some people would use the lens library to do such things. import Control.Lens
sumOf (each . _Just) (Just 1, Nothing, Just 2) 3
Just keep in mind that each member of the tuple must be the same type.
On Tue, Nov 1, 2016 at 11:42 AM, Lai Boon Hui
Hi all,
Is it possible to sum a tuple of monoids for example
sum (Just 1, Nothing)
expected result Just 1
sum (Just 1, Just 2)
expected result Just 3
-- Best Regards, Boon Hui
_______________________________________________ Beginners mailing list Beginners@haskell.org http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
participants (3)
-
David McBride
-
Imants Cekusins
-
Lai Boon Hui