From 75f354fb753817d5b27e92ea9204e59e8199062d Mon Sep 17 00:00:00 2001 From: Sergei Winitzki Date: Mon, 18 Mar 2024 10:08:33 +0100 Subject: [PATCH 1/2] add scala 3.3.3 and 3.4.0 to tests --- .github/workflows/build-and-test.yml | 4 +++- github-scala-build-and-test.dhall | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.github/workflows/build-and-test.yml b/.github/workflows/build-and-test.yml index 905ef96..a0ef768 100644 --- a/.github/workflows/build-and-test.yml +++ b/.github/workflows/build-and-test.yml @@ -29,7 +29,9 @@ jobs: java: - '17.0.9' scala: - - '2.13.11' + - '2.13.13' + - '3.3.3' + - '3.4.0' checks: name: Check formatting runs-on: ubuntu-latest diff --git a/github-scala-build-and-test.dhall b/github-scala-build-and-test.dhall index 76b56af..d8719c6 100644 --- a/github-scala-build-and-test.dhall +++ b/github-scala-build-and-test.dhall @@ -7,7 +7,7 @@ let matrix = -- "8.0.382", -- "11.0.21", "17.0.9", - ], scala = [ "2.13.11" ] } + ], scala = [ "2.13.13", "3.3.3", "3.4.0", ] } let setup = [ GithubActions.steps.actions/checkout // { `with` = Some (toMap { submodules = "true" }) } From 51fd24a7b8c10325d3f788b653a3b53ff3249004 Mon Sep 17 00:00:00 2001 From: Sergei Winitzki Date: Mon, 18 Mar 2024 10:27:05 +0100 Subject: [PATCH 2/2] fix problems --- README.md | 7 +++++++ build.sbt | 4 ++-- chapter01/worksheets/solution1.1.1.1_as_object_AR.scala | 2 +- chapter01/worksheets/solution1.6.2.3.scala | 2 +- chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala | 2 +- chapter02/worksheets/solution2.1.7.10_object_main.scala | 2 +- chapter02/worksheets/solution2.1.7.3_AR.scala | 2 +- chapter02/worksheets/solution2.1.7.4_AR.scala | 2 +- chapter02/worksheets/solution2.1.7.4_as_function_AR.scala | 2 +- chapter02/worksheets/solution2.1.7.9_object_main.scala | 2 +- 10 files changed, 17 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index a8a79b6..1f79a38 100644 --- a/README.md +++ b/README.md @@ -14,6 +14,8 @@ Files under `worksheets/` may be arbitrary Scala code, not made into a test. Run the command `bash run_sbt.sh test` in order to verify all solutions. This will create temporary solution files in each chapter. +To compile the code with all supported Scala versions, run `bash run_sbt.sh +test:compile` (just to compile under all Scala versions) or `bash run_sbt.sh +test` for full testing. + # Code formatting The GitHub job will verify that all code is properly formatted. @@ -23,3 +25,8 @@ To reformat code, run the script: `bash reformat_all_code.sh` Run this command before pushing any code changes in the test code. If formatting is not applied, builds will fail. + +# Configure the build + +The GitHub build is configured via the Dhall file [github-scala-build-and-test.dhall](./github-scala-build-and-test.dhall). +After changing that file, run `bash ./update_github_actions.sh` and commit changes in the Yaml file [build-and-test.yml](./.github/workflows/build-and-test.yml). diff --git a/build.sbt b/build.sbt index dde5f18..e84501a 100644 --- a/build.sbt +++ b/build.sbt @@ -1,5 +1,5 @@ -val scala2V = "2.13.11" -val scala3V = "3.3.1" +val scala2V = "2.13.13" +val scala3V = "3.3.3" val scalaV = scala2V val munitTest = "org.scalameta" %% "munit" % "0.7.29" % Test diff --git a/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala b/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala index 53452e9..3a1d81e 100644 --- a/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala +++ b/chapter01/worksheets/solution1.1.1.1_as_object_AR.scala @@ -9,7 +9,7 @@ object MyFactorial { def myFactorial(a: Int): Int = { (1 to a).foldRight(1)(_ * _) } - def main(args: Array[String]) { + def main(args: Array[String]) = { val expected = 3628800 val result = this.myFactorial(10) println("result: %s".format(result)) diff --git a/chapter01/worksheets/solution1.6.2.3.scala b/chapter01/worksheets/solution1.6.2.3.scala index d22e25c..6582e23 100644 --- a/chapter01/worksheets/solution1.6.2.3.scala +++ b/chapter01/worksheets/solution1.6.2.3.scala @@ -13,7 +13,7 @@ that function. def is3Factor(n: Int): Boolean = { val q = (2 to n-1) val r = q.filter(y => (n != y) && (n % y == 0)) - (r.length == 3) + r.length == 3 } def nestedIs3Factor(f: (Int => Boolean)): List[Int] = { diff --git a/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala b/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala index ce20fc2..6009fca 100644 --- a/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala +++ b/chapter02/src/test/scala/sofp/unit/Exercises_2_1_7.scala @@ -23,7 +23,7 @@ class Exercises_2_1_7 extends FunSuite { val names: List[String] = List("Joe", "Bob", "Mary") val a: List[Boolean] = List(true, false, true) val b: List[(String, Boolean)] = names.zip(a) - val c: List[Any] = b.map { case (x, y) => if (y == true) x } + val c: List[Any] = b.map { case (x, y) => if (y == true) x else () } val d: List[Any] = c.filter(_ != (())) expect(d == List("Joe", "Mary")) } diff --git a/chapter02/worksheets/solution2.1.7.10_object_main.scala b/chapter02/worksheets/solution2.1.7.10_object_main.scala index 18dca49..6d7f21b 100644 --- a/chapter02/worksheets/solution2.1.7.10_object_main.scala +++ b/chapter02/worksheets/solution2.1.7.10_object_main.scala @@ -18,7 +18,7 @@ object LargestTree { def largestTree(a: Seq[List[Int]]): Seq[List[Int]] = { a.map{ k=>k.sortBy(k=>(-k))}.map{ k => k.take(3) } } - def main(args: Array[String]) { + def main(args: Array[String]) = { val expected = Seq(List(50, 30, 10), List(100), List(200, 20, 2)) val result = this.largestTree(Seq(List(50, 30, 10), List(100), List(200, 20, 2))) assert(result == expected) diff --git a/chapter02/worksheets/solution2.1.7.3_AR.scala b/chapter02/worksheets/solution2.1.7.3_AR.scala index 84e9dae..caf65b8 100644 --- a/chapter02/worksheets/solution2.1.7.3_AR.scala +++ b/chapter02/worksheets/solution2.1.7.3_AR.scala @@ -5,7 +5,7 @@ Given two sequences p: Seq[String] and q: Seq[Boolean] of equal length, compute Seq[String] with those elements of p for which the corresponding element of q is true. Hint: use zip, map, filter. */ -val result = Seq("Joe", "Bob", "Mary").zip(Seq(true, false, true)).map { case (x, y) => if (y == true) x }.filter(_ != (())).map{ case x => x.toString } +val result = Seq("Joe", "Bob", "Mary").zip(Seq(true, false, true)).map { case (x, y) => if (y == true) x else () }.filter(_ != (())).map{ case x => x.toString } assert(result == List("Joe", "Mary")) // scala> :load solution2.1.7.3_AR.scala diff --git a/chapter02/worksheets/solution2.1.7.4_AR.scala b/chapter02/worksheets/solution2.1.7.4_AR.scala index c6cea0a..53ded03 100644 --- a/chapter02/worksheets/solution2.1.7.4_AR.scala +++ b/chapter02/worksheets/solution2.1.7.4_AR.scala @@ -13,7 +13,7 @@ val a = Seq[Int](1, 3, 2, 4) val b = a.last val c: Seq[(Int, Boolean)] = Seq((b, false)) // val c = List(b, false) -val d: Seq[(Int, Boolean)] = Seq[Int](1, 3, 2, 4).sliding(2).toList.map{ case List(x, y) => if (x < y) (x, true) else (x, false)} +val d: Seq[(Int, Boolean)] = Seq[Int](1, 3, 2, 4).sliding(2).toList.map{ case Seq(x, y) => if (x < y) (x, true) else (x, false)} val result = d ++ c val expected: Seq[(Int, Boolean)] = List((1,true), (3,false), (2,true), (4,false)) diff --git a/chapter02/worksheets/solution2.1.7.4_as_function_AR.scala b/chapter02/worksheets/solution2.1.7.4_as_function_AR.scala index 4f17f97..f4859cb 100644 --- a/chapter02/worksheets/solution2.1.7.4_as_function_AR.scala +++ b/chapter02/worksheets/solution2.1.7.4_as_function_AR.scala @@ -12,7 +12,7 @@ def followedByGreater(a: Seq[Int]): Seq[(Int, Boolean)] = { val b = a.last val c: Seq[(Int, Boolean)] = Seq((b, false)) - val d: Seq[(Int, Boolean)] = a.sliding(2).toList.map{ case List(x, y) => if (x < y) (x, true) else (x, false)} + val d: Seq[(Int, Boolean)] = a.sliding(2).toList.map{ case Seq(x, y) => if (x < y) (x, true) else (x, false)} val e = d ++ c e } diff --git a/chapter02/worksheets/solution2.1.7.9_object_main.scala b/chapter02/worksheets/solution2.1.7.9_object_main.scala index d6bea7d..8b92ee4 100644 --- a/chapter02/worksheets/solution2.1.7.9_object_main.scala +++ b/chapter02/worksheets/solution2.1.7.9_object_main.scala @@ -15,7 +15,7 @@ object TotalCount { def totalCount(a: Seq[(String, Int)]): Map[String, Int] = { a.groupBy(s => s._1).map { case (x, y) => (x, y.map(_._2).sum) } } - def main(args: Array[String]) { + def main(args: Array[String]) = { val expected = Map("apple" -> 10, "pear" -> 3, "lemon" -> 2) val result = this.totalCount(Seq(("apple", 2), ("pear", 3), ("apple", 5), ("lemon", 2), ("apple", 3))) assert(result == expected)