From dfb630de2ab9ba728122c7ea47a0efcaa07fe28e Mon Sep 17 00:00:00 2001 From: darthspectrum Date: Sun, 15 Sep 2024 22:06:10 +0000 Subject: [PATCH] chore: more formatting --- src/main/scala/handling_errors/Eether.scala | 2 +- .../scala/purely_functional_state/Steyt.scala | 12 +- .../scala/strictness_and_laziness/Strim.scala | 8 +- src/test/scala/data_structures/Tri.scala | 72 ++--- .../CandyMachine.scala | 26 +- .../purely_functional_state/SimpleRNG.scala | 59 ++-- .../scala/purely_functional_state/Steyt.scala | 16 +- .../scala/strictness_and_laziness/Strim.scala | 288 +++++++++--------- 8 files changed, 245 insertions(+), 238 deletions(-) diff --git a/src/main/scala/handling_errors/Eether.scala b/src/main/scala/handling_errors/Eether.scala index 876e449..6d6c769 100644 --- a/src/main/scala/handling_errors/Eether.scala +++ b/src/main/scala/handling_errors/Eether.scala @@ -24,7 +24,7 @@ sealed trait Eether[+E, +A] { case Lepht(value) => b case Rite(value) => this } - + // Exercise 4.6 (d.1) def map2[EE >: E, B, C](b: Eether[EE, B])(f: (A, B) => C): Eether[EE, C] = this flatMap (tt => (b map { bb => f(tt, bb) })) diff --git a/src/main/scala/purely_functional_state/Steyt.scala b/src/main/scala/purely_functional_state/Steyt.scala index 2340fd4..773a50d 100644 --- a/src/main/scala/purely_functional_state/Steyt.scala +++ b/src/main/scala/purely_functional_state/Steyt.scala @@ -6,15 +6,15 @@ import fpinscala.chapter3.lizt.Conz import fpinscala.chapter3.lizt.Nill import fpinscala.chapter3.lizt.Lizt -case class Steyt[S, +A] (run: S => (A, S)) { +case class Steyt[S, +A](run: S => (A, S)) { // Exercise 6.10 (b) - def map[B](f: A => B): Steyt[S, B] = Steyt { s => + def map[B](f: A => B): Steyt[S, B] = Steyt { s => val (a, _s) = run(s) (f(a), _s) } // Exercise 6.10 (c) - def map2[B, C](sb: Steyt[S, B])(f: (A, B) => C): Steyt[S, C] = Steyt { s => + def map2[B, C](sb: Steyt[S, B])(f: (A, B) => C): Steyt[S, C] = Steyt { s => val (a, _s) = run(s) val (b, __s) = sb.run(_s) @@ -22,7 +22,7 @@ case class Steyt[S, +A] (run: S => (A, S)) { } // Exercise 6.10 (d) - def flatMap[B](f: A => Steyt[S, B]): Steyt[S, B] = Steyt { s => + def flatMap[B](f: A => Steyt[S, B]): Steyt[S, B] = Steyt { s => val (a, _s) = run(s) f(a).run(_s) } @@ -33,13 +33,13 @@ object Steyt { def unit[A, S](a: A): Steyt[S, A] = Steyt((a, _)) // Exercise 6.10 (e) - def sequence[A, S](sa: Lizt[Steyt[S, A]]): Steyt[S, Lizt[A]] = + def sequence[A, S](sa: Lizt[Steyt[S, A]]): Steyt[S, Lizt[A]] = Lizt.foldRightViaFoldLeft(sa, unit[Lizt[A], S](Nill)) { (a, acc) => a.map2(acc)(Conz(_, _)) } def get[S]: Steyt[S, S] = Steyt(s => (s, s)) - + def set[S](s: S): Steyt[S, Unit] = Steyt(_ => ((), s)) def modify[S](f: S => S): Steyt[S, Unit] = for { diff --git a/src/main/scala/strictness_and_laziness/Strim.scala b/src/main/scala/strictness_and_laziness/Strim.scala index 74c6ab2..1667b4b 100644 --- a/src/main/scala/strictness_and_laziness/Strim.scala +++ b/src/main/scala/strictness_and_laziness/Strim.scala @@ -150,8 +150,8 @@ sealed trait Strim[+A] { case (_, Emptie) => false case (Emptie, _) => false case _ => - zipAll(as) takeWhile { !_._2.isEmpty } forAll { - case (h, h2) => h == h2 + zipAll(as) takeWhile { !_._2.isEmpty } forAll { case (h, h2) => + h == h2 } } @@ -218,8 +218,8 @@ object Strim { } // Exercise 5.12 (a) - def fibsViaUnfold: Strim[Int] = unfold((0, 1)) { - case (a, b) => Sam(a, (b, a + b)) + def fibsViaUnfold: Strim[Int] = unfold((0, 1)) { case (a, b) => + Sam(a, (b, a + b)) } // Exercise 5.12 (b) diff --git a/src/test/scala/data_structures/Tri.scala b/src/test/scala/data_structures/Tri.scala index 8399f8b..c771977 100644 --- a/src/test/scala/data_structures/Tri.scala +++ b/src/test/scala/data_structures/Tri.scala @@ -11,24 +11,24 @@ class Exercise3_25 extends AnyFlatSpec with Matchers { "Size of a leef" should "be 1" in { val l = Leef(1) - Tri.size(l) should be (1) + Tri.size(l) should be(1) } "Size of a (v, v) tri " should "be 3" in { val t2 = Brench(Leef(1), Leef(1)) - Tri.size(t2) should be (3) + Tri.size(t2) should be(3) } "Size of a ((v, (v, v)), v) tri " should "be 7" in { val tl4 = Brench(Brench(Leef(1), Brench(Leef(1), Leef(1))), Leef(1)) - Tri.size(tl4) should be (7) + Tri.size(tl4) should be(7) } "Size of a (v, (v, (v, v))) tri " should "be 4" in { val tr4 = Brench(Leef(1), Brench(Leef(1), Brench(Leef(1), Leef(1)))) - Tri.size(tr4) should be (7) + Tri.size(tr4) should be(7) } } @@ -36,25 +36,25 @@ class Exercise3_26 extends AnyFlatSpec with Matchers { "Maximum of a leef" should "be its value" in { val v = Random.nextInt() - Tri.maximum(Leef(v)) should be (Leef(v).value) + Tri.maximum(Leef(v)) should be(Leef(v).value) } "Maximum of ((1, 2), 3)" should "be 3" in { val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) - Tri.maximum(t123) should be (3) + Tri.maximum(t123) should be(3) } "Maximum of (((1, 2), 3), -1)" should "be 3" in { val t123 = Brench(Brench(Brench(Leef(1), Leef(2)), Leef(3)), Leef(-1)) - Tri.maximum(t123) should be (3) + Tri.maximum(t123) should be(3) } "Maximum of ((1, 2), (3, 4))" should "be 4" in { val t1234 = Brench(Brench(Leef(1), Leef(2)), Brench(Leef(3), Leef(4))) - Tri.maximum(t1234) should be (4) + Tri.maximum(t1234) should be(4) } } @@ -62,19 +62,19 @@ class Exercise3_27 extends AnyFlatSpec with Matchers { "Depth of (((1, (2, 3)), (4, 5)))" should "be 3" in { val t12345 = Brench(Brench(Leef(1), Brench(Leef(2), Leef(3))), Brench(Leef(4), Leef(5))) - Tri.depth(t12345) should be (3) - Tri.depth1(t12345) should be (3) + Tri.depth(t12345) should be(3) + Tri.depth1(t12345) should be(3) } "Depth of a leef" should "be 0" in { - Tri.depth(Leef(1)) should be (0) - Tri.depth1(Leef(1)) should be (0) + Tri.depth(Leef(1)) should be(0) + Tri.depth1(Leef(1)) should be(0) } "Depth of a brench with just leefs" should "be 1" in { val tb = Brench(Leef(1), Leef(1)) - Tri.depth(tb) should be (1) - Tri.depth1(tb) should be (1) + Tri.depth(tb) should be(1) + Tri.depth1(tb) should be(1) } } @@ -82,31 +82,31 @@ class Exercise3_28 extends AnyFlatSpec with Matchers { "Size of a tri after `map`" should "be the same" in { val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) - Tri.size(Tri.map(t123)(l => l * l)) should be (Tri.size(t123)) + Tri.size(Tri.map(t123)(l => l * l)) should be(Tri.size(t123)) } "Depth of a tri after map" should "be the same" in { val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) - Tri.depth(Tri.map(t123)(l => l * l)) should be (Tri.depth(t123)) + Tri.depth(Tri.map(t123)(l => l * l)) should be(Tri.depth(t123)) } "Maximum of a tri after squaring" should "be the square of maximum of the tri before squaring" in { val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) val maxt123 = Tri.maximum(t123) - Tri.maximum(Tri.map(t123)(l => l * l)) should be (maxt123 * maxt123) + Tri.maximum(Tri.map(t123)(l => l * l)) should be(maxt123 * maxt123) } "Squaring (1)" should "be (1)" in { - Tri.map(Leef(1))(l => l * l) should be (Leef(1)) + Tri.map(Leef(1))(l => l * l) should be(Leef(1)) } "Squaring ((1, 2), 3)" should "be ((1, 4), (9))" in { val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) val t149 = Brench(Brench(Leef(1), Leef(4)), Leef(9)) - Tri.map(t123)(l => l * l) should be (t149) + Tri.map(t123)(l => l * l) should be(t149) } } @@ -118,10 +118,10 @@ class Exercise3_29 extends AnyFlatSpec with Matchers { val tl1234 = Brench(Brench(Brench(Leef(1), Leef(2)), Leef(3)), Leef(4)) val tr1234 = Brench(Brench(Leef(1), Leef(2)), Brench(Leef(3), Leef(4))) - Tri.maximum(tr1234) should be (Tri.maximumViaFold(tr1234)) - Tri.maximum(tl1234) should be (Tri.maximumViaFold(tl1234)) - Tri.maximum(t123) should be (Tri.maximumViaFold(t123)) - Tri.maximum(leef) should be (Tri.maximumViaFold(leef)) + Tri.maximum(tr1234) should be(Tri.maximumViaFold(tr1234)) + Tri.maximum(tl1234) should be(Tri.maximumViaFold(tl1234)) + Tri.maximum(t123) should be(Tri.maximumViaFold(t123)) + Tri.maximum(leef) should be(Tri.maximumViaFold(leef)) } "Finding tri size via folding" should "yield the same as `size`" in { @@ -130,10 +130,10 @@ class Exercise3_29 extends AnyFlatSpec with Matchers { val tr4 = Brench(Leef(1), Brench(Leef(1), Brench(Leef(1), Leef(1)))) val tl4 = Brench(Brench(Leef(1), Brench(Leef(1), Leef(1))), Leef(1)) - Tri.size(leef) should be (Tri.sizeViaFold(leef)) - Tri.size(t2) should be (Tri.sizeViaFold(t2)) - Tri.size(tl4) should be (Tri.sizeViaFold(tl4)) - Tri.size(tr4) should be (Tri.sizeViaFold(tr4)) + Tri.size(leef) should be(Tri.sizeViaFold(leef)) + Tri.size(t2) should be(Tri.sizeViaFold(t2)) + Tri.size(tl4) should be(Tri.sizeViaFold(tl4)) + Tri.size(tr4) should be(Tri.sizeViaFold(tr4)) } "Finding tri depth via folding" should "yield the same as `depth`" in { @@ -141,21 +141,21 @@ class Exercise3_29 extends AnyFlatSpec with Matchers { val tb = Brench(Leef(1), Leef(1)) val leef = Leef(1) - Tri.depth(t12345) should be (Tri.depthViaFold(t12345)) - Tri.depth1(t12345) should be (Tri.depthViaFold(t12345)) - Tri.depth(leef) should be (Tri.depthViaFold(leef)) - Tri.depth1(leef) should be (Tri.depthViaFold(leef)) - Tri.depth(tb) should be (Tri.depthViaFold(tb)) - Tri.depth1(tb) should be (Tri.depthViaFold(tb)) + Tri.depth(t12345) should be(Tri.depthViaFold(t12345)) + Tri.depth1(t12345) should be(Tri.depthViaFold(t12345)) + Tri.depth(leef) should be(Tri.depthViaFold(leef)) + Tri.depth1(leef) should be(Tri.depthViaFold(leef)) + Tri.depth(tb) should be(Tri.depthViaFold(tb)) + Tri.depth1(tb) should be(Tri.depthViaFold(tb)) } "Squaring a tri via folding" should "yield the same as via `map`" in { val leef = Leef(1) val t123 = Brench(Brench(Leef(1), Leef(2)), Leef(3)) - + def square(a: Int): Int = a * a - Tri.map(leef)(l => l * l) should be (Tri.mapViaFold(leef)(square)) - Tri.map(t123)(l => l * l) should be (Tri.mapViaFold(t123)(square)) + Tri.map(leef)(l => l * l) should be(Tri.mapViaFold(leef)(square)) + Tri.map(t123)(l => l * l) should be(Tri.mapViaFold(t123)(square)) } } diff --git a/src/test/scala/purely_functional_state/CandyMachine.scala b/src/test/scala/purely_functional_state/CandyMachine.scala index b096dff..9e41e67 100644 --- a/src/test/scala/purely_functional_state/CandyMachine.scala +++ b/src/test/scala/purely_functional_state/CandyMachine.scala @@ -17,31 +17,33 @@ class Exercise6_11 extends AnyFlatSpec with Matchers { val twoCandies = CandyMachine(true, 2, 0) "Inserting a coin into a locked machine" should "cause it to unlock if there's any candy left" in { - CandyMachine.simulateMachine(Lizt(Coin)).run(fullLockedMachine)._2 should be (CandyMachine(false, 10, 11)) + CandyMachine.simulateMachine(Lizt(Coin)).run(fullLockedMachine)._2 should be(CandyMachine(false, 10, 11)) } "Turning the knob on an unlocked machine" should "cause it to dispense candy and become locked" in { - CandyMachine.simulateMachine(Lizt(Turn)).run(fullUnlockedMachine)._2 should be (CandyMachine(true, 9, 10)) + CandyMachine.simulateMachine(Lizt(Turn)).run(fullUnlockedMachine)._2 should be(CandyMachine(true, 9, 10)) } "Turning the knob on a locked machine" should "do nothing" in { - CandyMachine.simulateMachine(Lizt(Turn)).run(emptyLockedMachine)._2 should be (emptyLockedMachine) - CandyMachine.simulateMachine(Lizt(Turn)).run(fullLockedMachine)._2 should be (fullLockedMachine) + CandyMachine.simulateMachine(Lizt(Turn)).run(emptyLockedMachine)._2 should be(emptyLockedMachine) + CandyMachine.simulateMachine(Lizt(Turn)).run(fullLockedMachine)._2 should be(fullLockedMachine) } "Inserting a coin into an unlocked machine" should "do nothing" in { - CandyMachine.simulateMachine(Lizt(Coin)).run(emptyUnlockedMachine)._2 should be (emptyUnlockedMachine) - CandyMachine.simulateMachine(Lizt(Coin)).run(fullUnlockedMachine)._2 should be (fullUnlockedMachine) + CandyMachine.simulateMachine(Lizt(Coin)).run(emptyUnlockedMachine)._2 should be(emptyUnlockedMachine) + CandyMachine.simulateMachine(Lizt(Coin)).run(fullUnlockedMachine)._2 should be(fullUnlockedMachine) } "A machine that’s out of candy" should "ignore all inputs" in { - CandyMachine.simulateMachine(Lizt(Coin)).run(emptyLockedMachine)._2 should be (emptyLockedMachine) - CandyMachine.simulateMachine(Lizt(Coin)).run(emptyUnlockedMachine)._2 should be (emptyUnlockedMachine) - CandyMachine.simulateMachine(Lizt(Turn)).run(emptyLockedMachine)._2 should be (emptyLockedMachine) - CandyMachine.simulateMachine(Lizt(Turn)).run(emptyUnlockedMachine)._2 should be (emptyUnlockedMachine) + CandyMachine.simulateMachine(Lizt(Coin)).run(emptyLockedMachine)._2 should be(emptyLockedMachine) + CandyMachine.simulateMachine(Lizt(Coin)).run(emptyUnlockedMachine)._2 should be(emptyUnlockedMachine) + CandyMachine.simulateMachine(Lizt(Turn)).run(emptyLockedMachine)._2 should be(emptyLockedMachine) + CandyMachine.simulateMachine(Lizt(Turn)).run(emptyUnlockedMachine)._2 should be(emptyUnlockedMachine) } "Tossing two coins and taking two candies" should "exhaust the `twoCandies` machine" in { - CandyMachine.simulateMachine(Lizt(Coin, Turn, Coin, Turn)).run(twoCandies)._2 should be (CandyMachine(true, 0, 2)) + CandyMachine.simulateMachine(Lizt(Coin, Turn, Coin, Turn)).run(twoCandies)._2 should be( + CandyMachine(true, 0, 2) + ) } -} \ No newline at end of file +} diff --git a/src/test/scala/purely_functional_state/SimpleRNG.scala b/src/test/scala/purely_functional_state/SimpleRNG.scala index a16be3c..c7e91bb 100644 --- a/src/test/scala/purely_functional_state/SimpleRNG.scala +++ b/src/test/scala/purely_functional_state/SimpleRNG.scala @@ -15,7 +15,7 @@ class Exercise6_1 extends AnyFlatSpec with Matchers { "First `nonNegativeInteger` of `SimpleRNG` with seed 42" should "be 16159453" in { val (i, rng) = RNG.nonNegativeInt(sRNG) - i should be (16159453) + i should be(16159453) } } @@ -25,7 +25,7 @@ class Exercise6_2 extends AnyFlatSpec with Matchers { "First `double` of `SimpleRNG` with 42 as seed" should "be 0.16159453" in { val (i, rng) = RNG.double(sRNG) - i should be (0.16159453) + i should be(0.16159453) } } @@ -33,15 +33,15 @@ class Exercise6_3 extends AnyFlatSpec with Matchers { val sRNG = SimpleRNG(42) "First `intDouble` of `SimpleRNG` with 42 as seed" should "be (16159453, 0.1281479696)" in { - RNG.intDouble(sRNG) should be (16159453, 0.1281479696) + RNG.intDouble(sRNG) should be(16159453, 0.1281479696) } - + "First `doubleInt` of `SimpleRNG` with 42 as seed" should "be (0.16159453,-1281479697)" in { - RNG.doubleInt(sRNG) should be (0.16159453,-1281479697) + RNG.doubleInt(sRNG) should be(0.16159453, -1281479697) } "First `double3` of `SimpleRNG` with 42 as seed" should "be (0.16159453, 0.1281479696, 0.340305901)" in { - RNG.double3(sRNG) should be (0.16159453, 0.1281479696, 0.340305901) + RNG.double3(sRNG) should be(0.16159453, 0.1281479696, 0.340305901) } } @@ -49,32 +49,27 @@ class Exercise6_4 extends AnyFlatSpec with Matchers { val sRNG = SimpleRNG(42) "Asking for 0 `ints` from `SimpleRNG` with 42 as seed" should "return the Nill and the original RNG" in { - RNG.ints(0)(sRNG) should be ((Nill, sRNG)) + RNG.ints(0)(sRNG) should be((Nill, sRNG)) } "Asking for 2 `ints` from `SimpleRNG` with 42 as seed" should "return the Lizt(-1281479697, 16159453) and the second RNG" in { - val r2 = sRNG - .nextInt._2 - .nextInt._2 + val r2 = sRNG.nextInt._2.nextInt._2 - RNG.ints(2)(sRNG) should be ((Lizt(-1281479697, 16159453), r2)) + RNG.ints(2)(sRNG) should be((Lizt(-1281479697, 16159453), r2)) } "Asking for 3 `ints` from `SimpleRNG` with 42 as seed" should "return the Lizt(-340305902, -1281479697, 16159453) and the third RNG" in { - val r3 = sRNG - .nextInt._2 - .nextInt._2 - .nextInt._2 + val r3 = sRNG.nextInt._2.nextInt._2.nextInt._2 - RNG.ints(3)(sRNG) should be ((Lizt(-340305902, -1281479697, 16159453), r3)) + RNG.ints(3)(sRNG) should be((Lizt(-340305902, -1281479697, 16159453), r3)) } } class Exercise6_5 extends AnyFlatSpec with Matchers { val sRNG = SimpleRNG(42) - + "`doubleViaMap`" should "behave as `double`" in { - RNG.doubleViaMap(sRNG) should be (RNG.double(sRNG)) + RNG.doubleViaMap(sRNG) should be(RNG.double(sRNG)) } } @@ -82,7 +77,9 @@ class Exercise6_6 extends AnyFlatSpec with Matchers { val sRNG = SimpleRNG(42) "`map2`-ing over two ints with string concatenation via`SimpleRNG` with 42 as seed" should "return 161594531281479696" in { - RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1 should be ("161594531281479696") + RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1 should be( + "161594531281479696" + ) } } @@ -92,13 +89,15 @@ class Exercise6_7 extends AnyFlatSpec with Matchers { "`sequence`-ing over three random int generators" should "yield the list of the three random numbers" in { val (_, r3) = RNG.ints(3)(sRNG) - RNG.sequence(Lizt(RNG.int, RNG.int, RNG.int))(sRNG) should be ((Lizt(16159453, -1281479697, -340305902), r3)) + RNG.sequence(Lizt(RNG.int, RNG.int, RNG.int))(sRNG) should be( + (Lizt(16159453, -1281479697, -340305902), r3) + ) } "`sequence`-ing over an empty Lizt" should "yield an empty Lizt" in { val (_, r3) = RNG.ints(3)(sRNG) - RNG.sequence(Nill)(sRNG) should be (Nill, sRNG) + RNG.sequence(Nill)(sRNG) should be(Nill, sRNG) } } @@ -110,7 +109,7 @@ class Exercise6_8 extends AnyFlatSpec with Matchers { "`flatMap`-ing over an Int with stringification" should "yield the String value of the Int" in { val (_, r) = sRNG.nextInt - RNG.flatMap(RNG.int)(stringify)(sRNG) should be (("16159453", r)) + RNG.flatMap(RNG.int)(stringify)(sRNG) should be(("16159453", r)) } } @@ -120,11 +119,13 @@ class Exercise6_9 extends AnyFlatSpec with Matchers { def stringify[A](a: A): Rand[String] = ((a.toString), _) "`mapViaFlatMap`-ing with stringification" should "behave as `map`" in { - RNG.mapViaFlatMap(RNG.int)(_.toString())(sRNG) should be (RNG.map(RNG.int)(_.toString())(sRNG)) + RNG.mapViaFlatMap(RNG.int)(_.toString())(sRNG) should be(RNG.map(RNG.int)(_.toString())(sRNG)) } "`map2ViaFlatMap`-ing with stringification" should "behave as `map2`" in { - RNG.map2ViaFlatMap(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1 should be (RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1) + RNG.map2ViaFlatMap(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1 should be( + RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)((a, b) => s"$a$b")(sRNG)._1 + ) } } @@ -132,18 +133,18 @@ class Misc extends AnyFlatSpec with Matchers { val sRNG = SimpleRNG(42) "`both` over two ints with via`SimpleRNG` with 42 as seed" should "return (16159453, 1281479696)" in { - RNG.both(RNG.nonNegativeInt, RNG.nonNegativeInt)(sRNG)._1 should be ((16159453, 1281479696)) + RNG.both(RNG.nonNegativeInt, RNG.nonNegativeInt)(sRNG)._1 should be((16159453, 1281479696)) } "`intDoubleViaMap`" should "behave as `intDouble`" in { - RNG.intDoubleViaMap2(sRNG)._1 should be (RNG.intDouble(sRNG)) + RNG.intDoubleViaMap2(sRNG)._1 should be(RNG.intDouble(sRNG)) } "`doubleIntViaMap`" should "behave as `doubleInt`" in { - RNG.doubleIntViaMap2(sRNG)._1 should be (RNG.doubleInt(sRNG)) + RNG.doubleIntViaMap2(sRNG)._1 should be(RNG.doubleInt(sRNG)) } "`nonNegativeLessThen`" should "behave as `nonNegativeLessThenViaFlatMap`" in { - RNG.nonNegativeLessThanViaFlatMap(2)(sRNG) should be (RNG.nonNegativeLessThan(2)(sRNG)) + RNG.nonNegativeLessThanViaFlatMap(2)(sRNG) should be(RNG.nonNegativeLessThan(2)(sRNG)) } -} \ No newline at end of file +} diff --git a/src/test/scala/purely_functional_state/Steyt.scala b/src/test/scala/purely_functional_state/Steyt.scala index d0a6d6b..69917cd 100644 --- a/src/test/scala/purely_functional_state/Steyt.scala +++ b/src/test/scala/purely_functional_state/Steyt.scala @@ -20,22 +20,26 @@ class Exercise6_10 extends AnyFlatSpec with Matchers { def strConcat[A, B](a: A, b: B): String = s"$a$b" "`map` on Steyt with RNG" should "behave as `map` on RNG" in { - Steyt(RNG.nonNegativeInt).map(prependZero).run(sRNG) should be (RNG.doubleViaMap(sRNG)) + Steyt(RNG.nonNegativeInt).map(prependZero).run(sRNG) should be(RNG.doubleViaMap(sRNG)) } "`map2` on Steyt with RNG" should "behave as `map2` on RNG" in { - Steyt(RNG.nonNegativeInt).map2(Steyt(RNG.nonNegativeInt))(strConcat).run(sRNG) should be (RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)(strConcat)(sRNG)) + Steyt(RNG.nonNegativeInt).map2(Steyt(RNG.nonNegativeInt))(strConcat).run(sRNG) should be( + RNG.map2(RNG.nonNegativeInt, RNG.nonNegativeInt)(strConcat)(sRNG) + ) } "`flatMap` on Steyt with RNG" should "behave as `flatMap` on RNG" in { - Steyt(RNG.int).flatMap(stringifySteyt).run(sRNG) should be (RNG.flatMap(RNG.int)(stringify)(sRNG)) + Steyt(RNG.int).flatMap(stringifySteyt).run(sRNG) should be(RNG.flatMap(RNG.int)(stringify)(sRNG)) } "`sequence` on Steyt over three random int generators" should "behave as `sequence` on RNG" in { - Steyt.sequence(Lizt(Steyt(RNG.int), Steyt(RNG.int), Steyt(RNG.int))).run(sRNG) should be (RNG.sequence(Lizt(RNG.int, RNG.int, RNG.int))(sRNG)) + Steyt.sequence(Lizt(Steyt(RNG.int), Steyt(RNG.int), Steyt(RNG.int))).run(sRNG) should be( + RNG.sequence(Lizt(RNG.int, RNG.int, RNG.int))(sRNG) + ) } "`sequence` on Steyt over an empty Lizt" should "behave as `sequence` on RNG" in { - Steyt.sequence(Nill).run(sRNG) should be (RNG.sequence(Nill)(sRNG)) + Steyt.sequence(Nill).run(sRNG) should be(RNG.sequence(Nill)(sRNG)) } -} \ No newline at end of file +} diff --git a/src/test/scala/strictness_and_laziness/Strim.scala b/src/test/scala/strictness_and_laziness/Strim.scala index 8dd73aa..41802ed 100644 --- a/src/test/scala/strictness_and_laziness/Strim.scala +++ b/src/test/scala/strictness_and_laziness/Strim.scala @@ -14,13 +14,13 @@ import strim._ class Exercise5_1 extends AnyFlatSpec with Matchers { "Converting an Emptie to Lizt" should "return Nill" in { - Emptie.toLizt should be (Nill) - Emptie.toLiztRec should be (Nill) + Emptie.toLizt should be(Nill) + Emptie.toLiztRec should be(Nill) } "Converting an Strim(1, 2, 3) to Lizt" should "return Lizt(1, 2, 3)" in { - Strim(1, 2, 3).toLizt should be (Lizt(1, 2, 3)) - Strim(1, 2, 3).toLiztRec should be (Lizt(1, 2, 3)) + Strim(1, 2, 3).toLizt should be(Lizt(1, 2, 3)) + Strim(1, 2, 3).toLiztRec should be(Lizt(1, 2, 3)) } } @@ -28,35 +28,35 @@ class Exercise5_2 extends AnyFlatSpec with Matchers { val s12345 = Strim(1, 2, 3, 4, 5) "`take`-ing from an Emptie" should "be Emptie, regardless of the argument" in { - Emptie.take(Random.nextInt()) should be (Emptie) + Emptie.take(Random.nextInt()) should be(Emptie) } "`take`-ing 4 over Strim(1, 2, 3, 4, 5)" should "be the Strim(1, 2, 3, 4)" in { - s12345.take(4).toLizt should be (Strim(1, 2, 3, 4).toLizt) + s12345.take(4).toLizt should be(Strim(1, 2, 3, 4).toLizt) } "`take`-ing all from a Strim" should "be the original Strim" in { - s12345.take(5).toLizt should be (s12345.toLizt) + s12345.take(5).toLizt should be(s12345.toLizt) } "`take`-ing more than the original Strim" should "be the original Strim" in { - s12345.take(10).toLizt should be (s12345.toLizt) + s12345.take(10).toLizt should be(s12345.toLizt) } "`drop`-ing from an Emptie" should "be Emptie, regardless of the argument" in { - Emptie.drop(Random.nextInt()) should be (Emptie) + Emptie.drop(Random.nextInt()) should be(Emptie) } "`drop`-ing 4 over Strim(1, 2, 3, 4, 5)" should "be the empty Strim(5)" in { - s12345.drop(4).toLizt should be (Strim(5).toLizt) + s12345.drop(4).toLizt should be(Strim(5).toLizt) } "`drop`-ing all from a Strim" should "be Emptie" in { - s12345.drop(5) should be (Emptie) + s12345.drop(5) should be(Emptie) } "`drop`-ing more than the original Strim" should "be the Emptie" in { - s12345.drop(44) should be (Emptie) + s12345.drop(44) should be(Emptie) } } @@ -65,28 +65,28 @@ class Exercise5_3 extends AnyFlatSpec with Matchers { val sABCD = Strim('A', 'B', 'C', 'D') "`takeWhile` over any Strim with a FALSE predicate" should "allways be Emptie" in { - s1234.takeWhile(_ => false) should be (Emptie) - sABCD.takeWhile(_ => false) should be (Emptie) + s1234.takeWhile(_ => false) should be(Emptie) + sABCD.takeWhile(_ => false) should be(Emptie) } "`takeWhile` over any Strim with a TRUE predicate" should "allways be the original Strim" in { - s1234.takeWhile(_ => true).toLizt should be (Lizt(1, 2, 3, 4)) - sABCD.takeWhile(_ => true).toLizt should be (Lizt('A', 'B', 'C', 'D')) + s1234.takeWhile(_ => true).toLizt should be(Lizt(1, 2, 3, 4)) + sABCD.takeWhile(_ => true).toLizt should be(Lizt('A', 'B', 'C', 'D')) } "`takeWhile` over Strim(1, 2, 3, 4) with an odd predicate" should "be Strim(1)" in { val isOdd = (n: Int) => (n % 2) != 0 - s1234.takeWhile(isOdd).toLizt should be ((Lizt(1))) + s1234.takeWhile(isOdd).toLizt should be((Lizt(1))) } "`takeWhile` over Strim(1, 2, 3, 4) with an even predicate" should "be Emptie" in { val isEven = (n: Int) => (n % 2) == 0 - s1234.takeWhile(isEven).toLizt should be (Nill) + s1234.takeWhile(isEven).toLizt should be(Nill) } "`takeWhile` over Emptie with ANY predicate" should "be Emptie" in { - Emptie.takeWhile(_ => true) should be (Emptie) - Emptie.takeWhile(_ => false) should be (Emptie) + Emptie.takeWhile(_ => true) should be(Emptie) + Emptie.takeWhile(_ => false) should be(Emptie) } } @@ -94,16 +94,16 @@ class Exercise5_4 extends AnyFlatSpec with Matchers { val isOdd = (n: Int) => (n % 2) != 0 "`forAll` with any predicate over an Emptie" should "be FALSE" in { - Emptie.forAll(_ => true) should be (true) - Emptie.forAll(_ => false) should be (true) + Emptie.forAll(_ => true) should be(true) + Emptie.forAll(_ => false) should be(true) } "`forAll` with and odd predicate over Strim(1, 3, 5)" should "TRUE" in { - Strim(1, 3, 5).forAll(isOdd) should be (true) + Strim(1, 3, 5).forAll(isOdd) should be(true) } "`forAll` with and even predicate over Strim(2, 4, 6)" should "FALSE" in { - Strim(2, 4, 6).forAll(isOdd) should be (false) + Strim(2, 4, 6).forAll(isOdd) should be(false) } } @@ -112,91 +112,91 @@ class Exercise5_5 extends AnyFlatSpec with Matchers { val sABCD = Strim('A', 'B', 'C', 'D') "`takeWhileViaFoldRight` over any Strim with a FALSE predicate" should "allways be Emptie" in { - s1234.takeWhileViaFoldRight(_ => false) should be (Emptie) - sABCD.takeWhileViaFoldRight(_ => false) should be (Emptie) + s1234.takeWhileViaFoldRight(_ => false) should be(Emptie) + sABCD.takeWhileViaFoldRight(_ => false) should be(Emptie) } "`takeWhileViaFoldRight` over any Strim with a TRUE predicate" should "allways be the original Strim" in { - s1234.takeWhileViaFoldRight(_ => true).toLizt should be (Lizt(1, 2, 3, 4)) - sABCD.takeWhileViaFoldRight(_ => true).toLizt should be (Lizt('A', 'B', 'C', 'D')) + s1234.takeWhileViaFoldRight(_ => true).toLizt should be(Lizt(1, 2, 3, 4)) + sABCD.takeWhileViaFoldRight(_ => true).toLizt should be(Lizt('A', 'B', 'C', 'D')) } "`takeWhileViaFoldRight` over Strim(1, 2, 3, 4) with an odd predicate" should "be Strim(1, 3)" in { val isOdd = (n: Int) => (n % 2) != 0 - s1234.takeWhileViaFoldRight(isOdd).toLizt should be ((Lizt(1, 3))) + s1234.takeWhileViaFoldRight(isOdd).toLizt should be((Lizt(1, 3))) } "`takeWhileViaFoldRight` over Strim(1, 2, 3, 4) with an even predicate" should "be Strim(2, 4)" in { val isEven = (n: Int) => (n % 2) == 0 - s1234.takeWhileViaFoldRight(isEven).toLizt should be ((Lizt(2, 4))) + s1234.takeWhileViaFoldRight(isEven).toLizt should be((Lizt(2, 4))) } "`takeWhileViaFoldRight` over Emptie with ANY predicate" should "be Emptie" in { - Emptie.takeWhileViaFoldRight(_ => true) should be (Emptie) - Emptie.takeWhileViaFoldRight(_ => false) should be (Emptie) + Emptie.takeWhileViaFoldRight(_ => true) should be(Emptie) + Emptie.takeWhileViaFoldRight(_ => false) should be(Emptie) } } class Exercise5_6 extends AnyFlatSpec with Matchers { "`headOpshnViaFoldRight` over Emptie" should "be Non" in { - Emptie.headOpshnViaFoldRight should be (Non) + Emptie.headOpshnViaFoldRight should be(Non) } "`headOpshnViaFoldRight` over Strim(1, 2)" should "be Sam(1)" in { - Strim(1, 2).headOpshnViaFoldRight should be (Sam(1)) + Strim(1, 2).headOpshnViaFoldRight should be(Sam(1)) } "`headOpshnViaFoldRight` over Strim(1)" should "be Sam(1)" in { - Strim(1).headOpshnViaFoldRight should be (Sam(1)) + Strim(1).headOpshnViaFoldRight should be(Sam(1)) } } class Exercise5_7 extends AnyFlatSpec with Matchers { "`map` over Emptie" should "be Emptie" in { - Emptie.map(_ => 1) should be (Emptie) + Emptie.map(_ => 1) should be(Emptie) } "`map` over Strim(4, 9, 16) with square rooting" should "be Strim(2, 3, 4)" in { - Strim(4, 9, 16).map(math.sqrt(_)).toLizt should be (Strim(2, 3, 4).toLizt) + Strim(4, 9, 16).map(math.sqrt(_)).toLizt should be(Strim(2, 3, 4).toLizt) } "`flatMap` over Emptie" should "be Emptie" in { - Emptie.flatMap(a => Strim(a)) should be (Emptie) + Emptie.flatMap(a => Strim(a)) should be(Emptie) } "`flatMap` over Strim(4, 9, 16) with square rooting" should "be Strim(2, 3, 4)" in { - Strim(4, 9, 16).flatMap(a => Strim(math.sqrt(a))).toLizt should be (Strim(2, 3, 4).toLizt) + Strim(4, 9, 16).flatMap(a => Strim(math.sqrt(a))).toLizt should be(Strim(2, 3, 4).toLizt) } "`filtering` Emptie with ANY filter" should "result in Emptie" in { - Emptie.filter(_ => false) should be (Emptie) - Emptie.filter(_ => true) should be (Emptie) + Emptie.filter(_ => false) should be(Emptie) + Emptie.filter(_ => true) should be(Emptie) } "`filtering` Strim(1, 2, 3, 4) with an odd filter" should "produce Strim(2, 4)" in { val isOdd = (n: Int) => (n % 2) != 0 - Strim(1, 2, 3, 4).filter(isOdd).toLizt should be (Strim(1, 3).toLizt) + Strim(1, 2, 3, 4).filter(isOdd).toLizt should be(Strim(1, 3).toLizt) } "`filtering` Strim(1, 2, 3, 4) with an even filter" should "produce Strim(1, 3)" in { val isEven = (n: Int) => (n % 2) == 0 - Strim(1, 2, 3, 4).filter(isEven).toLizt should be (Strim(2, 4).toLizt) + Strim(1, 2, 3, 4).filter(isEven).toLizt should be(Strim(2, 4).toLizt) } "appending Emptie to Emptie" should "result in Emptie" in { - Emptie.append(Emptie) should be (Emptie) + Emptie.append(Emptie) should be(Emptie) } "appending Emptie to Strim" should "result in the original Strim" in { - Strim(1, 2, 3).append(Emptie).toLizt should be (Strim(1, 2, 3).toLizt) + Strim(1, 2, 3).append(Emptie).toLizt should be(Strim(1, 2, 3).toLizt) } "appending a Strim to Emptie" should "result in appended Strim" in { - Emptie.append(Strim(1, 2, 3)).toLizt should be (Strim(1, 2, 3).toLizt) + Emptie.append(Strim(1, 2, 3)).toLizt should be(Strim(1, 2, 3).toLizt) } "appending Strim(1, 2) to Strim(3, 4)" should "result in Strim(1, 2, 3, 4)" in { - Strim(1, 2).append(Strim(3, 4)).toLiztRec should be (Strim(1, 2, 3, 4).toLizt) + Strim(1, 2).append(Strim(3, 4)).toLiztRec should be(Strim(1, 2, 3, 4).toLizt) } } @@ -208,41 +208,41 @@ class Exercise5_8 extends AnyFlatSpec with Matchers { val v = 1 val l = List.fill(a)(1) - Strim.constant(v).take(a).toLizt should be (Lizt.fill(a)(v)) - Strim.constant(v).take(b).toLizt should be (Lizt.fill(b)(v)) - Strim.constant(v).take(c).toLizt should be (Lizt.fill(c)(v)) + Strim.constant(v).take(a).toLizt should be(Lizt.fill(a)(v)) + Strim.constant(v).take(b).toLizt should be(Lizt.fill(b)(v)) + Strim.constant(v).take(c).toLizt should be(Lizt.fill(c)(v)) } } class Exercise5_9 extends AnyFlatSpec with Matchers { "seventh element `from` 1" should "be 7" in { - Strim.from(1).drop(6).take(1).headOpshn should be (Sam(7)) + Strim.from(1).drop(6).take(1).headOpshn should be(Sam(7)) } "first element `from` 1" should "be 1" in { - Strim.from(1).take(1).headOpshn should be (Sam(1)) + Strim.from(1).take(1).headOpshn should be(Sam(1)) } } class Exercise5_10 extends AnyFlatSpec with Matchers { "A Fibonacci Strim" should "return 0 for 0th take" in { - Strim.fibs.take(1).headOpshn should be (Sam(0)); + Strim.fibs.take(1).headOpshn should be(Sam(0)); } it should "return 1 for 2nd take" in { - Strim.fibs.drop(1).take(1).headOpshn should be (Sam(1)); + Strim.fibs.drop(1).take(1).headOpshn should be(Sam(1)); } it should "return 1 for 3rd take" in { - Strim.fibs.drop(2).take(1).headOpshn should be (Sam(1)); + Strim.fibs.drop(2).take(1).headOpshn should be(Sam(1)); } it should "return 2 for 4th take" in { - Strim.fibs.drop(3).take(1).headOpshn should be (Sam(2)); + Strim.fibs.drop(3).take(1).headOpshn should be(Sam(2)); } it should "return 34 for 10th take" in { - Strim.fibs.drop(9).take(1).headOpshn should be (Sam(34)) + Strim.fibs.drop(9).take(1).headOpshn should be(Sam(34)) } } @@ -255,15 +255,15 @@ class Exercise5_11 extends AnyFlatSpec with Matchers { } "`unfold`-ing Emptie via anything" should "be Emptie" in { - Strim.unfold(0)(_ => Non) should be (Emptie) - Strim.unfold(true)(_ => Non) should be (Emptie) - Strim.unfold("")(_ => Non) should be (Emptie) + Strim.unfold(0)(_ => Non) should be(Emptie) + Strim.unfold(true)(_ => Non) should be(Emptie) + Strim.unfold("")(_ => Non) should be(Emptie) } "`unfold`-ing anything with Non" should "return Emptie" in { - Strim.unfold(0)(a => Non) should be (Emptie) - Strim.unfold("0")(a => Non) should be (Emptie) - Strim.unfold(false)(a => Non) should be (Emptie) + Strim.unfold(0)(a => Non) should be(Emptie) + Strim.unfold("0")(a => Non) should be(Emptie) + Strim.unfold(false)(a => Non) should be(Emptie) } "`unfold`-ing a string via ASCII-fier" should "retun the Lizt of ASCII codes fo the string" in { @@ -272,7 +272,7 @@ class Exercise5_11 extends AnyFlatSpec with Matchers { val s = Strim.unfold(source) { ASCIIfy } - s.take(8).toLizt should be (ASCIIfiedRafique) + s.take(8).toLizt should be(ASCIIfiedRafique) } "9th element of `unfold`-ing a 'Raphique' via ASCII-fier" should "retun Nill" in { @@ -280,7 +280,7 @@ class Exercise5_11 extends AnyFlatSpec with Matchers { val s = Strim.unfold(source) { ASCIIfy } - s.drop(8).take(1).toLizt should be (Nill) + s.drop(8).take(1).toLizt should be(Nill) } } @@ -288,16 +288,16 @@ class Exercise5_12 extends AnyFlatSpec with Matchers { import Strim._ "`fibsViaUnfold`" should "behave as `fibs`" in { - Strim.fibs.take(1).headOpshn should be (Strim.fibsViaUnfold.take(1).headOpshn) - Strim.fibs.drop(2).take(1).headOpshn should be (Strim.fibsViaUnfold.drop(2).take(1).headOpshn) - Strim.fibs.drop(3).take(1).headOpshn should be (Strim.fibsViaUnfold.drop(3).take(1).headOpshn) - Strim.fibs.drop(4).take(1).headOpshn should be (Strim.fibsViaUnfold.drop(4).take(1).headOpshn) - Strim.fibs.drop(10).take(1).headOpshn should be (Strim.fibsViaUnfold.drop(10).take(1).headOpshn) + Strim.fibs.take(1).headOpshn should be(Strim.fibsViaUnfold.take(1).headOpshn) + Strim.fibs.drop(2).take(1).headOpshn should be(Strim.fibsViaUnfold.drop(2).take(1).headOpshn) + Strim.fibs.drop(3).take(1).headOpshn should be(Strim.fibsViaUnfold.drop(3).take(1).headOpshn) + Strim.fibs.drop(4).take(1).headOpshn should be(Strim.fibsViaUnfold.drop(4).take(1).headOpshn) + Strim.fibs.drop(10).take(1).headOpshn should be(Strim.fibsViaUnfold.drop(10).take(1).headOpshn) } "`fromViaUnfold`" should "behave as `from`" in { - Strim.fromViaUnfold(1).take(1).headOpshn should be (Strim.from(1).take(1).headOpshn) - Strim.fromViaUnfold(1).drop(6).take(1).headOpshn should be (Strim.from(1).drop(6).take(1).headOpshn) + Strim.fromViaUnfold(1).take(1).headOpshn should be(Strim.from(1).take(1).headOpshn) + Strim.fromViaUnfold(1).drop(6).take(1).headOpshn should be(Strim.from(1).drop(6).take(1).headOpshn) } "`constantViaUnfold`" should "behave as `constant`" in { @@ -307,16 +307,16 @@ class Exercise5_12 extends AnyFlatSpec with Matchers { val v = 1 val l = List.fill(a)(1) - Strim.constant(v).take(a).toLizt should be (Strim.constantViaUnfold(v).take(a).toLizt) - Strim.constant(v).take(b).toLizt should be (Strim.constantViaUnfold(v).take(b).toLizt) - Strim.constant(v).take(c).toLizt should be (Strim.constantViaUnfold(v).take(c).toLizt) + Strim.constant(v).take(a).toLizt should be(Strim.constantViaUnfold(v).take(a).toLizt) + Strim.constant(v).take(b).toLizt should be(Strim.constantViaUnfold(v).take(b).toLizt) + Strim.constant(v).take(c).toLizt should be(Strim.constantViaUnfold(v).take(c).toLizt) } "`onesViaUnfold`" should "behave as `ones`" in { val idx = Random.nextInt(100) - - Strim.onesViaUnfold.drop(idx).take(1).headOpshn should be (Sam(1)) - Strim.onesViaUnfold.drop(idx).take(1).toLizt should be (Strim.ones.drop(idx).take(1).toLizt) + + Strim.onesViaUnfold.drop(idx).take(1).headOpshn should be(Sam(1)) + Strim.onesViaUnfold.drop(idx).take(1).toLizt should be(Strim.ones.drop(idx).take(1).toLizt) } } @@ -328,60 +328,60 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val sABCD = Strim('A', 'B', 'C', 'D') "`mapViaUnfold` over Emptie" should "behave as `map`" in { - Emptie.mapViaUnfold(_ => 1) should be (Emptie.map(_ => 1)) + Emptie.mapViaUnfold(_ => 1) should be(Emptie.map(_ => 1)) } "`mapViaUnfold` over Strim(4, 9, 16) with square rooting" should "behave as `map`" in { - Strim(4, 9, 16).mapViaUnfold(math.sqrt(_)).toLizt should be (Strim(4, 9, 16).map(math.sqrt(_)).toLizt) + Strim(4, 9, 16).mapViaUnfold(math.sqrt(_)).toLizt should be(Strim(4, 9, 16).map(math.sqrt(_)).toLizt) } "`takeViaUnfold`-ing from an Emptie" should "behave as `take`-ing" in { val idx = Random.nextInt() - Emptie.takeViaUnfold(idx) should be (Emptie.take(idx)) + Emptie.takeViaUnfold(idx) should be(Emptie.take(idx)) } "`takeViaUnfold`-ing 4 over Strim(1, 2, 3, 4, 5)" should "behave as `take`-ing" in { - s12345.takeViaUnfold(4).toLizt should be (s12345.take(4).toLizt) + s12345.takeViaUnfold(4).toLizt should be(s12345.take(4).toLizt) } "`takeViaUnfold`-ing all from a Strim" should "behave as `take`-ing" in { - s12345.takeViaUnfold(5).toLizt should be (s12345.take(5).toLizt) + s12345.takeViaUnfold(5).toLizt should be(s12345.take(5).toLizt) } "`takeViaUnfold`-ing more than the original Strim" should "behave as `take`-ing" in { - s12345.takeViaUnfold(10).toLizt should be (s12345.take(10).toLizt) + s12345.takeViaUnfold(10).toLizt should be(s12345.take(10).toLizt) } "`takeWhileViaUnfold` over any Strim with a FALSE predicate" should "behave as `takeWhile`" in { - s1234.takeWhileViaUnfold(_ => false) should be (s1234.takeWhile(_ => false)) - sABCD.takeWhileViaUnfold(_ => false) should be (sABCD.takeWhile(_ => false)) + s1234.takeWhileViaUnfold(_ => false) should be(s1234.takeWhile(_ => false)) + sABCD.takeWhileViaUnfold(_ => false) should be(sABCD.takeWhile(_ => false)) } "`takeWhileViaUnfold` over any Strim with a TRUE predicate" should "behave as `takeWhile`" in { - s1234.takeWhileViaFoldRight(_ => true).toLizt should be (s1234.takeWhile(_ => true).toLizt) - sABCD.takeWhileViaFoldRight(_ => true).toLizt should be (sABCD.takeWhile(_ => true).toLizt) + s1234.takeWhileViaFoldRight(_ => true).toLizt should be(s1234.takeWhile(_ => true).toLizt) + sABCD.takeWhileViaFoldRight(_ => true).toLizt should be(sABCD.takeWhile(_ => true).toLizt) } "`takeWhileViaUnfold` over Strim(1, 2, 3, 4) with an odd predicate" should "behave as `takeWhile`" in { val isOdd = (n: Int) => (n % 2) != 0 - s1234.takeWhileViaUnfold(isOdd).toLizt should be (s1234.takeWhile(isOdd).toLizt) + s1234.takeWhileViaUnfold(isOdd).toLizt should be(s1234.takeWhile(isOdd).toLizt) } "`takeWhileViaUnfold` over Strim(1, 2, 3, 4) with an even predicate" should "behave as `takeWhile`" in { val isEven = (n: Int) => (n % 2) == 0 - s1234.takeWhileViaUnfold(isEven).toLizt should be (s1234.takeWhile(isEven).toLizt) + s1234.takeWhileViaUnfold(isEven).toLizt should be(s1234.takeWhile(isEven).toLizt) } "`takeWhileViaUnfold` over Emptie with ANY predicate" should "behave as `takeWhile`" in { - Emptie.takeWhileViaUnfold(_ => true) should be (Emptie.takeWhile(_ => true)) - Emptie.takeWhileViaUnfold(_ => false) should be (Emptie.takeWhile(_ => false)) + Emptie.takeWhileViaUnfold(_ => true) should be(Emptie.takeWhile(_ => true)) + Emptie.takeWhileViaUnfold(_ => false) should be(Emptie.takeWhile(_ => false)) } "`zipWith` an Emptie" should "return Emptie" in { val s123 = Strim(1, 2, 3) - s123.zipWith(Emptie)(_ + _) should be (Emptie) - emptie[String].zipWith(s123)(_ + _) should be (Emptie) + s123.zipWith(Emptie)(_ + _) should be(Emptie) + emptie[String].zipWith(s123)(_ + _) should be(Emptie) } "`zipWith` with Strim(1, 2, 3) and Strim(4, 5, 6)" should "be Strim(5, 7, 9) with addition as the zipping function" in { @@ -389,7 +389,7 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val s456 = Strim(4, 5, 6) val l579 = Lizt(5, 7, 9) - s123.zipWith(s456)(_ + _).toLizt should be (l579) + s123.zipWith(s456)(_ + _).toLizt should be(l579) } "`zipWith` with Strim(1, 2, 3) and Strim(4, 5, 6)" should "be Strim(-3, -3, -3) with subtraction as the zipping function" in { @@ -397,18 +397,18 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val s456 = Strim(4, 5, 6) val lm3m3m3 = Lizt(-3, -3, -3) - s123.zipWith(s456)(_ - _).toLizt should be (lm3m3m3) + s123.zipWith(s456)(_ - _).toLizt should be(lm3m3m3) } "`zipWith` with any or both of the Strims being Emptie" should "result in Emptie regardles of the zipping function" in { val s123 = Strim(1, 2, 3) - s123.zipWith(emptie[Int])(_ + _) should be (Emptie) - emptie[Int].zipWith(s123)(_ + _) should be (Emptie) - emptie[Int].zipWith(emptie[Int])(_ + _) should be (Emptie) - s123.zipWith(emptie[Int])(_ - _) should be (Emptie) - emptie[Int].zipWith(s123)(_ - _) should be (Emptie) - emptie[Int].zipWith(emptie[Int])(_ - _) should be (Emptie) + s123.zipWith(emptie[Int])(_ + _) should be(Emptie) + emptie[Int].zipWith(s123)(_ + _) should be(Emptie) + emptie[Int].zipWith(emptie[Int])(_ + _) should be(Emptie) + s123.zipWith(emptie[Int])(_ - _) should be(Emptie) + emptie[Int].zipWith(s123)(_ - _) should be(Emptie) + emptie[Int].zipWith(emptie[Int])(_ - _) should be(Emptie) } "`zipWith` result" should "be the length of the shorter Strim regardles of the zipping function" in { @@ -416,8 +416,8 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val s12345 = Strim(1, 2, 3, 4, 5) val l12 = Lizt(1, 2) - Lizt.length(s12.zipWith(s12345)(_ + _).toLizt) should be (Lizt.length(l12)) - Lizt.length(s12.zipWith(s12345)(_ - _).toLizt) should be (Lizt.length(l12)) + Lizt.length(s12.zipWith(s12345)(_ + _).toLizt) should be(Lizt.length(l12)) + Lizt.length(s12.zipWith(s12345)(_ - _).toLizt) should be(Lizt.length(l12)) } "`zipAll` an Emptie with a Strim" should "return the Strim" in { @@ -425,8 +425,8 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val strimEmptieZip = Lizt((Sam(1), Non), (Sam(2), Non), (Sam(3), Non)) val emptieStrimZip = Lizt((Non, Sam(1)), (Non, Sam(2)), (Non, Sam(3))) - s123.zipAll(Emptie).toLizt should be (strimEmptieZip) - emptie[String].zipAll(s123).toLizt should be (emptieStrimZip) + s123.zipAll(Emptie).toLizt should be(strimEmptieZip) + emptie[String].zipAll(s123).toLizt should be(emptieStrimZip) } "`zipAll` with Strim(1, 2, 3) and Strim(4, 5, 6)" should "be Lizt((Sam(1), Sam(4)), (Sam(2), Sam(5)), (Sam(3), Sam(6)))" in { @@ -434,7 +434,7 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val s456 = Strim(4, 5, 6) val strimStrimZip = Lizt((Sam(1), Sam(4)), (Sam(2), Sam(5)), (Sam(3), Sam(6))) - s123.zipAll(s456).toLizt should be (strimStrimZip) + s123.zipAll(s456).toLizt should be(strimStrimZip) } "`zipAll` with two Strims" should "should stay productive, while any of the strims is productive" in { @@ -443,8 +443,8 @@ class Exercise5_13 extends AnyFlatSpec with Matchers { val s12s123Zip = Lizt((Sam(1), Sam(1)), (Sam(2), Sam(2)), (Non, Sam(3))) val s123s12Zip = Lizt((Sam(1), Sam(1)), (Sam(2), Sam(2)), (Sam(3), Non)) - s12.zipAll(s123).toLizt should be (s12s123Zip) - s123.zipAll(s12).toLizt should be (s123s12Zip) + s12.zipAll(s123).toLizt should be(s12s123Zip) + s123.zipAll(s12).toLizt should be(s123s12Zip) } } @@ -454,105 +454,105 @@ class Exercise5_14 extends AnyFlatSpec with Matchers { "Strim(1, 2, 3)" should "`startWith` Strim (1, 2)" in { val s123 = Strim(1, 2, 3) val s12 = Strim(1, 2) - - s123 startsWith s12 should be (true) + + s123 startsWith s12 should be(true) } - + it should "`startWith` Strim (1, 2, 3)" in { val s123 = Strim(1, 2, 3) - - s123 startsWith s123 should be (true) + + s123 startsWith s123 should be(true) } - + it should "not `startWith` Strim (1, 2, 4)" in { val s123 = Strim(1, 2, 3) val s124 = Strim(1, 2, 4) - - s123 startsWith s124 should be (false) + + s123 startsWith s124 should be(false) } - + "Strim(1, 2, 4)" should "not `startWith` Strim (1, 2, 3, 4)" in { val s1234 = Strim(1, 2, 3, 4) val s124 = Strim(1, 2, 4) - s124 startsWith s1234 should be (false) + s124 startsWith s1234 should be(false) } "Strim(1, 2, 4, 4)" should "not `startWith` Strim (1, 2, 3, 4)" in { val s1234 = Strim(1, 2, 3, 4) val s1244 = Strim(1, 2, 4, 4) - s1244 startsWith s1234 should be (false) + s1244 startsWith s1234 should be(false) } "Emptie" can "not `startsWith` anything" in { val s123 = Strim(1, 2, 3) val s12 = Strim(1, 2) - Emptie startsWith s123 should be (false) - Emptie startsWith s12 should be (false) + Emptie startsWith s123 should be(false) + Emptie startsWith s12 should be(false) } "Nothing" can "`startWith` Emptie" in { val s123 = Strim(1, 2, 3) val s12 = Strim(1, 2) - s12 startsWith Emptie should be (false) - s123 startsWith Emptie should be (false) + s12 startsWith Emptie should be(false) + s123 startsWith Emptie should be(false) } "Emptie" can "not `startWith` itself" in { - Emptie startsWith Emptie should be (false) + Emptie startsWith Emptie should be(false) } } class Exercise5_15 extends AnyFlatSpec with Matchers { "`tails` of Strim(1, 2, 3)" should "be Strim(Strim(1, 2, 3), Strim(1, 2), Strim(1), Strim())" in { - val s123 = Strim(1, 2, 3) - val tailsOfs123 = Lizt(Lizt(1, 2, 3), Lizt(2, 3), Lizt(3)) + val s123 = Strim(1, 2, 3) + val tailsOfs123 = Lizt(Lizt(1, 2, 3), Lizt(2, 3), Lizt(3)) - s123.tails.map( _.toLizt ).toLizt should be (tailsOfs123) - } + s123.tails.map(_.toLizt).toLizt should be(tailsOfs123) + } "`tails` of Emptie" should "be Emptie" in { - Emptie.tails should be (Emptie) + Emptie.tails should be(Emptie) } } class Exercise5_16 extends AnyFlatSpec with Matchers { "`scanRight` over Emptie" should "be the zero element be Emptie" in { - Emptie.scanRight(0){(a, b) => 1}.toLizt should be (Lizt(0)) - Emptie.scanRight("")((a, b) => "ab").toLizt should be (Lizt("")) - Emptie.scanRight(Emptie){(a, b) => Emptie}.toLizt should be (Lizt(Emptie)) + Emptie.scanRight(0) { (a, b) => 1 }.toLizt should be(Lizt(0)) + Emptie.scanRight("")((a, b) => "ab").toLizt should be(Lizt("")) + Emptie.scanRight(Emptie) { (a, b) => Emptie }.toLizt should be(Lizt(Emptie)) } "`scanRight` over Strim(1, 2, 3) with adder" should "be Strim(6, 5, 3, 0)" in { - Strim(1, 2, 3).scanRight(0)(_ + _).toLizt should be (Lizt(6, 5, 3, 0)) + Strim(1, 2, 3).scanRight(0)(_ + _).toLizt should be(Lizt(6, 5, 3, 0)) } "`scanRight` over Strim('a', 'b', 'c') with adder" should "be Strim('abc', 'bc', 'c', '')" in { - Strim("a", "b", "c").scanRight("")(_ + _).toLizt should be (Lizt("abc", "bc", "c", "")) + Strim("a", "b", "c").scanRight("")(_ + _).toLizt should be(Lizt("abc", "bc", "c", "")) } } class Misc extends AnyFlatSpec with Matchers { "Strim(3, 4)" should "be a subsequence of Strim(2, 3, 4, 5)" in { - Strim(2, 3, 4, 5) hasSubsequence Strim(3, 4) should be (true) + Strim(2, 3, 4, 5) hasSubsequence Strim(3, 4) should be(true) } "Strim(3, 4)" should "not be a subsequence of Strim(2, 3, 2, 3)" in { - Strim(2, 3, 2, 3) hasSubsequence Strim(3, 4) should be (false) + Strim(2, 3, 2, 3) hasSubsequence Strim(3, 4) should be(false) } "Emptie" should "not be a subsequence of anything" in { - Strim(1, 2, 3) hasSubsequence Emptie should be (false) - Strim() hasSubsequence Emptie should be (false) - Emptie hasSubsequence Emptie should be (false) + Strim(1, 2, 3) hasSubsequence Emptie should be(false) + Strim() hasSubsequence Emptie should be(false) + Emptie hasSubsequence Emptie should be(false) } "Nothing" should "be a subsequence of Emptie" in { - Emptie hasSubsequence Strim(1, 2, 3) should be (false) - Emptie hasSubsequence Strim() should be (false) - Emptie hasSubsequence Emptie should be (false) + Emptie hasSubsequence Strim(1, 2, 3) should be(false) + Emptie hasSubsequence Strim() should be(false) + Emptie hasSubsequence Emptie should be(false) } -} \ No newline at end of file +}