Skip to content

Instantly share code, notes, and snippets.

@tristanwietsma
Last active May 17, 2019 03:01
Show Gist options
  • Save tristanwietsma/385ec9242d9cbbac5fcf to your computer and use it in GitHub Desktop.
Save tristanwietsma/385ec9242d9cbbac5fcf to your computer and use it in GitHub Desktop.
Scala for the Impatient

Exercise 1

scala> 3.
%   +   >    >>>            isInstanceOf   toDouble   toLong     unary_+   |
&   -   >=   ^              toByte         toFloat    toShort    unary_-
*   /   >>   asInstanceOf   toChar         toInt      toString   unary_~

Exercise 2

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

Exercise 3

res variables are var.

Exercise 4

scala> "crazy" * 3
res2: String = crazycrazycrazy

From StringOps in the docs, "*" returns the string concatenated n times.

Exercise 5

10 max 2 returns the max of the two numbers (10). Defined in RichInt.

Exercise 6

scala> val two = BigInt("2")
two: scala.math.BigInt = 2

scala> two.pow(1024)
res2: scala.math.BigInt = 179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586298239947245938479716304835356329624224137216

Exercise 7

scala> import scala.util.Random
import scala.util.Random

scala> import BigInt._
import BigInt._

scala> probablePrime(100, Random)
res0: scala.math.BigInt = 752657953138039345657544035981

Exercise 8

scala> val num = probablePrime(100, Random)
num: scala.math.BigInt = 793354810523066938087955098157

scala> num.toString(36)
res1: String = 24x0gg5vc0a3svf3srot

Exercise 9

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

Exercise 10

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)

Exercise 1

scala> def signum(x: Int): Int = {
     | if (x > 0) 1
     | else if (x < 0) return -1
     | else 0
     | }
signum: (x: Int)Int

scala> signum(1)
res0: Int = 1

scala> signum(-1)
res1: Int = -1

scala> signum(0)
res2: Int = 0

Alternatively, with pattern matching:

scala> def signum2(x: Int): Int = {
     | x match {
     | case x if x > 0 => return 1
     | case x if x < 0 => return -1
     | case _ => return 0
     | }
     | }
signum2: (x: Int)Int

scala> signum2(10)
res3: Int = 1

scala> signum2(-10)
res4: Int = -1

scala> signum2(0)
res5: Int = 0

Equivalent, but cleaner:

scala> def signum3(x: Int): Int = {
     | return x match {
     | case x if x > 0 => 1
     | case x if x < 0 => -1
     | case _ => 0
     | }
     | }

Exercise 2

This is literally nothing.

scala> var x = {}
x: Unit = ()

Exercise 3

Dumb question. x is Unit.

Exercise 4

scala> for(i <- 1 to 10; j = 11 - i) println(j);
10
9
8
7
6
5
4
3
2
1

or this...

scala> for(i <- 10 to 1 by -1) println(i);
10
9
8
7
6
5
4
3
2
1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment