public
Last active

Equal library for Scala (standalone)

  • Download Gist
Equal.scala
Scala
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
trait Equal[A] {
def equal(a1: A, a2: A): Boolean
 
def apply(a1: A, a2: A): Boolean =
equal(a1, a2)
 
def contramap[B](f: B => A): Equal[B] =
Equal((b1, b2) => apply(f(b1), f(b2)))
 
def &&[B](b: => Equal[B]): Equal[(A, B)] =
Equal((ab1, ab2) => apply(ab1._1, ab2._1) && b(ab1._2, ab2._2))
 
def ||[B](b: => Equal[B]): Equal[(A, B)] =
Equal((ab1, ab2) => apply(ab1._1, ab2._1) || b(ab1._2, ab2._2))
 
def ==>[B](b: => Equal[B]): Equal[(A, B)] =
Equal((ab1, ab2) => !apply(ab1._1, ab2._1) || b(ab1._2, ab2._2))
 
def <==[B](b: => Equal[B]): Equal[(A, B)] =
Equal((ab1, ab2) => apply(ab1._1, ab2._1) || !b(ab1._2, ab2._2))
 
def ===[B](b: Equal[B]): Equal[(A, B)] =
Equal((ab1, ab2) => apply(ab1._1, ab2._1) == b(ab1._2, ab2._2))
 
def =/=[B](b: Equal[B]): Equal[(A, B)] =
negate === b
 
def negate: Equal[A] =
Equal((a1, a2) => !apply(a1, a2))
 
def unary_! : Equal[A] =
negate
 
def zip[B](b: => Equal[B]): Equal[(A, B)] =
&&(b)
 
def notEqual(a1: A, a2: A): Boolean =
negate(a1, a2)
 
def curry: A => A => Boolean =
a1 => a2 => apply(a1, a2)
 
def curryFlip: A => A => Boolean =
a2 => a1 => apply(a1, a2)
 
def run: (A, A) => Boolean =
(a1, a2) => apply(a1, a2)
 
def when1(a1: A, a2: A): Option[A] =
if(apply(a1, a2))
Some(a1)
else
None
 
def when2(a1: A, a2: A): Option[A] =
when1(a2, a1)
 
def unless1(a1: A, a2: A): Option[A] =
negate.when1(a1, a2)
 
def unless2(a1: A, a2: A): Option[A] =
negate.when2(a1, a2)
 
def pick1(a1: A, a2: A): A =
if(apply(a1, a2))
a1
else
a2
 
def pick2(a1: A, a2: A): A =
negate.pick1(a1, a2)
}
 
object Equal extends EqualFunctions with EqualInstances {
def apply[A](f: (A, A) => Boolean): Equal[A] =
new Equal[A] {
def equal(a1: A, a2: A) =
f(a1, a2)
}
 
def always[A](p: => Boolean): Equal[A] =
apply((_, _) => p)
 
def for1[A](f: A => Boolean): Equal[A] =
apply((a1, _) => f(a1))
 
def for2[A](f: A => Boolean): Equal[A] =
apply((_, a2) => f(a2))
 
def curried[A](f: A => A => Boolean): Equal[A] =
apply((a1, a2) => f(a1)(a2))
 
def counzip[A, B](e: Either[Equal[A], Equal[B]]): Equal[Either[A, B]] =
e match {
case Left(ea) => Equal((a1, a2) => a1.left exists (aa1 => a2.left exists (aa2 => ea(aa1, aa2))))
case Right(eb) => Equal((b1, b2) => b1.right exists (bb1 => b2.right exists (bb2 => eb(bb1, bb2))))
}
}
 
trait EqualFunctions {
 
implicit def ToEqualed[A](a: A): Equaled[A] =
Equaled(a)
 
case class EqualingBy[A](a1: A, a2: A) {
def by[B](f: A => B)(implicit E: Equal[B]): Boolean =
E contramap f apply (a1, a2)
}
 
case class NotEqualingBy[A](a1: A, a2: A) {
def by[B](f: A => B)(implicit E: Equal[B]): Boolean =
E contramap f notEqual (a1, a2)
}
 
case class Equaled[A](a: A) {
def ===(a2: A)(implicit E: Equal[A]): Boolean =
E(a, a2)
 
def =/=(a2: A)(implicit E: Equal[A]): Boolean =
E.notEqual(a, a2)
 
def >===(a2: A): EqualingBy[A] =
EqualingBy(a, a2)
 
def >=/=(a2: A): NotEqualingBy[A] =
NotEqualingBy(a, a2)
 
def equal1(implicit E: Equal[A]): A => Boolean =
E.curry(a)
 
def equal2(implicit E: Equal[A]): A => Boolean =
E.curryFlip(a)
 
def when1(a2: A)(implicit E: Equal[A]): Option[A] =
E.when1(a, a2)
 
def when2(a2: A)(implicit E: Equal[A]): Option[A] =
E.when2(a, a2)
 
def unless1(a2: A)(implicit E: Equal[A]): Option[A] =
E.unless1(a, a2)
 
def unless2(a2: A)(implicit E: Equal[A]): Option[A] =
E.when1(a, a2)
 
def pick1(a2: A)(implicit E: Equal[A]): A =
E.pick1(a, a2)
 
def pick2(a2: A)(implicit E: Equal[A]): A =
E.pick2(a, a2)
}
 
}
 
trait EqualInstances {
 
// CAUTION
def objectEquals[A]: Equal[A] = new Equal[A] {
def equal(a1: A, a2: A): Boolean = a1 == a2
}
 
// Scala primitives
implicit val BooleanEqual: Equal[Boolean] = objectEquals
implicit val ByteEqual: Equal[Byte] = objectEquals
implicit val CharEqual: Equal[Char] = objectEquals
implicit val DoubleEqual: Equal[Double] = objectEquals
implicit val FloatEqual: Equal[Float] = objectEquals
implicit val IntEqual: Equal[Int] = objectEquals
implicit val LongEqual: Equal[Long] = objectEquals
implicit val NothingEqual: Equal[Nothing] = Equal.always(true)
implicit val ShortEqual: Equal[Short] = objectEquals
implicit val UnitEqual: Equal[Unit] = Equal.always(true)
 
// Java primitives
implicit val JavaBooleanEqual: Equal[java.lang.Boolean] = objectEquals
implicit val JavaByteEqual: Equal[java.lang.Byte] = objectEquals
implicit val JavaCharEqual: Equal[java.lang.Character] = objectEquals
implicit val JavaDoubleEqual: Equal[java.lang.Double] = objectEquals
implicit val JavaFloatEqual: Equal[java.lang.Float] = objectEquals
implicit val JavaIntEqual: Equal[java.lang.Integer] = objectEquals
implicit val JavaLongEqual: Equal[java.lang.Long] = objectEquals
implicit val JavaShortEqual: Equal[java.lang.Short] = objectEquals
 
// big numbers
implicit val BigDecimalEqual: Equal[BigDecimal] = objectEquals
implicit val BigIntegerEqual: Equal[BigInt] = objectEquals
 
// products
implicit def Function0Equal[A: Equal]: Equal[Function0[A]] =
implicitly[Equal[A]].contramap(_())
implicit def Tuple2Equal[A: Equal, B: Equal]: Equal[(A, B)] =
implicitly[Equal[A]] && implicitly[Equal[B]]
implicit def Tuple3Equal[A: Equal, B: Equal, C: Equal]: Equal[(A, B, C)] =
implicitly[Equal[A]] && implicitly[Equal[B]] && implicitly[Equal[C]] contramap {
case (a, b, c) => ((a, b), c)
}
implicit def Tuple4Equal[A: Equal, B: Equal, C: Equal, D: Equal]: Equal[(A, B, C, D)] =
implicitly[Equal[A]] && implicitly[Equal[B]] && implicitly[Equal[C]] && implicitly[Equal[D]] contramap {
case (a, b, c, d) => (((a, b), c), d)
}
implicit def Tuple5Equal[A: Equal, B: Equal, C: Equal, D: Equal, E: Equal]: Equal[(A, B, C, D, E)] =
implicitly[Equal[A]] && implicitly[Equal[B]] && implicitly[Equal[C]] && implicitly[Equal[D]] && implicitly[Equal[E]] contramap {
case (a, b, c, d, e) => ((((a, b), c), d), e)
}
 
// sums
implicit def OptionEqual[A: Equal]: Equal[Option[A]] =
Equal((x1, x2) => x1 match {
case None => x2.isEmpty
case Some(a1) => x2 exists (implicitly[Equal[A]].curry(a1))
})
implicit def EitherEqual[A: Equal, B: Equal]: Equal[Either[A, B]] =
Equal((x1, x2) =>
x1 match {
case Left(a1) => x2.left exists (implicitly[Equal[A]].curry(a1))
case Right(b1) => x2.right exists (implicitly[Equal[B]].curry(b1))
})
implicit def ListEqual[A: Equal]: Equal[List[A]] =
Equal((x1, x2) => (x1 corresponds x2)(implicitly[Equal[A]].run))
implicit def StreamEqual[A: Equal]: Equal[Stream[A]] =
Equal((x1, x2) => (x1 corresponds x2)(implicitly[Equal[A]].run))
 
// collections
implicit def QueueEqual[A: Equal]: Equal[collection.immutable.Queue[A]] =
Equal((x1, x2) => (x1 corresponds x2)(implicitly[Equal[A]].run))
implicit def StackEqual[A: Equal]: Equal[collection.immutable.Stack[A]] =
Equal((x1, x2) => (x1 corresponds x2)(implicitly[Equal[A]].run))
 
// strings
implicit val StringEqual: Equal[String] = objectEquals
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.