Beruflich Dokumente
Kultur Dokumente
/*
You can execute commands directly in the terminal
REPL : Read Evaluate Print Loop
Type scala in terminal to start and :q to quit
// You only use semicolons in Scala if you have multiple statements per line
// Create a BigInt
val lgPrime =
BigInt("622288097498926496141095869268883999563096063592498055290461")
lgPrime + 1
Create a BigDecimal
val pi50 = BigDecimal("3.14159265358979323846264338327950288419716939937510")
0.00000000000000000000000000000000000000000000000001 + pi50
randInt -= 1
"randInt -= 1" + randInt
randInt *= 1
"randInt *= 1" + randInt
randInt /= 1
"randInt /= 1" + randInt
abs(-8)
cbrt(64) // Cube root a^3 = n (Find a)
ceil(5.45)
round(5.45)
floor(5.45)
exp(1) // Euler's number raised to the power
pow(2, 2) // 2^2
sqrt(pow(2,2) + pow(2,2))
hypot(2, 2) // sqrt(X^2 + y^2)
log10(1000) // = 3 (101010 = 10^3)
log(2.7182818284590455) // Natural logarithm to the base e
min(5, 10)
max(5, 10)
(random * (11 - 1) + 1).toInt // Random number between 1 and 10
toRadians(90)
toDegrees(1.5707963267948966)
// acos, asin, atan, atan2, cos, cosh, sin, sinh, tan, tanh
var age = 18
val canVote = if (age >= 18) "yes" else "no"
true || false
!(true)
*/
object ScalaTut {
def main(args: Array[String]) {
var i = 0
do {
println(i)
i += 1
} while(i <= 20)
println("Even List")
for(i <- evenList)
println(i)
printPrimes
var numberGuess = 0
do{
print("Guess a number : ")
} while(numberGuess != 15)
// Concatenate strings
println(randSent.concat(" and explode"))
sayHi
// Create an ArrayBuffer
val friends2 = ArrayBuffer[String]()
// Use yield to multiply all values times 2 and store in a new array
val favNumsTimes2 = for(num <- favNums) yield 2 * num
favNumsDiv4.foreach(println)
// Add an item
customers(102) = "Megan Swift"
// Output a Map
for((k,v) <- customers)
printf("%d : %s\n", k, v)
println(whiskers.toString)
spike.setName("Spike")
println(spike.toString)
println(log10Func(1000))
// Any code that follows the class name is executed each time an
// object is created as part of the Primary Constructor
// The companion object for a class is where you'd define static class
// variables and functions in Java
object Animal {
private var idNumber = 0
private def newIdNum = { idNumber += 1; idNumber }
}
}
class Wolf(name : String) extends Mammal(name){
// You don't use override when defining abstract fields
var moveSpeed = 35.0;
trait Bulletproof {
def hitByBullet : String
// The first trait starts with extends and then with for each other
class Superhero(val name : String) extends Flyable with Bulletproof{
def fly = "%s flys through the air".format(this.name)