Permalink
Browse files

Merge pull request #657 from adelbertc/const

Add Const functor and const function
  • Loading branch information...
2 parents f04b418 + 2d9b40e commit ab1eb86e700f70d8523db55796055d609b6bfeb9 @larsrh larsrh committed Mar 9, 2014
@@ -0,0 +1,35 @@
+package scalaz
+
+import Id._
+
+case class Const[A, B](getConst: A) {
+ def ===(x: Const[A, B])(implicit A: Equal[A]): Boolean =
+ A.equal(getConst, x.getConst)
+
+ def map[C](f: B => C): Const[A, C] = Const(getConst)
+}
+
+object Const extends ConstInstances with ConstFunctions
+
+sealed abstract class ConstInstances {
+ /** The constant functor that maps every type to `A` */
+ implicit def constFunctor[C]: Functor[({type l[a] = Const[C, a]})#l] =
+ new Functor[({type l[a] = Const[C, a]})#l] {
+ override def map[A, B](fa: Const[C, A])(f: A => B): Const[C, B] =
+ fa.map(f)
+ }
+
+ implicit def constEqual[A : Equal, B]: Equal[Const[A, B]] =
+ new Equal[Const[A, B]] {
+ override def equal(a1: Const[A, B], a2: Const[A, B]): Boolean =
+ a1 === a2
+ }
+}
+
+sealed trait ConstFunctions {
+ /** A properly universally quantified constant function. */
+ def const[A](a: A): Function0 ~> ({type l[_] = A})#l =
+ new (Function0 ~> ({type l[_] = A})#l) {
+ override def apply[B](fa: Function0[B]): A = a
+ }
+}
@@ -241,6 +241,9 @@ object ScalazArbitrary {
implicit def eitherTArb[F[+_], A, B](implicit A: Arbitrary[F[A \/ B]]): Arbitrary[EitherT[F, A, B]] =
Functor[Arbitrary].map(A)(EitherT[F, A, B](_))
+ implicit def constArbitrary[A, B](implicit A: Arbitrary[A]): Arbitrary[Const[A, B]] =
+ Functor[Arbitrary].map(A)(Const(_))
+
implicit def dlistArbitrary[A](implicit A: Arbitrary[List[A]]) = Functor[Arbitrary].map(A)(as => DList(as : _*))
implicit def ilistArbitrary[A](implicit A: Arbitrary[List[A]]) = Functor[Arbitrary].map(A)(IList.fromList)
@@ -0,0 +1,18 @@
+package scalaz
+
+import std.AllInstances._
+import scalaz.scalacheck.ScalazProperties._
+import scalaz.scalacheck.ScalazArbitrary._
+import Const._
+import org.scalacheck.Prop.forAll
+
+object ConstTest extends SpecLite {
+ checkAll(functor.laws[({type l[a] = Const[Int, a]})#l])
+ checkAll(equal.laws[Const[Int, String]])
+
+ "const functions" in {
+ "const" ! forAll { (x: Int, y: Function0[String]) =>
+ const(x)(y) == x
+ }
+ }
+}

0 comments on commit ab1eb86

Please sign in to comment.