What will be the result of applying function combine for semigroup of functions Int => Int?
import cats.implicits._
import cats.kernel.Semigroup

Semigroup[Int => Int].combine(_ + 1, _ * 10).apply(6)
Explanation
For Semigroup[A => B] combine function is defined in the following way:
def combine(x: A => B, y: A => B): A => B =
    (a: A) => Semigroup[B].combine(x(a), y(a))
Which means that initial expression boils down to
Semigroup[Int => Int].combine(_ + 1, _ * 10).apply(6) == Semigroup[Int].combine(6 + 1, 6 * 10) == Semigroup[Int].combine(7, 60)
For Semigroup[Int] combine function is defined as sum:
def combine(x: Int, y: Int): Int = x + y
Semigroup[Int].combine(7, 60) == (7 + 60) == 67

Source: Scala Exercises: cats semigroup

Follow CodeGalaxy

Mobile Beta

Get it on Google Play
Send Feedback
Keep exploring
Scala quizzes
Cosmo
Sign Up Now
or Subscribe for future quizzes