Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- abstract class SemiGroup[A] {
- def add(x: A, y: A): A
- }
- abstract class Monoid[A] extends SemiGroup[A] {
- def unit: A
- }
- object ImplicitTest extends App {
- implicit object StringMonoid extends Monoid[String] {
- def add(x: String, y: String): String = x concat y
- def unit: String = ""
- }
- implicit object IntMonoid extends Monoid[Int] {
- def add(x: Int, y: Int): Int = x + y
- def unit: Int = 0
- }
- def sum[A](xs: List[A])(implicit m: Monoid[A]): A =
- if (xs.isEmpty) m.unit
- else m.add(xs.head, sum(xs.tail))
- println(sum(List(1, 2, 3))) // używa IntMonoid
- println(sum(List("a", "b", "c"))) // używa StringMonoid
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement