【发布时间】:2019-05-23 21:46:36
【问题描述】:
遇到了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))
注意 - 来自DSLs in Action的代码
andThen 强大吗?基于这个例子,它看起来像mm.andThen 去糖到x => mm.apply(x)。如果andThen有更深层次的含义,那我还没看懂。
【问题讨论】:
标签: scala function-composition