Se ha denunciado esta presentación.
Utilizamos tu perfil de LinkedIn y tus datos de actividad para personalizar los anuncios y mostrarte publicidad más relevante. Puedes cambiar tus preferencias de publicidad en cualquier momento.

Semigroups and Monoids in Cats

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

  • Sé el primero en comentar

  • Sé el primero en recomendar esto

Semigroups and Monoids in Cats

  1. 1. Presented By: Mansi Babbar
  2. 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. 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. 4. ● 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?
  5. 5. “combine” in Semigroups
  6. 6. Associativity is the only law for Semigroups Associativity allows us to partition the data any way we want and potentially parallelize the operations.
  7. 7. Semigroups with Strings
  8. 8. Semigroups with Integers
  9. 9. Semigroups with Integers
  10. 10. Semigroups with Collections
  11. 11. 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).
  12. 12. ● 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?
  13. 13. 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.
  14. 14. “empty” in Monoids
  15. 15. Why “identity” or “empty” ? Empty string passed in combineStrings method is known as identity of empty value.
  16. 16. Monoids follow Associativity and Identity Laws combine operation has to be associative and empty value should be an identity for the combine operation:
  17. 17. References ● https://typelevel.org/cats/ ● http://underscore.io/books/scala-with-cats/ ● https://github.com/typelevel/cats

×