Created
July 25, 2015 14:48
-
-
Save matsu-chara/e58aeac4ea7f61dfe934 to your computer and use it in GitHub Desktop.
Monoid
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
object MonoidComponent { | |
// モノイドの定義 | |
trait Monoid[M] { | |
def <>(m1: M, m2: M): M | |
val mempty: M | |
} | |
// Monoid[Int]のインスタンス定義 | |
implicit val monoidIntInstance = new Monoid[Int] { | |
override def <>(m1: Int, m2: Int): Int = m1 + m2 | |
override val mempty = 0 | |
} | |
// <>(1, 2)のように呼び出せるようにしたいので便利関数を定義 | |
def <>[M: Monoid](m1: M, m2: M): M = implicitly[Monoid[M]].<>(m1, m2) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment