Isomorphism
あるcase classに対してMonoidを定義したいとき、大体のものはTupleのMonoidのインスタンスが使えると思います。
こんなときにIsomorphismMonoidが使えます。
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
case class Person(name: String, age: Int) | |
object Person { | |
type T = (String, Int) | |
object PersonTuple extends (Person <=> T) { | |
def from = Function.tupled(Person.apply) | |
def to = Person.unapply _ >>> (_.get) | |
} | |
implicit object PersonMonoid extends IsomorphismMonoid[Person, T] { | |
def G = Monoid[T] | |
def iso = PersonTuple | |
} | |
} |
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
scala> import scalaz._, Scalaz._ | |
import scalaz._ | |
import Scalaz._ | |
scala> mzero[Person] | |
res0: Person = Person(,0) | |
scala> Person("hoge", 2) multiply 3 | |
res1: Person = Person(hogehogehoge,6) |
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
implicit object PersonShow extends IsomorphismShow[Person, T] { | |
def G = Show[T] | |
def iso = PersonTuple | |
} | |
implicit object PersonOrder extends IsomorphismOrder[Person, T] { | |
def G = Order[T] | |
def iso = PersonTuple | |
} |
<=>[A, B]はIso[Function1, A, B]のtype aliasで、to: A => Bとfrom: B => Aを定義します。
IsomorphismMonoidなどの型クラスは、toを使って実装されていることに注意しましょう。
0 件のコメント:
コメントを投稿