ScalaにはFunction.constという常に一定の値を返す関数があります。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
val one = Function.const(1)_ | |
one(2) assert_=== 1 | |
one("hoge") assert_=== 1 |
この関数を型レベルで表したものがScalazにKonstとして定義されています。
Konst
NaturalTransformationを例にKonstを使ってみます。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
new (List ~> Option) { | |
def apply[A](list: List[A]) = list.headOption | |
} | |
new (List ~> Konst[Int]#Apply) { | |
def apply[A](list: List[A]) = list.size | |
} | |
new (Konst[String]#Apply ~> Konst[Int]#Apply) { | |
def apply[A](string: String) = string.size | |
} |
あまり使わなさそうですね!
私としてはまだScalaz6にあったPartialApplyの方が便利な気がするのですが。
まあ、型推論がうまくいかなかったり、Unapplyが新しく入ったりしたことが原因にあるのでしょうね。
0 件のコメント:
コメントを投稿