scala> 3.
% + > >>> isInstanceOf toDouble toLong unary_+ |
& - >= ^ toByte toFloat toShort unary_-
* / >> asInstanceOf toChar toInt toString unary_~
scala> import math._
import math._
scala> var root3 = math.sqrt(3)
root3: Double = 1.7320508075688772
scala> math.pow(root3, 2)
res1: Double = 2.9999999999999996
scala> 3 - res1
res2: Double = 4.440892098500626E-16
res
variables are var
.
scala> "crazy" * 3
res2: String = crazycrazycrazy
From StringOps in the docs, "*" returns the string concatenated n
times.
10 max 2
returns the max of the two numbers (10). Defined in RichInt.
scala> val two = BigInt("2")
two: scala.math.BigInt = 2
scala> two.pow(1024)
res2: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216
scala> import scala.util.Random
import scala.util.Random
scala> import BigInt._
import BigInt._
scala> probablePrime(100, Random)
res0: scala.math.BigInt = 752657953138039345657544035981
scala> val num = probablePrime(100, Random)
num: scala.math.BigInt = 793354810523066938087955098157
scala> num.toString(36)
res1: String = 24x0gg5vc0a3svf3srot
scala> val x = "Me"
x: String = Me
scala> x(0)
res2: Char = M
scala> x.length
res3: Int = 2
scala> x(x.length-1)
res4: Char = e
Or, use the builtins:
scala> x.tail
res8: String = e
scala> x.head
res9: Char = M
take n
returns the first n
chars; drop n
returns the chars remaining after n
chars. +Right
does the same operation from the opposite end of the array.
scala> val test = "some word"
test: String = some word
scala> test take 3
res14: String = som
scala> test drop 3
res15: String = e word
scala> test takeRight 3
res16: String = ord
scala> test dropRight 3
res17: String = some w
Also works on other collections:
scala> 1 to 10 take 3
res19: scala.collection.immutable.Range = Range(1, 2, 3)
scala> 1 to 10 drop 3
res20: scala.collection.immutable.Range = Range(4, 5, 6, 7, 8, 9, 10)