Skip to content

Instantly share code, notes, and snippets.

@dacr
Last active May 27, 2023 06:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dacr/42ccc0948f84c9bb1e864993c739ee21 to your computer and use it in GitHub Desktop.
Save dacr/42ccc0948f84c9bb1e864993c739ee21 to your computer and use it in GitHub Desktop.
scala language / published by https://github.com/dacr/code-examples-manager #84200162-6102-4e5c-9ade-19296bc405f8/e0006e4b4e8bfb9005437f4fd6c9ba9667cfe8a3
// summary : scala language
// 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.3.0"
//> using dep "org.scalatest::scalatest:3.2.16"
//> using objectWrapper
// ---------------------
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 {
// ===================================================
"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.head shouldBe 4
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)
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) 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
}
}
// ===================================================
"Strings" can {
// -------------------------------------------------
"provide useful 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")
val res =
"""line1
|line2""".stripMargin
"line1\nline2" should be equals res
"line1\nline2" shouldEqual res
String("line1\nline2") should be(res)
}
// -------------------------------------------------
"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 {
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 {
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")
}
}
// ===================================================
"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)
}
}
}
}
org.scalatest.tools.Runner.main(Array("-oDF", "-s", classOf[ScalaLanguageBasicsTest].getName))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment