Skip to content

Instantly share code, notes, and snippets.

@matsu-chara
Created July 25, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save matsu-chara/e58aeac4ea7f61dfe934 to your computer and use it in GitHub Desktop.
Save matsu-chara/e58aeac4ea7f61dfe934 to your computer and use it in GitHub Desktop.
Monoid
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