In [1]:
println(s"""Details of exec env ==>
| ${util.Properties.versionMsg}
| ${util.Properties.javaVmName} ${util.Properties.javaVersion} ${util.Properties.javaVmVersion}"""
.stripMargin)
In [2]:
def signum(number: Int) = {
if (number > 0) 1
else if (number < 0) -1
else 0;
}
println(signum(-50), signum(0), signum(50))
In [3]:
for (n <- Range(10, -1, -1)) println(n)
In [4]:
def countdown(n: Int) {
for( i <- Range(n, -1, -1) )
println(i)
}
countdown(5);
In [5]:
var prod = 1l
for (c <- "Hello") {
prod *= c
println(c, prod)
}
In [6]:
"Hello".foldLeft(1L)((a, b) => a * b)
Out[6]:
In [7]:
def product(s: String) = {
s.foldLeft(1L)((a, b) => a * b)
}
println(product("Hello"))
In [8]:
def productRecursive(s: String):Long = {
if(s.length == 0) 1
else s(0) * productRecursive(s drop 1)
}
productRecursive("Hello")
Out[8]:
In [9]:
def powerOfXToN(x: BigInt, n: Int) : BigInt = {
if(n > 0 && n % 2 == 0) powerOfXToN(x, n / 2) * powerOfXToN(x, n / 2)
else if(n > 0) x * powerOfXToN(x, n - 1)
else if(n == 0) 1
else 1 / powerOfXToN(x, -n)
}
powerOfXToN(2, 100)
Out[9]:
java.time.LocalDate as date"$year-$month-$day". You need to define an implicit class with a date method, like this:implicit class DateInterpolator(val sc: StringContext) extends AnyVal {
def date(args: Any*): LocalDate = . . .
}
args(i) is the value of the ith expression. Convert each to a string and then to an integer, and pass them to the LocalDate.of method. If you already know some Scala, add error handling. Throw an exception if there aren’t three arguments, or if they aren’t integers, or if they aren’t separated by dashes. (You get the strings in between the expressions as sc.parts.)
In [10]:
println("TBC")