Skip to content

Instantly share code, notes, and snippets.

@lyricallogical
Last active December 10, 2015 22:38
Show Gist options
  • Save lyricallogical/4503565 to your computer and use it in GitHub Desktop.
Save lyricallogical/4503565 to your computer and use it in GitHub Desktop.
partialfunction

Partial Function 便利だなーと思うとき

  • 単純に domain でかい

    • まあ普通
  • 内部実装と外部に見せたいシグネチャで domain が異なる場合

    • json みたいな自己参照ふくむ値コンストラクタ必要なデータに対する処理でよくやる
    • Partial Function ないとコードの重複が生じちゃう
      • open recursion 使えば部分関数実現できるので、頑張れば重複なくせると思うけどここまでシンプルにかけない
sealed trait N
case class A(ns: Seq[N]) extends N
case class B(b: Int) extends N
case object C extends N
def f(a: A) : Nanka = {
  val impl: PartialFunction[N => Nanka] = pf.orElse { ... }
  val pf: PartialFunction[A => Nanka] = { ... impl(); ... }
  pf(a)
}

他にもまあなんかこまごまあるけど書くの疲れた。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment