Skip to content

Instantly share code, notes, and snippets.

@drstevens
Created September 18, 2014 18:31
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 drstevens/2b6a9686881c1183ed55 to your computer and use it in GitHub Desktop.
Save drstevens/2b6a9686881c1183ed55 to your computer and use it in GitHub Desktop.
Reduce boilerplate when not using primitives
import scalaz.Id.Id
import scalaz.BijectionT._
import scalaz.Lens
trait Bijectionz[A, C] {
def apply(str: C): A
def unapply(a: A): Option[C]
val valueLens: Lens[A, C] = bijectionId.lens
implicit def bijectionId: Bijection[A, C] = bijection[Id, Id, A, C](a => unapply(a).get, apply)
}
case class UserId(value: Long)
object UserId extends Bijectionz[UserId, Long]
case class UserName(value: String)
object UserName extensd Bijectionz[UserName, String]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment