Skip to content

Instantly share code, notes, and snippets.

@gclaramunt
Created July 13, 2016 00:22
Show Gist options
  • Save gclaramunt/ddfad182dd48de45db99026527a92c3d to your computer and use it in GitHub Desktop.
Save gclaramunt/ddfad182dd48de45db99026527a92c3d to your computer and use it in GitHub Desktop.
Not a monoid
abstract class Blarhg[A] {
def add(x: A, y: A): A
}
abstract class Blejjj[A] extends Blarhg[A] {
def unit: A
}
object ImplicitTest extends App {
implicit object StringBlejjj extends Blejjj[String] {
def add(x: String, y: String): String = x concat y
def unit: String = ""
}
implicit object IntBlejjj extends Blejjj[Int] {
def add(x: Int, y: Int): Int = x + y
def unit: Int = 0
}
def sum[A](xs: List[A])(implicit m: Blejjj[A]): A =
if (xs.isEmpty) m.unit
else m.add(xs.head, sum(xs.tail))
println(sum(List(1, 2, 3)))
println(sum(List("a", "b", "c")))
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment