Skip to content

Commit b079ce7

Browse files
authored
Merge pull request #11 from scala-exercises/arf-Fix_Empty_Exercises
#619 ScalaExercises 2º Part
2 parents 7a66f0e + 9669669 commit b079ce7

File tree

4 files changed

+42
-31
lines changed

4 files changed

+42
-31
lines changed

src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
155155
* }
156156
*
157157
* // toString redefinition to return the value of an instance instead of its memory addres
158-
* override def toString = s"Note($name, $duration, $octave)"
158+
* override def toString = s"Note($name,$duration,$octave)"
159159
*
160160
* // Create a copy of a case class, with potentially modified field values
161161
* def copy(name: String = name, duration: String = duration, octave: Int = octave): Note =
@@ -170,12 +170,19 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
170170
* new Note(name, duration, octave)
171171
*
172172
* // Extractor for pattern matching
173-
* def unapply(note: Note): Option[(String, String, Int)) =
173+
* def unapply(note: Note): Option[(String, String, Int)] =
174174
* if (note eq null) None
175175
* else Some((note.name, note.duration, note.octave))
176176
*
177177
* }
178178
* }}}
179179
*/
180-
def nothing(): Unit = ()
180+
def encoding(res0: String, res1: Boolean, res2: String): Unit = {
181+
val c3 = Note("C", "Quarter", 3)
182+
c3.toString shouldBe res0
183+
val d = Note("D", "Quarter", 3)
184+
c3.equals(d) shouldBe res1
185+
val c4 = c3.copy(octave = 4)
186+
c4.toString shouldBe res2
187+
}
181188
}

src/main/scala/scalatutorial/sections/LazyEvaluation.scala

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,18 @@ object LazyEvaluation extends ScalaTutorialSection {
209209
* lazy val x = expr
210210
* }}}
211211
*
212+
* = Lazy Vals and Streams =
213+
*
214+
* Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently:
215+
*
216+
* {{{
217+
* def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
218+
* def head = hd
219+
* lazy val tail = tl
220+
* …
221+
* }
222+
* }}}
223+
*
212224
* == Exercise ==
213225
*/
214226
def lazyVal(res0: String): Unit = {
@@ -223,18 +235,4 @@ object LazyEvaluation extends ScalaTutorialSection {
223235
builder.result() shouldBe res0
224236
}
225237

226-
/**
227-
* = Lazy Vals and Streams =
228-
*
229-
* Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently:
230-
*
231-
* {{{
232-
* def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
233-
* def head = hd
234-
* lazy val tail = tl
235-
* …
236-
* }
237-
* }}}
238-
*/
239-
def nothing(): Unit = ()
240238
}

src/main/scala/scalatutorial/sections/StructuringInformation.scala

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -141,20 +141,20 @@ object StructuringInformation extends ScalaTutorialSection {
141141
* the possible case of symbols is fixed. The compiler can leverage this
142142
* knowledge to warn us if we write code that does not handle ''all''
143143
* the cases:
144-
*/
145-
def unexhaustive(): Unit = {
146-
sealed trait Symbol
147-
case class Note(name: String, duration: String, octave: Int) extends Symbol
148-
case class Rest(duration: String) extends Symbol
149-
150-
def nonExhaustiveDuration(symbol: Symbol): String =
151-
symbol match {
152-
case Rest(duration) => duration
153-
}
154-
}
155-
156-
/**
157-
* Try to run the above code to see how the compiler informs us that
144+
* {{{
145+
* def unexhaustive(): Unit = {
146+
* sealed trait Symbol
147+
* case class Note(name: String, duration: String, octave: Int) extends Symbol
148+
* case class Rest(duration: String) extends Symbol
149+
*
150+
* def nonExhaustiveDuration(symbol: Symbol): String =
151+
* symbol match {
152+
* case Rest(duration) => duration
153+
* }
154+
* }
155+
*}}}
156+
*
157+
* If we try to run the above code to see how the compiler informs us that
158158
* we don’t handle all the cases in `nonExhaustiveDuration`.
159159
*
160160
* = Equals =

src/test/scala/scalatutorial/sections/ClassesVsCaseClassesSpec.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,10 @@ class ClassesVsCaseClassesSpec extends Spec with Checkers {
1919
def `check equality`: Unit =
2020
check(Test.testSuccess(ClassesVsCaseClasses.equality _, false :: true :: HNil))
2121

22+
def `check encoding`: Unit =
23+
check(
24+
Test.testSuccess(
25+
ClassesVsCaseClasses.encoding _,
26+
"Note(C,Quarter,3)" :: false :: "Note(C,Quarter,4)" :: HNil))
27+
2228
}

0 commit comments

Comments
 (0)