Skip to content

Commit

Permalink
chore: more formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
amuradyan committed Sep 15, 2024
1 parent b204c13 commit dfb630d
Show file tree
Hide file tree
Showing 8 changed files with 245 additions and 238 deletions.
2 changes: 1 addition & 1 deletion src/main/scala/handling_errors/Eether.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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) }))
Expand Down
12 changes: 6 additions & 6 deletions src/main/scala/purely_functional_state/Steyt.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,23 @@ 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)

(f(a, b), __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)
}
Expand All @@ -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 {
Expand Down
8 changes: 4 additions & 4 deletions src/main/scala/strictness_and_laziness/Strim.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}

Expand Down Expand Up @@ -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)
Expand Down
72 changes: 36 additions & 36 deletions src/test/scala/data_structures/Tri.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,102 +11,102 @@ 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)
}
}

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)
}
}

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)
}
}

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)
}
}

Expand All @@ -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 {
Expand All @@ -130,32 +130,32 @@ 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 {
val t12345 = Brench(Brench(Leef(1), Brench(Leef(2), Leef(3))), Brench(Leef(4), Leef(5)))
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))
}
}
26 changes: 14 additions & 12 deletions src/test/scala/purely_functional_state/CandyMachine.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)
}
}
}
Loading

0 comments on commit dfb630d

Please sign in to comment.