Skip to content

kevin-margueritte/workshop-functor-applicative-traverse-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commit
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Semigroup & Monoid

Semigroup

A Semigroup has a binary operator. Below the typeclass definition :

trait Semigroup[A] {
  def combine(x: A, y: A): A
}

In this project we define the operator max which return the max between two values.

implicit val max = new Semigroup[Int] {
    override def combine(x: Int, y: Int): Int = if (x < y) x else y
  }

And the syntactical sugar :

implicit class SemigroupOps[A](x: A)(implicit semigroup: Semigroup[A]) {
    def |+|(y: A): A = semigroup.combine(x,y)
  }

Now we can call max in this way :

3 |+| 5
// Int: 5

Monoid

A Monoid extends Semigroup and has an additional value empty

trait Monoid[A] extends Semigroup[A] {
  def empty: A
  def combineAll(l: List[A]): A = l.foldLeft(empty)((x,y) => combine(x,y))
}

For example, this value allows to implement a method combineAll, which allows to apply the combine(x: A, y: A): A in the elements from the list in parameter

Run project

$ sbt
$ test

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages