Slide 21
Slide 21 text
1 // Type-class for combining values of the same type
2 trait Combine[A] {
3 def combine(a1: A, a2: => A): A
4 def start: A
5 }
6
7 // Type-class instance for Int
8 object IntCombine extends Combine[Int] {
9 def combine(a1: Int, a2: Int): Int = a1 + a2
10 val start: Int = 0
11 }
12
13 def specialSum[A](xs: List[Option[A]])(C: Combine[A]): Option[A] = {
14
15 def go(acc: Option[A], rem: List[Option[A]]): Option[A] =
16 rem match {
17 case y :: ys => (acc, y) match {
18 case (Some(a), Some(i)) => go(Some(C.combine(a, i)), ys)
19 case (Some(a), None) => go(Some(a), ys)
20 case (None, Some(i)) => go(Some(i), ys)
21 case (None, None) => go(None, ys)
22 }
23 case Nil => acc
24 }
25
26 go(none[A], xs)
27 }
28
29 // e.g.
30 val teamReputation: Int =
31 specialSum(reputations)(IntCombine).getOrElse(0) // 27167