Skip to content

Commit aed0021

Browse files
author
Alejandro Gómez
authored
Merge pull request #46 from jonboiser/patch-3
Extractors chapter - markdown fixes
2 parents fa4be00 + 8e09fa5 commit aed0021

File tree

1 file changed

+9
-9
lines changed

1 file changed

+9
-9
lines changed

src/main/scala/stdlib/Extractors.scala

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ object Extractors extends FlatSpec with Matchers with org.scalaexercises.definit
1111
*
1212
* For instance, the following code defines an extractor object `Twice`.
1313
*
14-
*
14+
* {{{
1515
* object Twice {
1616
* def apply(x: Int): Int = x * 2
1717
* def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None
@@ -21,26 +21,26 @@ object Extractors extends FlatSpec with Matchers with org.scalaexercises.definit
2121
* val x = Twice(21)
2222
* x match { case Twice(n) => Console.println(n) } // prints 21
2323
* }
24-
*
24+
* }}}
2525
*
2626
* There are two syntactic conventions at work here:
2727
*
28-
* * The pattern `case Twice(n)` will cause an invocation of `Twice.unapply`, which is used to match even number; the return value of the `unapply` signals whether the argument has matched or not, and any sub-values that can be used for further matching. Here, the sub-value is `z/2`
29-
* * The `apply` method is not necessary for pattern matching. It is only used to mimick a constructor. `val x = Twice(21)` expands to `val x = Twice.apply(21)`.
28+
* - The pattern `case Twice(n)` will cause an invocation of `Twice.unapply`, which is used to match even number; the return value of the `unapply` signals whether the argument has matched or not, and any sub-values that can be used for further matching. Here, the sub-value is `z/2`
29+
* - The `apply` method is not necessary for pattern matching. It is only used to mimick a constructor. `val x = Twice(21)` expands to `val x = Twice.apply(21)`.
3030
*
3131
* The code in the preceding example would be expanded as follows:
3232
*
33-
*
33+
* {{{
3434
* object TwiceTest extends Application {
3535
* val x = Twice.apply(21)
3636
* Twice.unapply(x) match { case Some(n) => Console.println(n) } // prints 21
3737
* }
38-
*
38+
* }}}
3939
* The return type of an `unapply` should be chosen as follows:
4040
*
41-
* * If it is just a test, return a `Boolean`. For instance `case even()`
42-
* * If it returns a single sub-value of type `T`, return a `Option[T]`
43-
* * If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`.
41+
* - If it is just a test, return a `Boolean`. For instance `case even()`
42+
* - If it returns a single sub-value of type `T`, return a `Option[T]`
43+
* - If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`.
4444
*
4545
* Sometimes, the number of sub-values is fixed and we would like to return a sequence. For this reason, you can also define patterns through `unapplySeq`. The last sub-value type `Tn` has to be `Seq[S]`. This mechanism is used for instance in pattern `case List(x1, ..., xn)`.
4646
*

0 commit comments

Comments
 (0)