Fahad/CodeSamples/ImplicitParameters.scala
author Chengsong
Sat, 22 Jan 2022 22:57:28 +0000
changeset 395 5bffeacdf17e
parent 44 a751aa1ee4f7
permissions -rw-r--r--
preserves!

package Main

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 {
      println("head: " + xs.head);
      println("tail: " + xs.tail);
      m.add(xs.head, sum(xs.tail))
    }

  println(sum(List(1, 2, 3)))
  println(sum(List("a", "b", "c")))
}