相关疑难解决方法(0)

理解`andThen`

我遇到过andThen,但没有正确理解它.

为了进一步研究,我阅读了Function1.andThen 文档.

def andThen[A](g: (R) ? A): (T1) ? A
Run Code Online (Sandbox Code Playgroud)

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))
Run Code Online (Sandbox Code Playgroud)

注 - 来自行动中的DSL的代码

andThen强大的?基于这个例子,看起来像mm.andThen去糖x => mm.apply(x).如果有更深刻的含义andThen,那么我还没有理解它.

scala function-composition

19
推荐指数
1
解决办法
1万
查看次数

标签 统计

function-composition ×1

scala ×1