Skip to content

Commit 37f3bad

Browse files
committed
Redefine equals for Lst
1 parent 413b9c6 commit 37f3bad

File tree

2 files changed

+54
-50
lines changed

2 files changed

+54
-50
lines changed

tests/run/lst/Lst.scala

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -448,23 +448,27 @@ class Lst[+T](val elems: Any) extends AnyVal { self =>
448448
}
449449
}
450450

451-
def === [U](that: Lst[U]) =
452-
(this `eqLst` that) || {
453-
elems match {
454-
case elems1: Arr =>
455-
that.elems match {
456-
case elems2: Arr =>
457-
val len = elems1.length
458-
len == elems2.length && {
459-
var i = 0
460-
while (i < len && elems1(i).equals(elems2(i))) i += 1
461-
i == len
462-
}
463-
case _ => false
464-
}
465-
case elem => elem == that.elems
451+
override def equals(that: Any) = that match {
452+
case that: Lst[_] =>
453+
(this `eqLst` that) || {
454+
elems match {
455+
case elems1: Arr =>
456+
that.elems match {
457+
case elems2: Arr =>
458+
val len = elems1.length
459+
len == elems2.length && {
460+
var i = 0
461+
while (i < len && elems1(i).equals(elems2(i))) i += 1
462+
i == len
463+
}
464+
case _ => false
465+
}
466+
case elem => elem == that.elems
467+
}
466468
}
467-
}
469+
case _ =>
470+
false
471+
}
468472

469473
def eqLst[U](that: Lst[U]) = eq(elems, that.elems)
470474

tests/run/lst/LstTest.scala

Lines changed: 34 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ object Test extends App {
178178
val ys1a = xs1.filterNot(_.head >= 'c')
179179
assert(ys1a `eqLst` xs1)
180180
val ys5 = xs5.filter(_.head % 2 != 0)
181-
assert(ys5 === Lst("a", "c", "e"), ys5)
181+
assert(ys5 == Lst("a", "c", "e"), ys5)
182182

183183
val js0 = is0.filter(_ > 3)
184184
assert(js0.isEmpty)
@@ -187,7 +187,7 @@ object Test extends App {
187187
val js1a = is1.filterNot(_ > 3)
188188
assert(js1a `eqLst` is1)
189189
val js5 = is5.filter(_ % 2 != 0)
190-
assert(js5 === Lst(1, 3, 5), js5)
190+
assert(js5 == Lst(1, 3, 5), js5)
191191

192192
val yss0 = xss0.filter(_.nonEmpty)
193193
assert(yss0.isEmpty)
@@ -196,9 +196,9 @@ object Test extends App {
196196
val yss1a = xss1.filterNot(_.head.head >= 'c')
197197
assert(yss1a `eqLst` xss1)
198198
val yss5 = xss5.filter(_.head.head % 2 != 0)
199-
assert(yss5 === Lst(Lst("a"), Lst("c"), Lst("e")), yss5)
199+
assert(yss5 == Lst(Lst("a"), Lst("c"), Lst("e")), yss5)
200200
val yss5a = xss5.map(_.filter(_.head % 2 != 0))
201-
assert(yss5a === Lst(Lst("a"), Lst(), Lst("c"), Lst(), Lst("e")), yss5)
201+
assert(yss5a == Lst(Lst("a"), Lst(), Lst("c"), Lst(), Lst("e")), yss5)
202202
}
203203

204204
def existsTest() = {
@@ -267,23 +267,23 @@ object Test extends App {
267267
assert(is1.reduceLeft(_ + _) == 1)
268268
assert(is3.reduceLeft(_ + _) == 6)
269269

270-
assert(xss0.foldLeft(Lst("x"))(_ ++ _) === Lst("x"))
271-
assert(xss1.foldLeft(Lst("x"))(_ ++ _) === Lst("x", "a"))
272-
assert(xss2.foldLeft(Lst(): Lst[String])(_ ++ _) === Lst("a", "b"))
273-
assert(xss3.foldLeft(Lst.Empty: Lst[String])(_ ++ _) === Lst("a", "b", "c"))
274-
assert((Lst("x") /: xss0)(_ ++ _) === Lst("x"))
275-
assert((Lst("x") /: xss1)(_ ++ _) === Lst("x", "a"))
276-
assert((Lst("x") /: xss2)(_ ++ _) === Lst("x", "a", "b"))
277-
assert((Lst("x") /: xss3)(_ ++ _) === Lst("x", "a", "b", "c"))
278-
assert(xss1.reduceLeft(_ ++ _) === Lst("a"))
279-
assert(xss3.reduceLeft(_ ++ _) === Lst("a", "b", "c"))
270+
assert(xss0.foldLeft(Lst("x"))(_ ++ _) == Lst("x"))
271+
assert(xss1.foldLeft(Lst("x"))(_ ++ _) == Lst("x", "a"))
272+
assert(xss2.foldLeft(Lst(): Lst[String])(_ ++ _) == Lst("a", "b"))
273+
assert(xss3.foldLeft(Lst.Empty: Lst[String])(_ ++ _) == Lst("a", "b", "c"))
274+
assert((Lst("x") /: xss0)(_ ++ _) == Lst("x"))
275+
assert((Lst("x") /: xss1)(_ ++ _) == Lst("x", "a"))
276+
assert((Lst("x") /: xss2)(_ ++ _) == Lst("x", "a", "b"))
277+
assert((Lst("x") /: xss3)(_ ++ _) == Lst("x", "a", "b", "c"))
278+
assert(xss1.reduceLeft(_ ++ _) == Lst("a"))
279+
assert(xss3.reduceLeft(_ ++ _) == Lst("a", "b", "c"))
280280
}
281281

282282
def reverseTest() = {
283-
assert(xs0.reverse === xs0)
284-
assert(xs1.reverse === xs1)
283+
assert(xs0.reverse == xs0)
284+
assert(xs1.reverse == xs1)
285285
assert(xs3.reverse.mkString == "c, b, a", xs3.reverse.mkString)
286-
assert(xs4.reverse.reverse === xs4, xs4.reverse.reverse)
286+
assert(xs4.reverse.reverse == xs4, xs4.reverse.reverse)
287287
}
288288

289289
def applyTest() = {
@@ -304,15 +304,15 @@ object Test extends App {
304304
}
305305

306306
def sliceTest() = {
307-
assert(xs5.slice(2, 4) === Lst("c", "d"))
308-
assert(xs5.drop(4) === Lst("e"))
307+
assert(xs5.slice(2, 4) == Lst("c", "d"))
308+
assert(xs5.drop(4) == Lst("e"))
309309
assert(xs5.take(4).mkString("") == "abcd")
310310
assert(xs5.drop(-1) `eqLst` xs5)
311311
assert(xs1.take(1) `eqLst` xs1)
312312
assert(xs0.take(10).length == 0)
313313

314-
assert(is5.slice(2, 4) === Lst(3, 4))
315-
assert(is5.drop(4) === Lst(5))
314+
assert(is5.slice(2, 4) == Lst(3, 4))
315+
assert(is5.drop(4) == Lst(5))
316316
assert(is5.take(4).mkString("") == "1234")
317317
assert(is5.drop(-1) `eqLst` is5)
318318
assert(is1.take(1) `eqLst` is1)
@@ -323,34 +323,34 @@ object Test extends App {
323323
val ys4a = xs4.zipWith(xs5)(_ + _)
324324
val ys4b = xs5.zipWith(xs4)(_ + _)
325325
assert(ys4a.mkString("") == "aabbccdd", ys4a)
326-
assert(ys4a === ys4b)
326+
assert(ys4a == ys4b)
327327
val ys1a = xs1.zipWith(xs1)(_ + _)
328-
assert(ys1a === Lst("aa"))
328+
assert(ys1a == Lst("aa"))
329329
val ys1b = xs1.zipWith(xs2)(_ + _)
330-
assert(ys1b === Lst("aa"))
330+
assert(ys1b == Lst("aa"))
331331
val ys1c = xs2.zipWith(xs1)(_ + _)
332-
assert(ys1c === Lst("aa"))
332+
assert(ys1c == Lst("aa"))
333333
val ys0a = xs1.zipWith(xs0)(_ + _)
334334
val ys0b = xs0.zipWith(xs1)(_ + _)
335335
assert((ys0a ++ ys0b).isEmpty)
336336
val ys3i = xs3.zipWithIndex.map((x, y) => (x, y + 1))
337-
assert(ys3i === Lst(("a", 1), ("b", 2), ("c", 3)), ys3i)
337+
assert(ys3i == Lst(("a", 1), ("b", 2), ("c", 3)), ys3i)
338338

339339
val js4a = is4.zipWith(is5)(_ + _)
340340
val js4b = is5.zipWith(is4)(_ + _)
341341
assert(js4a.mkString("") == "2468", js4a)
342-
assert(js4a === js4b)
342+
assert(js4a == js4b)
343343
val js1a = is1.zipWith(is1)(_ + _)
344-
assert(js1a === Lst(2))
344+
assert(js1a == Lst(2))
345345
val js1b = is1.zipWith(is2)(_ + _)
346-
assert(js1b === Lst(2))
346+
assert(js1b == Lst(2))
347347
val js1c = is2.zipWith(is1)(_ + _)
348-
assert(js1c === Lst(2))
348+
assert(js1c == Lst(2))
349349
val js0a = is1.zipWith(is0)(_ + _)
350350
val js0b = is0.zipWith(is1)(_ + _)
351351
assert((js0a ++ js0b).isEmpty)
352352
val js3i = is3.zipWithIndex.map((x, y) => (x, y + 1))
353-
assert(js3i === Lst((1, 1), (2, 2), (3, 3)), js3i)
353+
assert(js3i == Lst((1, 1), (2, 2), (3, 3)), js3i)
354354
assert(js3i.forall(_ == _))
355355
}
356356

@@ -380,7 +380,7 @@ object Test extends App {
380380
{ val b = new Lst.Buffer[String]
381381
b += "a"
382382
assert(b.size == 1)
383-
assert(b.toLst === Lst("a"))
383+
assert(b.toLst == Lst("a"))
384384
b += "aa"
385385
b ++= Lst.fill(20)("a")
386386
assert(b.toLst.mkString("") == "a" * 23)
@@ -390,7 +390,7 @@ object Test extends App {
390390
{ val b = new Lst.Buffer[Int]
391391
b += 1
392392
assert(b.size == 1)
393-
assert(b.toLst === Lst(1))
393+
assert(b.toLst == Lst(1))
394394
b += 11
395395
b ++= Lst.fill(20)(1)
396396
assert(b.toLst.mkString("") == "1" * 23)
@@ -400,7 +400,7 @@ object Test extends App {
400400
{ val b = new Lst.Buffer[Lst[String]]
401401
b += Lst("a")
402402
assert(b.size == 1)
403-
assert(b.toLst === Lst(Lst("a")))
403+
assert(b.toLst == Lst(Lst("a")))
404404
b += Lst("aa")
405405
b ++= Lst.fill(20)(Lst("a"))
406406
assert(b.toLst.map(_.mkString("")).mkString("") == "a" * 23)

0 commit comments

Comments
 (0)