@@ -55,7 +55,8 @@ object EitherStyleWithAdts {
5555 parse(s).flatMap(reciprocal).map(stringify)
5656}
5757
58- /** In day-to-day programming, it is fairly common to find ourselves writing functions that
58+ /**
59+ * In day-to-day programming, it is fairly common to find ourselves writing functions that
5960 * can fail. For instance, querying a service may result in a connection issue, or some
6061 * unexpected JSON response.
6162 *
@@ -94,7 +95,8 @@ object EitherStyleWithAdts {
9495 */
9596object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.definitions.Section {
9697
97- /** More often than not we want to just bias towards one side and call it a day - by convention,
98+ /**
99+ * More often than not we want to just bias towards one side and call it a day - by convention,
98100 * the right side is most often chosen.
99101 */
100102 def eitherMapRightBias (res0 : Either [String , Int ], res1 : Either [String , Int ]) = {
@@ -106,7 +108,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
106108 left.map(_ + 1 ) should be(res1)
107109 }
108110
109- /** Because `Either` is right-biased, it is possible to define a `Monad` instance for it.
111+ /**
112+ * Because `Either` is right-biased, it is possible to define a `Monad` instance for it.
110113 *
111114 * Since we only ever want the computation to continue in the case of `Right` (as captured
112115 * by the right-bias nature), we fix the left type parameter and leave the right one free.
@@ -125,7 +128,6 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
125128 * }}}
126129 *
127130 * So the `flatMap` method is right-biased:
128- *
129131 */
130132 def eitherMonad (res0 : Either [String , Int ], res1 : Either [String , Int ]) = {
131133
@@ -136,7 +138,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
136138 left.flatMap(x => Either .right(x + 1 )) should be(res1)
137139 }
138140
139- /** = Using `Either` instead of exceptions =
141+ /**
142+ * = Using `Either` instead of exceptions =
140143 *
141144 * As a running example, we will have a series of functions that will parse a string into an integer,
142145 * take the reciprocal, and then turn the reciprocal into a string.
@@ -178,15 +181,14 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
178181 * }}}
179182 *
180183 * Do these calls return a `Right` value?
181- *
182184 */
183185 def eitherStyleParse (res0 : Boolean , res1 : Boolean ) = {
184186 EitherStyle .parse(" Not a number" ).isRight should be(res0)
185187 EitherStyle .parse(" 2" ).isRight should be(res1)
186188 }
187189
188- /** Now, using combinators like `flatMap` and `map`, we can compose our functions together. Will the following incantations return a `Right` value?
189- *
190+ /**
191+ * Now, using combinators like `flatMap` and `map`, we can compose our functions together. Will the following incantations return a `Right` value?
190192 */
191193 def eitherComposition (res0 : Boolean , res1 : Boolean , res2 : Boolean ) = {
192194 import EitherStyle ._
@@ -196,7 +198,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
196198 magic(" Not a number" ).isRight should be(res2)
197199 }
198200
199- /** With the composite function that we actually care about, we can pass in strings and then pattern
201+ /**
202+ * With the composite function that we actually care about, we can pass in strings and then pattern
200203 * match on the exception. Because `Either` is a sealed type (often referred to as an algebraic data type,
201204 * or ADT), the compiler will complain if we do not check both the `Left` and `Right` case.
202205 *
@@ -207,7 +210,6 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
207210 * `NumberFormatException` or `IllegalArgumentException`. However, we "know" by inspection of the source
208211 * that those will be the only exceptions thrown, so it seems strange to have to account for other exceptions.
209212 * This implies that there is still room to improve.
210- *
211213 */
212214 def eitherExceptions (res0 : String ) = {
213215 import EitherStyle ._
@@ -221,7 +223,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
221223 result should be(res0)
222224 }
223225
224- /** Instead of using exceptions as our error value, let's instead enumerate explicitly the things that
226+ /**
227+ * Instead of using exceptions as our error value, let's instead enumerate explicitly the things that
225228 * can go wrong in our program.
226229 *
227230 * {{{
@@ -249,7 +252,6 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
249252 * exception classes as error values, we use one of the enumerated cases. Now when we pattern
250253 * match, we get much nicer matching. Moreover, since `Error` is `sealed`, no outside code can
251254 * add additional subtypes which we might fail to handle.
252- *
253255 */
254256 def eitherErrorsAsAdts (res0 : String ) = {
255257 import EitherStyleWithAdts ._
@@ -262,7 +264,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
262264 result should be(res0)
263265 }
264266
265- /** = Either in the small, Either in the large =
267+ /**
268+ * = Either in the small, Either in the large =
266269 *
267270 * Once you start using `Either` for all your error-handling, you may quickly run into an issue where
268271 * you need to call into two separate modules which give back separate kinds of errors.
@@ -380,7 +383,6 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
380383 * }}}
381384 *
382385 * Let's review the `leftMap` and `map` methods:
383- *
384386 */
385387 def eitherInTheLarge (
386388 res0 : Either [String , Int ],
@@ -395,7 +397,8 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
395397 left.leftMap(_.reverse) should be(res2)
396398 }
397399
398- /** There will inevitably come a time when your nice `Either` code will have to interact with exception-throwing
400+ /**
401+ * There will inevitably come a time when your nice `Either` code will have to interact with exception-throwing
399402 * code. Handling such situations is easy enough.
400403 *
401404 * {{{
@@ -417,16 +420,15 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
417420 * }}}
418421 *
419422 * If you want to catch all (non-fatal) throwables, you can use `catchNonFatal`.
420- *
421- *
422423 */
423424 def eitherWithExceptions (res0 : Boolean , res1 : Boolean ) = {
424425 Either .catchOnly[NumberFormatException ](" abc" .toInt).isRight should be(res0)
425426
426427 Either .catchNonFatal(1 / 0 ).isLeft should be(res1)
427428 }
428429
429- /** = Additional syntax =
430+ /**
431+ * = Additional syntax =
430432 *
431433 * For using Either's syntax on arbitrary data types, you can import `cats.implicits._`. This will
432434 * make possible to use the `asLeft` and `asRight` methods:
@@ -440,7 +442,6 @@ object EitherSection extends AnyFlatSpec with Matchers with org.scalaexercises.d
440442 * }}}
441443 *
442444 * These method promote values to the `Either` data type:
443- *
444445 */
445446 def eitherSyntax (res0 : Either [String , Int ]) = {
446447 import cats .implicits ._
0 commit comments