Introduction to Scala – Functional Data Processing



Introduction to Scala – Functional Data Processing

0 0


scala-presentation

Presentation about Scala

On Github fedeoasi / scala-presentation

Introduction to Scala

Functional Data Processing

Created by Federico Caimi / @fedeoasi Backstop Solutions Group

Outline

  • Why Scala?
  • Functional and Object Oriented Programming
  • (Some) Language Features
  • Live Code Example (CTA Data Processing)
  • Scala at Backstop

Why Scala?

  • JVM Language
  • Unifies Functional and Object Oriented Programming
  • Typesafe
  • Concise Code

Functional Programming

  • Programming paradigm where functions are first-class citizens
  • Minimize state changes and mutability
  • Minimize external dependencies (e.g., the result of a function only depends on its parameters)
  • Minimize side effects (external changes)

Code is suddenly easier to test, debug, and run in parallel!

Unifying FP with Object Oriented Programming

OOP is good at structuring programs

Interfaces, Classes, Encapsulation, Delegation, Singleton

FP is good at isolating state change

Immutability, Repeatability, Concurrency

Scala Combines the two:

  • Preserves all the OOP you can do in Java
  • Fully functional language

Syntax

val ten = 10 //ten is an immutable value of type Int

var number = 2 //number is mutable
number += 1 //number is now 3

def sum(a: Int, b: Int): Int = a + b

def sumThree(a: Int): Int = sum(a, 3)

val numbers = 1 to 4 //Range(1, 2, 3, 4)

val doubles = numbers.map(_ * 2) //Range(2, 4, 6, 8)

val squares = numbers.map(n => n * n) //Range(1, 4, 9, 16)

val summedThree = numbers.map(sumThree) //Range(4, 5, 6, 7)

Option

Option[A] is a container type and can have two values: Some[A] and None.

class User(val username: String, val email: Option[String])
val users = Seq(
  new User("user1", Some("user1@gmail.com")),
  new User("user2", None),
  new User("user3", Some("user3@gmail.com"))
)
def sendMessage(email: String, message: String) = println(s"Sending email to $message")
users.foreach { u =>
  u.email match {
    case Some(email) => sendMessage(email, "Welcome to MyApp")
    case None =>
  }
}

Your code will be safer as the compiler will force you to think about the None case.

Case Classes

case class Person(firstName: String, 
                  lastName: String,
		  birthDate: LocalDate)

With just a little typing, we get a lot of things:

  • A class with immutable fields
  • Ability to use Person in pattern matching expression
  • .equals() and .hashCode() that work
  • An apply method

Processing CTA Datasets

Goal: Have fun processing collections in Scala

Possible Side Effect: Learn something interesting about the CTA

Code on Github

Scala at Backstop

  • Used in a team of 3-4 developers
  • Rewrote part of our accounting engine
  • Made it 20x faster
  • Most of us learned Scala as we were working on the project

Want to Learn More?

Introduction to Scala Functional Data Processing Created by Federico Caimi / @fedeoasi Backstop Solutions Group