foreach and the functional style

Introduction to Scala

David Venturi

Curriculum Manager, DataCamp

Scala is functional

Scala is functional

  1. Functions are first-class values
  2. Operations of a program should map input values to output values rather than change data in place
Introduction to Scala

Scala is a imperative/functional hybrid

Scala usually is functional but can also be imperative sometimes

Scala is functional:

  1. Functions are first-class values
  2. Operations of a program should map input values to output values rather than change data in place
Introduction to Scala

Scala nudges us towards the functional style

Polar bear nudging a panda bear

The word "functional"

Introduction to Scala

"imperative" according to Oxford Dictionary

Imperative (English):

  • Definition: Of the nature of or expressing a command.
1 https://www.thefreedictionary.com/imperative
Introduction to Scala

The imperative style

Scala usually is functional but can also be imperative sometimes

Scala can be imperative:

  • One command at a time
  • Iterate with loops
  • Mutate shared state (e.g., mutating variables out of scope)
  • Examples: C, Java, Python
Introduction to Scala

The imperative style

// Define counter variable
var i = 0

// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// Loop through hands and see if each busts
while (i < hands.length) {
  println(bust(hands(i)))
  i = i + 1
}
Introduction to Scala

The imperative style

// Define counter variable
var i = 0

// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// Loop through hands and see if each busts
while (i < hands.length) {
  println(bust(hands(i)))
  i = i + 1
}
1 http://bit.ly/state_wikipedia
Introduction to Scala

The functional style

Scala usually is functional but can also be imperative sometimes

Scala is functional:

  1. Functions are first-class values
Introduction to Scala

The functional style

  • Functions are first-class values
// Define counter variable
var i = 0

// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// Loop through hands and see if each busts
while(i < hands.length) {
  println(bust(hands(i)))
  i += 1
}
Introduction to Scala

The functional style

  • Functions are first-class values
// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// See if each hand busts
hands.foreach(INSERT FUNCTION HERE)
Introduction to Scala

Scala fuses OOP and FP -> Scala is scalable

  • Functions are first-class values
// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// See if each hand busts
hands.foreach(INSERT FUNCTION HERE)

From Chapter 1:

Scala combines object-oriented and functional programming

Introduction to Scala

Modify the bust function

// Define a function to determine if hand busts
def bust(hand: Int) = {
  println(hand > 21)
}
Introduction to Scala

The functional style

  • Functions are first-class values
// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// See if each hand busts
hands.foreach(INSERT FUNCTION HERE)
Introduction to Scala

The functional style

  • Functions are first-class values
// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// See if each hand busts
hands.foreach(bust)
false
true
false
Introduction to Scala

What is a side effect?

Scala usually is functional but can also be imperative sometimes

Scala is functional:

  1. Functions are first-class values
  2. Operations of a program should map input values to output values rather than change data in place

Side effect: code that modifies some variable outside of its local scope

1 http://bit.ly/side_effect_wikipedia
Introduction to Scala

What is a side effect?

def bust(hand: Int) = {
  println(hand > 21)
}

scala> val myHand = bust(22)
true
myHand: Unit = ()
Introduction to Scala

What is a side effect?

// Define counter variable
var i = 0

// Initialize array with each player's hand var hands = Array(17, 24, 21)
// Loop through hands and see if each busts while (i < hands.length) { println(bust(hands(i))) i = i + 1 }
Introduction to Scala

The spectrum of functional style

Less functional than before

// Define a function to determine if hand busts
def bust(hand: Int) = {
  println(hand > 21)
}

More functional than before

// Initialize array with each player's hand
var hands = Array(17, 24, 21)

// See if each hand busts
hands.foreach(bust)
1 http://bit.ly/array_foreach_documentation
Introduction to Scala

Signs of style

Imperative
  • var
  • Side effects
  • Unit
Functional
  • val
  • No side effects
  • Non-Unit value types
    • Int
    • Boolean
    • Double
1 http://bit.ly/scala_unit_documentation
Introduction to Scala

Let's practice!

Introduction to Scala

Preparing Video For Download...