Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hiamitchaurasia/3c8215e42c86c976b6281943f96c6db9 to your computer and use it in GitHub Desktop.
Save hiamitchaurasia/3c8215e42c86c976b6281943f96c6db9 to your computer and use it in GitHub Desktop.
Instruction and Code For the Course First Steps in Scala

Installing Scala On Your Machine

Install Java 8

Follow this link. It takes you to Oracle's Java 8 Distribution Page.

Run the following command to verify if Java 8 is installed?

java -version

You should see an output that would look similar to 1.8.0_202

Install Scala on Mac Operating System

Follow this link. It takes you to the HomeBrew page.

Run the following command to verify that HomeBrew was installed correctly on Mac

brew --version

You should see an output similar to Homebrew 2.0.5

Run the following command to update HomeBrew

brew update

Run the following command to install Scala on Mac

brew install scala

Install Scala on other Operating Systems

Follow this link. It takes you to the Scala Official Download Page.


Running Scala in the Browser

Working with Scala REPL mode

scala> :help
scala> :help load
scala> :help save
scala> 10 * 2
scala> "You must believe me, " + " Master!"
scala> :history
scala> a * 100

Working with Fundamental Scala Types

scala> scala.Short.MinValue
res0: Short = -32768

scala> scala.Short.MaxValue
res1: Short = 32767
scala> 10
res5: Int = 10

scala> 1.234
res6: Double = 1.234

scala> 'A'
res7: Char = A

scala> true
res8: Boolean = true

scala> "Yay!"
res9: String = Yay!

scala> 15L
res10: Long = 15
scala> new Int(190)
<console>:12: error: class Int is abstract; cannot be instantiated
       new Int(190)
       ^
scala> new Boolean("false")
<console>:12: error: class Boolean is abstract; cannot be instantiated
       new Boolean("false")
       ^
scala> new String("hello")
res12: String = hello
scala> 10
res0: Int = 10

scala> 10.+(20)
res1: Int = 30

scala> 10 + 30
res2: Int = 40
scala> 10 max 20
res3: Int = 20

scala> 10 min 20
res4: Int = 10

Understanding Immutability in Scala

public static void incrementByOne(Integer number) {
	number = number + 1 // changing the same number object
}
scala> var num = 10
num: Int = 10

scala> num = 11
num: Int = 11

scala> num
res0: Int = 11
scala> val salary = 2000
salary: Int = 2000

scala> salary = 1000
<console>:12: error: reassignment to val
       salary = 1000
              ^

Working with Control Structures

if (true) {
 "Yeah"
} else {
 "Meh!"
}
if (true) "Yeah" else "Meh"
if (true) "I trust you!" else "I don't"
for (i <- 1 to 10) {
 print(i + ", ")
}
for (i <- 1 to 10) print(i + ", ")
scala> for (i <- 1 to 10) i = i + 1
<console>:12: error: reassignment to val
       for (i <- 1 to 10) i = i + 1
                            ^
scala> for (i <- 1 to 10) yield i
res4: scala.collection.immutable.IndexedSeq[Int] = Vector(1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
var num = 10;
while (num > 0) {
 print(num + ", ")
 num = num - 1
}

What is a Function

scala> :paste
// Entering paste mode (ctrl-D to finish)

def square(number: Int): Int = {
  number * number
}

// Exiting paste mode, now interpreting.

square: (number: Int)Int

scala> square(10)
res5: Int = 100

scala> square("meh")
<console>:13: error: type mismatch;
 found   : String("meh")
 required: Int
       square("meh")
              ^
scala> :paste
// Entering paste mode (ctrl-D to finish)

def multiply(x: Int, y: Int): Int = {
 x * y
}

// Exiting paste mode, now interpreting.

multiply: (x: Int, y: Int)Int

scala> multiply(2, 10)
res8: Int = 20

scala>
scala> def multiply(x: Int, y: Int): Int = x * y
multiply: (x: Int, y: Int)Int

scala> multiply(2, 10)
res0: Int = 20
scala> def multiply(x: Int, y: Int) = x * y
multiply: (x: Int, y: Int)Int

scala> multiply(2, 10)
res0: Int = 20

scala>

What is Referential Transparency?

scala> :paste
// Entering paste mode (ctrl-D to finish)

val inputs = Array(2000, -100, -200, -400)

def getTotalExpenseRefTr(amounts: Array[Int]) = {
  var total = 0
  for (i <- 0 until amounts.length) {
    if (amounts(i) < 0) total += amounts(i)
  }
  total
}

val value01 = getTotalExpenseRefTr(inputs)
val value02 = getTotalExpenseRefTr(inputs)
println("value01", value01)
println("value02", value02)
println(value01 == value02)


// Exiting paste mode, now interpreting.

(value01,-700)
(value02,-700)
true
inputs: Array[Int] = Array(2000, -100, -200, -400)
getTotalExpenseRefTr: (amounts: Array[Int])Int
value01: Int = -700
value02: Int = -700

scala>
scala> :paste
// Entering paste mode (ctrl-D to finish)

val inputs = Array(2000, -100, -200, -400)
var totalExpense = 0
def getTotalExpense(amounts: Array[Int]) = {
  for (i <- 0 until amounts.length) {
    if (amounts(i) < 0) totalExpense += amounts(i)
  }
  totalExpense
}

val value03 = getTotalExpense(inputs)
val value04 = getTotalExpense(inputs)
println("value03", value03)
println("value04", value04)
println(value03 == value04)

// Exiting paste mode, now interpreting.

(value03,-700)
(value04,-1400)
false
inputs: Array[Int] = Array(2000, -100, -200, -400)
totalExpense: Int = -1400
getTotalExpense: (amounts: Array[Int])Int
value03: Int = -700
value04: Int = -1400

What are other types of functions?

def square(x: Int): Int = x * x
def f1(num: Array[Int], f: Int => Int): IndexedSeq[Int] = ??? // ??? throws NotImplementedError
def f2(num: Array[Int]): Int => Int = ???
scala> :paste
// Entering paste mode (ctrl-D to finish)

def transform(nums: Array[Int], f: Int => Int): IndexedSeq[Int] = {
  for (i <- 0 until nums.size) yield f(nums(i))
}

def square(x: Int) = x * x
val nums = Array(1, 2, 3)
transform(nums, square)

// Exiting paste mode, now interpreting.

transform: (nums: Array[Int], f: Int => Int)IndexedSeq[Int]
square: (x: Int)Int
nums: Array[Int] = Array(1, 2, 3)
res0: IndexedSeq[Int] = Vector(1, 4, 9)

scala>
scala> :paste
// Entering paste mode (ctrl-D to finish)

def transform(nums: Array[Int], f: Int => Int): IndexedSeq[Int] = {
  for (i <- 0 until nums.size) yield f(nums(i))
}

val nums = Array(1, 2, 3)
transform(nums, (x: Int) => x * x)

// Exiting paste mode, now interpreting.

transform: (nums: Array[Int], f: Int => Int)IndexedSeq[Int]
nums: Array[Int] = Array(1, 2, 3)
res0: IndexedSeq[Int] = Vector(1, 4, 9)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment