けんろん!
圏論の基礎を読んでいるのですが高校数学では太刀打ち出来ませぬ。
Categoryはcompose(合成関数)、id(恒等関数)を持ちます。
MAB#>>>, MAB#<<<, MAB#⋙, MAB#⋘
(f >>> g).apply(x)はg(f(x))、(f <<< g)(x)はf(g(x))と同じです。
⋙は>>>の、⋘は<<<のエイリアスです。
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
lazy val f: Int => Int = _ + 2 | |
lazy val g: Int => Int = _ * 2 | |
(f >>> g).apply(5) assert_=== 14 | |
(f <<< g).apply(5) assert_=== 12 | |
(f.promise >>> g.promise).apply(3).get assert_=== (f.promise >=> g.promise).apply(3).get |
Endo
Endomorphismと言うらしい?
早く可換図に書いてあることがわかるようになりたいですね・・・
EndoはA => AのNewType。
つまり、入力と出力が同じ関数です。
Function1W#endo
Function1[A, A]をEndo[A]に変換します。
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
(f |+| g).apply(5) assert_=== 17 | |
(f.endo |+| g.endo).apply(5) assert_=== 12 | |
mzero[Endo[Int]].apply(5) assert_=== 5 |
EndoはMonoidで、|+|は関数合成、mzeroは恒等関数が返ります。
Dual
そうつい!
DualはMonidで、結合時に左辺と右辺を入れ替えます。
Identity#dual, Identity#σ
dualはDualのインスタンスを返します。
σはdualのエイリアスです。
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
"1" |+| "2" assert_=== "12" | |
(("1".dual |+| "2".dual): String) assert_=== "21" | |
(f.endo.dual |+| g.endo.dual).apply(5) assert_=== 14 |
Dualがとる型はMonoidに限りませんが、今のところMonoid以外で特殊な動きはしません。
今回はCategoryはあまり深くまで調べませんでしたが、Categoryオブジェクトをみると意味不明用途不明なものがたくさんあります。
使い道などがわかったら是非教えて下さい!
0 件のコメント:
コメントを投稿