Last active
May 25, 2024 10:20
Revisions
-
dacr revised this gist
May 25, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ // run-with : scala-cli $file // --------------------- //> using scala "3.4.2" //> using dep "org.scalatest::scalatest:3.2.16" //> using objectWrapper // --------------------- -
dacr revised this gist
May 25, 2024 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,7 +9,7 @@ // run-with : scala-cli $file // --------------------- //> using scala "3.4.2" //> using dep "org.scalatest::scalatest:3.2.16" //> using objectWrapper // --------------------- -
dacr revised this gist
May 27, 2023 . 1 changed file with 3 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,9 @@ // run-with : scala-cli $file // --------------------- //> using scala "3.3.0" //> using dep "org.scalatest::scalatest:3.2.16" //> using objectWrapper // --------------------- import org.scalatest._, wordspec._, matchers._, OptionValues._ -
dacr revised this gist
Apr 2, 2023 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,15 +2,15 @@ // keywords : scala, learning, @testable // publish : gist // authors : David Crosson // license : Apache NON-AI License Version 2.0 (https://raw.githubusercontent.com/non-ai-licenses/non-ai-licenses/main/NON-AI-APACHE2) // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // managed-by : https://github.com/dacr/code-examples-manager // run-with : scala-cli $file // --------------------- //> using scala "3.1.1" //> using dep "org.scalatest::scalatest:3.2.10" // --------------------- import org.scalatest._, wordspec._, matchers._, OptionValues._ -
dacr revised this gist
Dec 11, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ // keywords : scala, learning, @testable // publish : gist // authors : David Crosson // license : Apache2 BUT Machine Learning models training is not allowed by the author // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // managed-by : https://github.com/dacr/code-examples-manager -
dacr revised this gist
Jul 20, 2022 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // managed-by : https://github.com/dacr/code-examples-manager // run-with : scala-cli $file // --------------------- //> using scala "3.1.1" -
dacr revised this gist
Jan 28, 2022 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,8 @@ // run-with : scala-cli $scriptFile // --------------------- //> using scala "3.1.1" //> using lib "org.scalatest::scalatest:3.2.10" // --------------------- import org.scalatest._, wordspec._, matchers._, OptionValues._ -
dacr revised this gist
Dec 18, 2021 . 1 changed file with 6 additions and 6 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -38,9 +38,10 @@ class ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { list.tail shouldBe List(1, 3, 1, 5, 2, 2) list.last shouldBe 2 intercept[Exception] { List.empty[Int].last shouldBe 1 } List.empty[Int].lastOption shouldBe None List.empty[Int].lastOption shouldBe None list.init shouldBe List(4, 1, 3, 1, 5, 2) list.take(2) shouldBe List(4, 1) list.drop(4) shouldBe List(5, 2, 2) @@ -113,8 +114,8 @@ class ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { Array(1, 2, 3) === Array(1, 2, 3L) shouldBe true Array(1, 2, Array(3, 4)) === Array(1, 2, Array(3, 4)) shouldBe true Array(1, 2, List(3, 4)) === Array(1, 2, List(3, 5)) shouldBe false (Array(1, 2, List(3, 4)) !== Array(1, 2, List(3, 5))) shouldBe true // ??? (Array(1, 2, Array(3, 4)) !== Array(1, 2, Array(3, 5))) shouldBe true } } // =================================================== @@ -142,9 +143,8 @@ class ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { """line1 |line2""".stripMargin "line1\nline2" should be equals res "line1\nline2" shouldEqual res String("line1\nline2") should be(res) } // ------------------------------------------------- "support basic string interpolations" in { -
dacr revised this gist
Nov 30, 2021 . 1 changed file with 7 additions and 3 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,9 +6,13 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // managed-by : https://github.com/dacr/code-examples-manager // run-with : scala-cli $scriptFile // --------------------- // using scala 3.1.0 // using lib "org.scalatest::scalatest:3.2.10" // --------------------- import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale @@ -34,7 +38,7 @@ class ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { list.tail shouldBe List(1, 3, 1, 5, 2, 2) list.last shouldBe 2 intercept[Exception] { List.empty[Int].lastOption shouldBe None } List.empty[Int].lastOption shouldBe None list.init shouldBe List(4, 1, 3, 1, 5, 2) -
dacr revised this gist
Nov 30, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,6 +5,7 @@ // license : Apache2 // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // managed-by : https://github.com/dacr/code-examples-manager // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.6` -
dacr revised this gist
Nov 29, 2021 . 1 changed file with 1 addition and 0 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -4,6 +4,7 @@ // authors : David Crosson // license : Apache2 // id : 84200162-6102-4e5c-9ade-19296bc405f8 // created-on : 2020-05-31T19:54:52Z // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.6` -
dacr revised this gist
Mar 27, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.6` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale -
dacr revised this gist
Mar 7, 2021 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.5` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale -
dacr revised this gist
Oct 9, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -2,7 +2,7 @@ // keywords : scala, learning, @testable // publish : gist // authors : David Crosson // license : Apache2 // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' -
dacr revised this gist
Oct 3, 2020 . No changes.There are no files selected for viewing
-
dacr revised this gist
Sep 8, 2020 . 1 changed file with 2 additions and 2 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import $ivy.`org.scalatest::scalatest:3.2.2` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale class ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { override def suiteName: String = "Scala Language Basics" "scala language" must { @@ -206,4 +206,4 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { } org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ScalaLanguageBasicsTest].getName)) -
dacr revised this gist
Sep 6, 2020 . 1 changed file with 0 additions and 58 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -9,8 +9,6 @@ import $ivy.`org.scalatest::scalatest:3.2.2` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { override def suiteName: String = "Scala Language Basics" @@ -208,60 +206,4 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { } ScalaLanguageBasicsTest.execute() -
dacr revised this gist
Sep 4, 2020 . 1 changed file with 1 addition and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -6,7 +6,7 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.2` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale import scala.concurrent.Future -
dacr revised this gist
Jul 7, 2020 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -1,7 +1,8 @@ // summary : scala language // keywords : scala, learning, @testable // publish : gist // authors : David Crosson // license : GPL // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' -
dacr revised this gist
Jun 30, 2020 . 1 changed file with 17 additions and 1 deletion.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -108,7 +108,7 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { Array(1, 2, 3) === Array(1, 2, 3L) shouldBe true Array(1, 2, Array(3, 4)) === Array(1, 2, Array(3, 4)) shouldBe true Array(1, 2, List(3, 4)) === Array(1, 2, List(3, 5)) shouldBe false Array(1, 2, List(3, 4)) !== Array(1, 2, List(3, 5)) shouldBe true // ??? Array(1, 2, Array(3, 4)) !== Array(1, 2, Array(3, 5)) shouldBe true } } @@ -187,6 +187,22 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { } } // =================================================== "Currying" can { "support in lambda" in { val f = (x:Int) => (y:Int) => (z:Int) => x+y+z // ------------- f(1) f(1)(2) f(1)(2)(3) } "support in function" in { def f(x:Int)(y:Int)(z:Int):Int = x+y+z // ------------- f(1)(_:Int)(_:Int) // mandatory at lest up to scala 2.13 f(1)(2)(_) f(1)(2)(3) } } } } -
dacr revised this gist
Jun 26, 2020 . 1 changed file with 14 additions and 5 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,9 +5,9 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.2.0` import org.scalatest._, wordspec._, matchers._, OptionValues._ import java.util.Locale import scala.concurrent.Future import scala.util.Success @@ -101,9 +101,13 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { Array(1, 2, 3) == Array(1, 2, 3) shouldBe true } info("But with the triple equal, === it is correctly checked") List(1, 2, 3) === List(1, 2, 3) shouldBe true List(1, 2, 3) === List(1, 2, 4) shouldBe false Array(1, 2, 3) === Array(1, 2, 3) shouldBe true Array(1, 2, 3) === Array(1, 2, 4) shouldBe false Array(1, 2, 3) === Array(1, 2, 3L) shouldBe true Array(1, 2, Array(3, 4)) === Array(1, 2, Array(3, 4)) shouldBe true Array(1, 2, List(3, 4)) === Array(1, 2, List(3, 5)) shouldBe false Array(1, 2, List(3, 4)) !== Array(1, 2, List(3, 5)) shouldBe true Array(1, 2, Array(3, 4)) !== Array(1, 2, Array(3, 5)) shouldBe true } @@ -129,9 +133,13 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { // ------------------------------------------------- "be defined using multiple lines" in { info("With stripMargin method you can even keep the right code indentation") val res = """line1 |line2""".stripMargin "line1\nline2" should be equals res //"line1\nline2" shouldEqual res // Strange it doesn't work info("Take care with be and references !") "line1\nline2" should not be(res) // --- Take care so !! be != equals } // ------------------------------------------------- "support basic string interpolations" in { @@ -147,8 +155,9 @@ object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { } // ------------------------------------------------- "support basic printf like interpolations with locale changes" in { Locale.setDefault(Locale.US) // TODO - find better alternative val price = 32.54351 f"The price is $price%.1f dollar" shouldBe "The price is 32.5 dollar" } // ------------------------------------------------- "support basic raw strings" in { -
dacr revised this gist
Apr 23, 2020 . 1 changed file with 4 additions and 4 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -5,13 +5,13 @@ // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.1.1` import org.scalatest._, wordspec._, matchers._, OptionValues._ import scala.concurrent.Future import scala.util.Success object ScalaLanguageBasicsTest extends AnyWordSpec with should.Matchers { override def suiteName: String = "Scala Language Basics" "scala language" must { @@ -182,7 +182,7 @@ object ScalaLanguageBasicsTest extends WordSpec with Matchers { } object ScalaLanguageAsyncBasicsTest extends AsyncWordSpec with should.Matchers { override def suiteName: String = "Scala Language Asynchronous Basics" "Scala language" must { -
dacr revised this gist
Nov 30, 2019 . 1 changed file with 162 additions and 20 deletions.There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -8,9 +8,13 @@ import $ivy.`org.scalatest::scalatest:3.0.8` import org.scalatest._ import scala.concurrent.Future import scala.util.Success object ScalaLanguageBasicsTest extends WordSpec with Matchers { override def suiteName: String = "Scala Language Basics" "scala language" must { // =================================================== "List" can { // ------------------------------------------------- @@ -19,35 +23,95 @@ object ScalaLanguageBasicsTest extends WordSpec with Matchers { } // ------------------------------------------------- "be initialized" in { List(1, 2, 3, 4).size shouldBe 4 } // ------------------------------------------------- "provide a set of standard useful operations" in { val list = List(4, 1, 3, 1, 5, 2, 2) list.size shouldBe 7 list.head shouldBe 4 list.tail shouldBe List(1, 3, 1, 5, 2, 2) list.last shouldBe 2 intercept[Exception] { List.empty[Int].last shouldBe None } List.empty[Int].lastOption shouldBe None list.init shouldBe List(4, 1, 3, 1, 5, 2) list.take(2) shouldBe List(4, 1) list.drop(4) shouldBe List(5, 2, 2) list.distinct shouldBe List(4, 1, 3, 5, 2) list.sorted shouldBe List(1, 1, 2, 2, 3, 4, 5) list.reverse shouldBe List(2, 2, 5, 1, 3, 1, 4) } // ------------------------------------------------- "be filtered, queried and checked" in { val list = List(4, 1, 3, 1, 5, 2, 2) list.exists(_ == 5) shouldBe true list.count(_ % 3 == 0) shouldBe 1 list.contains(5) shouldBe true list.find(_ == 3) shouldBe Some(3) list.find(_ == 42) shouldBe None list.filter(_ < 3) shouldBe List(1, 1, 2, 2) list.filterNot(_ == 2) shouldBe List(4, 1, 3, 1, 5) list.forall(_ % 2 == 0) shouldBe false list.min shouldBe 1 list.max shouldBe 5 list.sum shouldBe 18 list.reduce(_ + _) shouldBe 18 intercept[Exception] { List.empty[Int].min shouldBe 0 } List.empty[Int].minOption shouldBe None List.empty[Int].maxOption shouldBe None List.empty[Int].sum shouldBe 0 } // ------------------------------------------------- "be transformed" in { val list = List(4, 1, 3, 1, 5, 2, 2) list.map(_ + 1) shouldBe List(5, 2, 4, 2, 6, 3, 3) } // ------------------------------------------------- "comparable each other" in { List(1, 2, 3) == List(1, 2, 3) shouldBe true List(1, 2, 3) != List(1, 2, 4) shouldBe true List(1, 2, List(3, 4)) == List(1, 2, List(3, 4)) shouldBe true List(1, 2, List(3, 4)) != List(1, 2, List(3, 5)) shouldBe true } // ------------------------------------------------- "be map/reduce" in { List(1,2,3,4,5,6,7,8,9,10,11,12) .filter(_ % 2 == 0) .map(_ + 1d) .reduce(_ + _) shouldBe 48d } } // =================================================== "Arrays" can { info("Arrays are collections as any others but with some issues...") info("There's no dedicated syntax") // ------------------------------------------------- "be created in a simple way" in { Array(1, 2, 3) } // ------------------------------------------------- "have some issues unfortunately with equalities" in { info("All collection are deeply compared, but arrays have issues") List(1, 2, 3) == List(1, 2, 3) shouldBe true intercept[Exception] { Array(1, 2, 3) == Array(1, 2, 3) shouldBe true } info("But with the triple equal, === it is correctly checked") List(1, 2, 3) === List(1, 2, 3) Array(1, 2, 3) === Array(1, 2, 3) shouldBe true Array(1, 2, Array(3, 4)) === Array(1, 2, Array(3, 4)) shouldBe true Array(1, 2, List(3, 4)) !== Array(1, 2, List(3, 5)) shouldBe true Array(1, 2, Array(3, 4)) !== Array(1, 2, Array(3, 5)) shouldBe true } } // =================================================== "Strings" can { // ------------------------------------------------- "provide useful utilities methods" in { "TRUC".toLowerCase shouldBe "truc" "truc".toUpperCase shouldBe "TRUC" "truc".capitalize shouldBe "Truc" @@ -56,7 +120,7 @@ object ScalaLanguageBasicsTest extends WordSpec with Matchers { "truc".startsWith("tr") shouldBe true "truc".equalsIgnoreCase("TRUC") shouldBe true "truc".matches(".*uc") shouldBe true "1 2 3".split(" ") shouldBe Array("1", "2", "3") } // ------------------------------------------------- "equals and == are the same" in { @@ -71,29 +135,107 @@ object ScalaLanguageBasicsTest extends WordSpec with Matchers { } // ------------------------------------------------- "support basic string interpolations" in { val age = 32 s"Sarah is $age years old" shouldBe "Sarah is 32 years old" s"Sarah is ${age + 1} years old" shouldBe "Sarah is 33 years old" } // ------------------------------------------------- "support basic printf like interpolations" in { val name = "joe" f"[$name%7s]" shouldBe "[ joe]" f"[$name%-7s]" shouldBe "[joe ]" } // ------------------------------------------------- "support basic printf like interpolations with locale changes" in { val price = 32.54351 f"The price is $price%.1f" shouldBe "The price is 032.5 dollar" } // ------------------------------------------------- "support basic raw strings" in { val x = 2 raw"x=$x\nsameline" shouldBe """x=2\nsameline""" raw"x=$x\nsameline" shouldBe "x=2\\nsameline" } } // =================================================== "Regexps" can { // ------------------------------------------------- "match strings" in { val reg = "(?i)john .*".r ("John Doe" match {case reg() => true}) shouldBe true reg.matches("John Doe") shouldBe true } // ------------------------------------------------- "extract sub string" in { val reg = """(?i)john\s+(.*)""".r ("John Doe" match {case reg(lastName) => lastName}) shouldBe "Doe" } // ------------------------------------------------- "extract sub strings" in { val reg = """(?i)john\s+(.*)""".r List("john Doe", "john chose") .collect{case reg(lastName)=>lastName} shouldBe List("Doe", "chose") } } // =================================================== } } object ScalaLanguageAsyncBasicsTest extends AsyncWordSpec with Matchers { override def suiteName: String = "Scala Language Asynchronous Basics" "Scala language" must { // =================================================== "Futures" can { "be created" in { Future{ 1 }.map{result => result shouldBe 1} } // ------------------------------------------------- "be badly composed" in { def f1() = Future{1} def f2() = Future{2} val futureResult = for { r1 <- f1() r2 <- f2() // will start only once f1 has finished } yield r1+r2 futureResult.map{_ shouldBe 3} } // ------------------------------------------------- "be better composed" in { val f1 = Future{1} val f2 = Future{2} val futureResult = for { r1 <- f1 r2 <- f2 } yield r1+r2 futureResult.map{_ shouldBe 3} } // ------------------------------------------------- "be reduced" in { val futures: Seq[Future[Int]] = Seq(Future{1}, Future{2}) val future: Future[Seq[Int]] = Future.sequence(futures) future.map(_.sum shouldBe 3) } // ------------------------------------------------- "transparent operation" in { Future{"A"} .andThen{case Success(value) => value+"B" } .map{_ shouldBe "A"} } // ------------------------------------------------- "recover from failure" in { Future{throw new Exception("BAD")} .recover{ th => "A" } .map{_ shouldBe "A"} } } // =================================================== } } ScalaLanguageBasicsTest.execute() ScalaLanguageAsyncBasicsTest.execute() -
dacr created this gist
Oct 29, 2019 .There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,99 @@ // summary : scala language // keywords : scala, learning, @testable // publish : gist, snippet // authors : David Crosson // id : 84200162-6102-4e5c-9ade-19296bc405f8 // execution : scala ammonite script (http://ammonite.io/) - run as follow 'amm scriptname.sc' import $ivy.`org.scalatest::scalatest:3.0.8` import org.scalatest._ object ScalaLanguageBasicsTest extends WordSpec with Matchers { override def suiteName: String = "Language Basics" "scala" must { // =================================================== "List" can { // ------------------------------------------------- "be empty" in { List().isEmpty shouldBe true } // ------------------------------------------------- "be initialized" in { List(1,2,3,4).size shouldBe 4 } // ------------------------------------------------- "provide a set of standard useful operations" in { val list = List(4, 1, 3, 1, 5, 2, 2 ) list.size shouldBe 7 list.distinct shouldBe List(4, 1, 3, 5, 2) list.sorted shouldBe List(1, 1, 2, 2, 3, 4, 5) list.head shouldBe 4 list.tail shouldBe List(1, 3, 1, 5, 2, 2 ) list.init shouldBe List(4, 1, 3, 1, 5, 2 ) list.take(2) shouldBe List(4, 1) list.drop(4) shouldBe List(5, 2, 2) list.reverse shouldBe List(2,2,5,1,3,1,4) } } // =================================================== "Arrays" can { info("Arrays are collections as any others") info("There's no dedicated syntax") // ------------------------------------------------- "be created in a simple way" in { Array(1,2,3) } } // =================================================== "Strings" can { // ------------------------------------------------- "provide usefull utilities methods" in { "TRUC".toLowerCase shouldBe "truc" "truc".toUpperCase shouldBe "TRUC" "truc".capitalize shouldBe "Truc" "truc".endsWith("uc") shouldBe true "truc".contains("ru") shouldBe true "truc".startsWith("tr") shouldBe true "truc".equalsIgnoreCase("TRUC") shouldBe true "truc".matches(".*uc") shouldBe true "1 2 3".split(" ") shouldBe Array("1","2","3") } // ------------------------------------------------- "equals and == are the same" in { ("truc" == new String("truc")) shouldBe true } // ------------------------------------------------- "be defined using multiple lines" in { info("With stripMargin method you can even keep the right code indentation") "line1\nline2" shouldBe """line1 |line2""".stripMargin } // ------------------------------------------------- "support basic string interpolations" in { val age=32 s"Sarah is $age years old" shouldBe "Sarah is 32 years old" s"Sarah is ${age+1} years old" shouldBe "Sarah is 33 years old" } // ------------------------------------------------- "support basic printf like interpolations" in { val name="joe" f"[$name%7s]" shouldBe "[ joe]" f"[$name%-7s]" shouldBe "[joe ]" } // ------------------------------------------------- "support basic printf like interpolations with locale changes" in { val price=32.54351 f"The price is $price%.1f" shouldBe "The price is 032.5 dollar" } // ------------------------------------------------- "support basic raw strings" in { val x=2 raw"x=$x\nsameline" shouldBe """x=2\nsameline""" raw"x=$x\nsameline" shouldBe "x=2\\nsameline" } } } } ScalaLanguageBasicsTest.execute()