Today we are going to deep dive into the Scala Cats again to find out about Monoids. In the session about Semigroups, we saw an example where we’re using Semigroups along with Scala’s fold() to operate on a collection of values
2. Lack of etiquette and manners is a huge turn off.
KnolX Etiquettes
Punctuality
Respect Knolx session timings, you
are requested not to join sessions
after a 5 minutes threshold post
the session start time.
Feedback
Make sure to submit a constructive
feedback for all sessions as it is
very helpful for the presenter.
Mute
Please keep your window on mute
Avoid Disturbance
Avoid leaving your window
unmuted after asking a question
3. Agenda
What are Semigroups?
03 Limitations of Semigroups
04 What are Monoids
Associativity law of Semigroups
02
01
05 Identity law of Monoids
4.
5. ● A concept which encapsulates aggregation with associative binary
operation.
● Comes with method combine which combines two values of same data
type following the principle of associativity.
What are Semigroups?
9. Associativity is the only law for Semigroups
Associativity allows us to partition the data any way we want and
potentially parallelize the operations.
16. Limitation of Semigroups
We cannot write a generic method combineAll(collection: Seq[A]): [A] for
the above expression.
Because the fallback value will depend on the type of A
(”” for String, 0 for Int, etc).
17.
18. ● Extends Semigroup and adds a default or fallback value for given type.
● Inherits combine method of Semigroups and comes with new method
empty which performs identity operation.
What are Monoids?
19.
20. Why Monoids?
While using Semigroups with Scala’s fold() to operator on collection of
values, we cannot write a generic method combineAll(collection: Seq[A]):
[A] because fallback value will depend on the type of Semigroup.
24. Why “identity” or “empty” ?
Empty string passed in combineStrings method is known as identity of
empty value.
25.
26. Monoids follow Associativity and Identity Laws
combine operation has to be associative and empty value should be an
identity for the combine operation: