Skip to content

#619 ScalaExercises 2º Part #11

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

Merged
merged 3 commits into from
Mar 31, 2017
Merged
Show file tree
Hide file tree
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
13 changes: 10 additions & 3 deletions src/main/scala/scalatutorial/sections/ClassesVsCaseClasses.scala
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
* }
*
* // toString redefinition to return the value of an instance instead of its memory addres
* override def toString = s"Note($name, $duration, $octave)"
* override def toString = s"Note($name,$duration,$octave)"
*
* // Create a copy of a case class, with potentially modified field values
* def copy(name: String = name, duration: String = duration, octave: Int = octave): Note =
Expand All @@ -170,12 +170,19 @@ object ClassesVsCaseClasses extends ScalaTutorialSection {
* new Note(name, duration, octave)
*
* // Extractor for pattern matching
* def unapply(note: Note): Option[(String, String, Int)) =
* def unapply(note: Note): Option[(String, String, Int)] =
* if (note eq null) None
* else Some((note.name, note.duration, note.octave))
*
* }
* }}}
*/
def nothing(): Unit = ()
def encoding(res0: String, res1: Boolean, res2: String): Unit = {
val c3 = Note("C", "Quarter", 3)
c3.toString shouldBe res0
val d = Note("D", "Quarter", 3)
c3.equals(d) shouldBe res1
val c4 = c3.copy(octave = 4)
c4.toString shouldBe res2
}
}
26 changes: 12 additions & 14 deletions src/main/scala/scalatutorial/sections/LazyEvaluation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,18 @@ object LazyEvaluation extends ScalaTutorialSection {
* lazy val x = expr
* }}}
*
* = Lazy Vals and Streams =
*
* Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently:
*
* {{{
* def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
* def head = hd
* lazy val tail = tl
* …
* }
* }}}
*
* == Exercise ==
*/
def lazyVal(res0: String): Unit = {
Expand All @@ -223,18 +235,4 @@ object LazyEvaluation extends ScalaTutorialSection {
builder.result() shouldBe res0
}

/**
* = Lazy Vals and Streams =
*
* Using a lazy value for `tail`, `Stream.cons` can be implemented more efficiently:
*
* {{{
* def cons[T](hd: T, tl: => Stream[T]) = new Stream[T] {
* def head = hd
* lazy val tail = tl
* …
* }
* }}}
*/
def nothing(): Unit = ()
}
28 changes: 14 additions & 14 deletions src/main/scala/scalatutorial/sections/StructuringInformation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -141,20 +141,20 @@ object StructuringInformation extends ScalaTutorialSection {
* the possible case of symbols is fixed. The compiler can leverage this
* knowledge to warn us if we write code that does not handle ''all''
* the cases:
*/
def unexhaustive(): Unit = {
sealed trait Symbol
case class Note(name: String, duration: String, octave: Int) extends Symbol
case class Rest(duration: String) extends Symbol

def nonExhaustiveDuration(symbol: Symbol): String =
symbol match {
case Rest(duration) => duration
}
}

/**
* Try to run the above code to see how the compiler informs us that
* {{{
* def unexhaustive(): Unit = {
* sealed trait Symbol
* case class Note(name: String, duration: String, octave: Int) extends Symbol
* case class Rest(duration: String) extends Symbol
*
* def nonExhaustiveDuration(symbol: Symbol): String =
* symbol match {
* case Rest(duration) => duration
* }
* }
*}}}
*
* If we try to run the above code to see how the compiler informs us that
* we don’t handle all the cases in `nonExhaustiveDuration`.
*
* = Equals =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,10 @@ class ClassesVsCaseClassesSpec extends Spec with Checkers {
def `check equality`: Unit =
check(Test.testSuccess(ClassesVsCaseClasses.equality _, false :: true :: HNil))

def `check encoding`: Unit =
check(
Test.testSuccess(
ClassesVsCaseClasses.encoding _,
"Note(C,Quarter,3)" :: false :: "Note(C,Quarter,4)" :: HNil))

}