Skip to content

Commit bf85f44

Browse files
author
Rafa Paradela
committed
Merge pull request scala-exercises#43 from natansil/patch-1
add collection style operations to options
2 parents 1e30a1d + c57b356 commit bf85f44

File tree

1 file changed

+19
-1
lines changed

1 file changed

+19
-1
lines changed

app/json/options.json

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,24 @@
4545
"0.0"
4646
],
4747
"postparagraph": ""
48+
},
49+
{
50+
"preparagraph": "An alternative for pattern matching is performing collection style operations.\nThis is possible because an option could be looked at as a collection with either one or zero elements.\n\nOne of these operations is `map`. this operation allows to map the inner value to a different type while preserving the option",
51+
"code": "val number: Option[Int] = Some(3)\nval noNumber: Option[Int] = None\nval result1 = number.map(_ * 1.5)\nval result2 = noNumber.map(_ * 1.5)\n\nresult1 should be(__)\nresult2 should be(__)",
52+
"solutions": [
53+
"Some(4.5)",
54+
"None"
55+
],
56+
"postparagraph": ""
57+
},
58+
{
59+
"preparagraph": "Another operation is `fold`. this operation will extract the value from the option, or provide a default if the value is `None`",
60+
"code": "val number: Option[Int] = Some(3)\nval noNumber: Option[Int] = None\nval result1 = number.fold(0)(_ * 3)val result2 = noNumber.fold(0)(_ * 3)\n\nresult1 should be(__)\nresult2 should be(__)",
61+
"solutions": [
62+
"9",
63+
"0"
64+
],
65+
"postparagraph": ""
4866
}
4967
]
50-
}
68+
}

0 commit comments

Comments
 (0)