Skip to content

Commit

Permalink
Bring to latest Scala and SBT versions, fix compile errors
Browse files Browse the repository at this point in the history
  • Loading branch information
iht committed Sep 28, 2019
1 parent a730fd2 commit cbc1ca4
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 22 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,6 @@ project/plugins/project/
# Emacs Ensime specific
.ensime
.ensime_cache

# IntelliJ
.idea
4 changes: 2 additions & 2 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ homepage := Some(url("https://github.com/iht/fpinscala/"))

startYear := Some(2014)

scalaVersion := "2.12.6"
scalaVersion := "2.13.1"

libraryDependencies += "org.specs2" %% "specs2-core" % "4.2.0" % "test"
libraryDependencies += "org.specs2" %% "specs2-core" % "4.7.1" % "test"

scalacOptions in Test ++= Seq("-Yrangepos")

Expand Down
2 changes: 1 addition & 1 deletion project/build.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@
sbt.version=1.1.6
sbt.version=1.3.2
4 changes: 2 additions & 2 deletions project/build.sbt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
addSbtPlugin("org.scoverage" % "sbt-scoverage" % "1.5.1")

addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.2.5")
addSbtPlugin("org.scoverage" % "sbt-coveralls" % "1.2.7")

addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.2.1")
addSbtPlugin("org.wartremover" % "sbt-wartremover" % "2.4.2")
32 changes: 16 additions & 16 deletions src/main/scala/chap03/ex26.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
* ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
* CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
*/

// ---------------------
// Code for example 3.26
Expand All @@ -30,10 +30,20 @@ package chap03
import adt._

object Ex26 {


// Naive version
// Causes stack overflow with very deep trees
def maximumNaive[A: Numeric](t: Tree[A]): A = {
val maxOp: (A, A) => A = implicitly[Numeric[A]].max _
t match {
case Leaf(x) => x
case Branch(l, r) => maxOp(maximum(l), maximum(r))
}
}

// Tail recursive version
def maximum[A: Numeric](t: Tree[A]): A = {
val maxOp = implicitly[Numeric[A]].max _
val maxOp: (A, A) => A = implicitly[Numeric[A]].max _

// This should be the smallest possible value for
// Numeric type A
Expand All @@ -46,22 +56,12 @@ object Ex26 {
@annotation.tailrec
def loop(t1: List[Tree[A]], m: A): A = {
t1 match {
case Nil => m
case Leaf(x) :: lt => loop(lt, maxOp(m,x))
case Branch(l,r) :: lt => loop(l :: r :: lt, m)
case Nil => m
case Leaf(x) :: lt => loop(lt, maxOp(m, x))
case Branch(l, r) :: lt => loop(l :: r :: lt, m)
}
}

loop(List(t), zero)
}

// Naive version
// Causes stack overflow with very deep trees
def maximumNaive[A: Numeric](t: Tree[A]): A = {
val maxOp = implicitly[Numeric[A]].max _
t match {
case Leaf(x) => x
case Branch(l, r) => maxOp(maximum(l), maximum(r))
}
}
}
2 changes: 1 addition & 1 deletion src/main/scala/chap03/ex29.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ object Ex29 {
}

def maximum[A: Numeric](t: Tree[A]): A = {
val max = implicitly[Numeric[A]].max _
val max: (A,A) => A = implicitly[Numeric[A]].max _
val minVal = implicitly[Numeric[A]].fromInt(Int.MinValue)

fold(t, minVal)((m,y) => max(m,y))
Expand Down

0 comments on commit cbc1ca4

Please sign in to comment.