# Scala的Higher-Kinded类型

04/11 20:52

Scala的Higher-Kinded类型

Higher-Kinded从字面意思上看是更高级的分类，也就是更高一级的抽象。我们先看个例子。

``````def sum(seq: Seq[Int]): Int = seq reduce (_ + _)

sum(Vector(1,2,3,4,5)) // 结果值: 15``````

``````trait Add[T] {
def add(t1: T, T2: T): T
}``````

``````object Add {
def add(i1: Int, i2: Int): Int = i1 + i2
}
def add(p1: (Int,Int), p2: (Int,Int)): (Int,Int) =
(p1._1 + p2._1, p1._2 + p2._2)
}
}``````

``````def sumSeq[T : Add](seq: Seq[T]): T =

``````sumSeq(Vector(1 -> 10, 2 -> 20, 3 -> 30)) // 结果值: (6,60)
sumSeq(1 to 10) // 结果值: 55
sumSeq(Option(2)) // 出错!``````

sumSeq可以接受Seq[Int]和Seq[(Int,Int)]类型，但是无法接收Option。

``````trait Reduce[T, -M[T]] {
def reduce(m: M[T])(f: (T, T) => T): T
}
object Reduce {
implicit def seqReduce[T] = new Reduce[T, Seq] {
def reduce(seq: Seq[T])(f: (T, T) => T): T = seq reduce f
}
implicit def optionReduce[T] = new Reduce[T, Option] {
def reduce(opt: Option[T])(f: (T, T) => T): T = opt reduce f
}
}``````

``````def sum[T : Add, M[T]](container: M[T])(
implicit red: Reduce[T,M]): T =

``````sum(Vector(1 -> 10, 2 -> 20, 3 -> 30)) // 结果值: (6,60)
sum(1 to 10) // 结果值: 55
sum(Option(2)) // 结果值: 2
sum[Int,Option](None) // 错误!``````

``````trait Reduce1[-M[_]] {
def reduce[T](m: M[T])(f: (T, T) => T): T
}
object Reduce1 {
implicit val seqReduce = new Reduce1[Seq] {
def reduce[T](seq: Seq[T])(f: (T, T) => T): T = seq reduce f
}
implicit val optionReduce = new Reduce1[Option] {
def reduce[T](opt: Option[T])(f: (T, T) => T): T = opt reduce f
}
}``````

M[_]是上篇文章我们讲到的存在类型。T 参数被移至reduce 方法。

``````def sum[T : Add, M[_] : Reduce1](container: M[T]): T =

M[_]就是我们经常会看到的higher-kinded， higher-kinded虽然带给我们额外的抽象，但是使代码变得更加复杂。大家可以酌情考虑是否需要使用。

0
0 收藏

0 评论
0 收藏
0