Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extractors chapter - markdown fixes #46

Merged
1 commit merged into from
Jul 18, 2016
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions src/main/scala/stdlib/Extractors.scala
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ object Extractors extends FlatSpec with Matchers with org.scalaexercises.definit
*
* For instance, the following code defines an extractor object `Twice`.
*
*
* {{{
* object Twice {
* def apply(x: Int): Int = x * 2
* def unapply(z: Int): Option[Int] = if (z%2 == 0) Some(z/2) else None
Expand All @@ -21,26 +21,26 @@ object Extractors extends FlatSpec with Matchers with org.scalaexercises.definit
* val x = Twice(21)
* x match { case Twice(n) => Console.println(n) } // prints 21
* }
*
* }}}
*
* There are two syntactic conventions at work here:
*
* * 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`
* * 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)`.
* - 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`
* - 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)`.
*
* The code in the preceding example would be expanded as follows:
*
*
* {{{
* object TwiceTest extends Application {
* val x = Twice.apply(21)
* Twice.unapply(x) match { case Some(n) => Console.println(n) } // prints 21
* }
*
* }}}
* The return type of an `unapply` should be chosen as follows:
*
* * If it is just a test, return a `Boolean`. For instance `case even()`
* * If it returns a single sub-value of type `T`, return a `Option[T]`
* * If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`.
* - If it is just a test, return a `Boolean`. For instance `case even()`
* - If it returns a single sub-value of type `T`, return a `Option[T]`
* - If you want to return several sub-values `T1,...,Tn`, group them in an optional tuple `Option[(T1,...,Tn)]`.
*
* 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)`.
*
Expand Down