Created
July 13, 2016 00:22
-
-
Save gclaramunt/ddfad182dd48de45db99026527a92c3d to your computer and use it in GitHub Desktop.
Not a monoid
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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