Sum a tuple of Monoids

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

Sum a tuple of Monoids

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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Sum a tuple of Monoids

Imants Cekusins
sum a tuple of monoids

here is something, not quite what you are looking for, but close:

Monoid a => Monoid (Maybe a)
(Monoid a, Monoid b) => Monoid (a, b)

mappend (Just [1], Just [2,3]) (Nothing, Just [5])::(Maybe [Int],Maybe [Int])
(Just [1],Just [2,3,5])


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Sum a tuple of Monoids

David McBride
In reply to this post by Lai Boon Hui
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 <[hidden email]> wrote:
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
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners



_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners