--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/Fahad/CodeSamples/ImplicitParameters.scala Sun Nov 09 19:25:10 2014 +0000
@@ -0,0 +1,32 @@
+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")))
+}
\ No newline at end of file