Kev*_*ith 19 scala function-composition
我遇到过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,那么我还没有理解它.
Lee*_*Lee 25
andThen只是功能组合.给定一个功能f
val f: String => Int = s => s.length
Run Code Online (Sandbox Code Playgroud)
andThen创建一个新函数,f后跟参数函数
val g: Int => Int = i => i * 2
val h = f.andThen(g)
Run Code Online (Sandbox Code Playgroud)
h(x) 那么 g(f(x))
| 归档时间: |
|
| 查看次数: |
10280 次 |
| 最近记录: |