我遇到过andThen
,但没有正确理解。
为了进一步了解它,我阅读了Function1.andThen文档
def andThen[A](g: (R) ⇒ A): (T1) ⇒ A
mm
是一个MultiMap实例。
scala> mm
res29: scala.collection.mutable.HashMap[Int,scala.collection.mutable.Set[String]] with scala.collection.mutable.MultiMap[Int,String] =
Map(2 -> Set(b) , 1 -> Set(c, a))
scala> mm.keys.toList.sortWith(_ < _).map(mm.andThen(_.toList))
res26: List[List[String]] = List(List(c, a), List(b))
scala> mm.keys.toList.sortWith(_ < _).map(x => mm.apply(x).toList)
res27: List[List[String]] = List(List(c, a), List(b))
注意 - 来自DSL 的代码在行动中
andThen
强大吗?根据这个例子,它看起来像是mm.andThen
对x => mm.apply(x)
. 如果还有更深层次的含义andThen
,那我还没有理解。