Win a copy of Murach's MySQL this week in the JDBC and Relational Databases forum!
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
• Campbell Ritchie
• Jeanne Boyarsky
• Ron McLeod
• Paul Clapham
• Liutauras Vilda
Sheriffs:
• paul wheaton
• Rob Spoor
• Devaka Cooray
Saloon Keepers:
• Stephan van Hulst
• Tim Holloway
• Carey Brown
• Frits Walraven
• Tim Moores
Bartenders:
• Mikalai Zaikin

# Scala foldLeft & foldRight Monoids

Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Are foldLeft and foldRight Monoidic operations in Scala? If I understand Monoid correctly, it is any type that obeys the algebraic laws of associativity and identity.

What the above two represent is just two functions defined as:

If I consider foldLeft and foldRight, don't they do the same by obeying the laws of Monoids?

Ranch Hand
Posts: 121
12
• Number of slices to send:
Optional 'thank-you' note:
Hi, Joe.

Monoid is type and operation which obeys monoid laws. Type alone do not define a monoid. For example, (Int, +) and (Int, *) are monoids for the Int type. Monoid rules define relationships between values and functions. They do not define an "identity function" itself. So it's not clear what do you mean when you say that rules could be represented by two functions.

Anyway, folds are not related to monoids. You could apply foldLeft operation to any collection of items and any operation. For monoid type and operation and for ordered collections (all these should apply at the same time) left and right folds would produce same result. But you can apply these folds for non-monoid operation on the type. For example, you could fold List[Int] using divide operation. So these operations are not specific (or related) to monoids at all.

Joe San
Ranch Hand
Posts: 10198
3
• Number of slices to send:
Optional 'thank-you' note:
Where do I find some good use cases for Monoids?