scalaz day0

trait Monoid[A] {
def mappend(a1: A, a2: A): A
def mzero: A
}

object Monoid {
implicit val IntMonoid: Monoid[Int] = new Monoid[Int] {
def mappend(a: Int, b: Int): Int = a + b
def mzero: Int = 0
}
implicit val StringMonoid: Monoid[String] = new Monoid[String] {
def mappend(a: String, b: String): String = a + b
def mzero: String = “”
}
}
def sum[A: Monoid](xs: List[A]

: A = {
val m = implicitly[Monoid[A]]
xs.foldLeftm.mzero)m.mappend)
}

println”Int Sum: ” + sumList”a”, “b”, “c”)))

val multiMonoid: Monoid[Int] = new Monoid[Int] {
def mappenda: Int, b: Int): Int = a * b
def mzero: Int = 1
}

println”String Mul: ” + sumList1, 2, 3, 4))multiMonoid))

trait FoldLeft[F[_]] {
def foldLeft[A, B](xs: F[A], b: B, f: (B, A) => B): B
}
object FoldLeft {
implicit val FoldLeftList: FoldLeft[List] = new FoldLeft[List] {
def foldLeft[A, B](xs: List[A], b: B, f: (B, A) => B) = xs.foldLeft(b)(f)
}
}
def sum[M[_]: FoldLeft, A: Monoid](xs: M[A]

: A = {
val m = implicitly[Monoid[A]]
val fl = implicitly[FoldLeft[M]]
fl.foldLeftxs, m.mzero, m.mappend)
}

println”Int sum: ” + sumList1, 2, 3, 4)))
println”String sum: ” + sumList”a”, “b”, “c”)))

def plus[A: Monoid]a: A, b: A): A = implicitly[Monoid[A]].mappenda,b)

println”plus: ” + plus3,4))

trait MonoidOp[A] {
val F: Monoid[A]
val value: A
def |+|(a2: A) = F.mappend(value, a2)
}

implicit def toMonoidOp[A: Monoid](a: A): MonoidOp[A] = new MonoidOp[A] {
val F = implicitly[Monoid[A]]
val value = a
}

val x = 3 |+| 4
val y = “a” |+| “b”

println(“3 |+| 4: ” + x)
println(“a |+| b: ” + y)

Advertisements