Skip to content

Commit c322bf9

Browse files
committed
minor Lazy change
1 parent ba74ed4 commit c322bf9

File tree

2 files changed

+19
-29
lines changed

2 files changed

+19
-29
lines changed

exercises/src/test/scala/exercises/CustomLazy.scala

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,17 +14,12 @@ class CustomLazy extends munit.FunSuite {
1414
def flatMap[B](f: A => Lazy[B]): Lazy[B] = ???
1515

1616
// TODO: Implement the fold function
17-
def fold(): A = ???
17+
def run(): A = ???
1818
}
1919

2020
object Lazy {
2121
// TODO: Implement the pure function
22-
def pure[A](a: () => A): Lazy[A] = ???
23-
}
24-
25-
def expensiveComputation(): Int = {
26-
log("expensive")
27-
42
22+
def pure[A](a: A): Lazy[A] = ???
2823
}
2924

3025
def increment(x: Int): Int = {
@@ -34,13 +29,13 @@ class CustomLazy extends munit.FunSuite {
3429

3530
def reverseString(x: Int): Lazy[String] = {
3631
log("reversed")
37-
Lazy.pure(() => x.toString.reverse)
32+
Lazy.pure(x.toString.reverse)
3833
}
3934

4035
test("creation phase") {
4136
val result = captureOutput {
4237
Lazy
43-
.pure(expensiveComputation)
38+
.pure(42)
4439
}
4540

4641
assertEquals(result, List())
@@ -49,7 +44,7 @@ class CustomLazy extends munit.FunSuite {
4944
test("combination phase - normal") {
5045
val result = captureOutput {
5146
Lazy
52-
.pure(expensiveComputation)
47+
.pure(42)
5348
.map(increment)
5449
}
5550

@@ -59,7 +54,7 @@ class CustomLazy extends munit.FunSuite {
5954
test("combination phase - effectful") {
6055
val result = captureOutput {
6156
Lazy
62-
.pure(expensiveComputation)
57+
.pure(42)
6358
.flatMap(reverseString)
6459
}
6560

@@ -69,10 +64,10 @@ class CustomLazy extends munit.FunSuite {
6964
test("removal phase value") {
7065
val result = captureOutput {
7166
Lazy
72-
.pure(expensiveComputation)
67+
.pure(42)
7368
.map(increment)
7469
.flatMap(reverseString)
75-
.fold()
70+
.run()
7671
}
7772

7873
assertEquals(result, List("expensive", "increment", "reversed"))

exercises/src/test/scala/exercises/answers/CustomLazy.scala

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,22 +6,17 @@ class CustomLazy extends munit.FunSuite {
66
case class Lazy[A](func: () => A) {
77

88
def map[B](f: A => B): Lazy[B] =
9-
Lazy.pure(() => f(func()))
9+
Lazy(() => f(func()))
1010

1111
def flatMap[B](f: A => Lazy[B]): Lazy[B] =
12-
Lazy.pure(() => f(func()).func())
12+
Lazy(() => f(func()).func())
1313

14-
def fold(): A =
14+
def run(): A =
1515
func()
1616
}
1717

1818
object Lazy {
19-
def pure[A](a: () => A): Lazy[A] = Lazy(a)
20-
}
21-
22-
def expensiveComputation(): Int = {
23-
log("expensive")
24-
42
19+
def pure[A](a: A): Lazy[A] = Lazy(() => a)
2520
}
2621

2722
def increment(x: Int): Int = {
@@ -31,13 +26,13 @@ class CustomLazy extends munit.FunSuite {
3126

3227
def reverseString(x: Int): Lazy[String] = {
3328
log("reversed")
34-
Lazy.pure(() => x.toString.reverse)
29+
Lazy.pure(x.toString.reverse)
3530
}
3631

3732
test("creation phase") {
3833
val result = captureOutput {
3934
Lazy
40-
.pure(expensiveComputation)
35+
.pure(42)
4136
}
4237

4338
assertEquals(result, List())
@@ -46,7 +41,7 @@ class CustomLazy extends munit.FunSuite {
4641
test("combination phase - normal") {
4742
val result = captureOutput {
4843
Lazy
49-
.pure(expensiveComputation)
44+
.pure(42)
5045
.map(increment)
5146
}
5247

@@ -56,7 +51,7 @@ class CustomLazy extends munit.FunSuite {
5651
test("combination phase - effectful") {
5752
val result = captureOutput {
5853
Lazy
59-
.pure(expensiveComputation)
54+
.pure(42)
6055
.flatMap(reverseString)
6156
}
6257

@@ -66,13 +61,13 @@ class CustomLazy extends munit.FunSuite {
6661
test("removal phase value") {
6762
val result = captureOutput {
6863
Lazy
69-
.pure(expensiveComputation)
64+
.pure(42)
7065
.map(increment)
7166
.flatMap(reverseString)
72-
.fold()
67+
.run()
7368
}
7469

75-
assertEquals(result, List("expensive", "increment", "reversed"))
70+
assertEquals(result, List("increment", "reversed"))
7671
}
7772

7873
def log(message: String): Unit =

0 commit comments

Comments
 (0)