Skip to content

Commit

Permalink
Update Options.scala
Browse files Browse the repository at this point in the history
Using `noNumber.fold(0)(_ * 3)` makes it unclear whether or not the default value goes through the same process. In other words, is the result 0 because that's the value we provide, or because `0 = 0 * 3`?

Using `val result2 = noNumber.fold(1)(_ * 3)` makes it clearer, as the result is 1 and not 3.
  • Loading branch information
KevinGreene authored May 5, 2017
1 parent 13aa90a commit 537b787
Showing 1 changed file with 2 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/main/scala/stdlib/Options.scala
Original file line number Diff line number Diff line change
Expand Up @@ -98,8 +98,8 @@ object Options extends FlatSpec with Matchers with org.scalaexercises.definition
def foldOptions(res0: Int, res1: Int) {
val number: Option[Int] = Some(3)
val noNumber: Option[Int] = None
val result1 = number.fold(0)(_ * 3)
val result2 = noNumber.fold(0)(_ * 3)
val result1 = number.fold(1)(_ * 3)
val result2 = noNumber.fold(1)(_ * 3)

result1 should be(res0)
result2 should be(res1)
Expand Down

0 comments on commit 537b787

Please sign in to comment.