25

に出くわしましandThenたが、よくわかりませんでした。

さらに詳しく調べるために、Function1.andThenドキュメントを読みました

def andThen[A](g: (R) ⇒ A): (T1) ⇒ A

mmMultiMapインスタンスです。

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、私はまだそれを理解していません。

4

1 に答える 1

34

andThen単なる関数合成です。与えられた関数f

val f: String => Int = s => s.length

andThenf引数関数が後に適用される新しい関数を作成します

val g: Int => Int = i => i * 2

val h = f.andThen(g)

h(x)その時はg(f(x))

于 2013-11-29T19:51:42.147 に答える