SlideShare a Scribd company logo
Getting Functional with Scala
An Introduction to Functional Programming and the Scala Programming Language
September, 2016
2
Software Engineer at IBM Mobile Innovation Lab
@jorgelpaez19
Jorge Paez
3
My IBM Journey… So far
www-01.ibm.com/employment/us/extremeblue https://www.ibm.com/innovation/milab/
4
Where will yours start?
http://www-03.ibm.com/employment/entrylevel_campus.html
5
What is Functional Programming?
6
Function Not a Function
7
Core Ideas
• Data is immutable
• PURE function are our basic building block
• Use expressions over instructions
• First class functions
• Type-strictness
8
…but why?
9
Benefits!
• Simpler concurrency/parallelism
• Optimizations with caching or memoization
• Easier debugging
• Cleaner/less verbose code
• Encourages code re-use
• Promotes Test Driven Development
10
Pure vs Impure
// In Main.java
public String getFormal(String name) {

name = "Mr." + name;

return name;

}
// In Main.scala
def getFormal(name: String) = s"Mr. $name!"
11
Expressions vs Instructions
// In Main.java
public double[] convertToMeters(double[] measurements) {

double[] metricMeasurements = 

new double[measurements.length];



for(int i = 0; i < measurements.length; ++i) {

metricMeasurements[i] =

measurements[i] * CONVERSION_FACTOR;

}



return metricMeasurements;

}
// In Main.scala
def convertToMeters(measurements: Seq[Double]) =

measurements.map(n => n * CONVERSION_FACTOR)
12
First Class Functions
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}



def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))



def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))





def draw(func: Double => Seq[(Double, Double)], scale: Double)
13
First Class Functions
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}



def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))



def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))





def draw(func: Double => Seq[(Double, Double)], scale: Double)




def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))







14
First Class Functions
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}



def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))



def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))





def draw(func: Double => Seq[(Double, Double)], scale: Double)


def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))



15
First Class Functions
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}



def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))



def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))





def draw(func: Double => Seq[(Double, Double)], scale: Double)
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}





def draw(func: Double => Seq[(Double, Double)], scale: Double)
16
First Class Functions
def main(args: Array[String]): Unit = {

draw(getCurve(0, 90), 1)

draw(getCurve, 2)

}



def getCurve: Double => Seq[(Double, Double)] =

(r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))



def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =

(r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))



def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =

((r * Math.cos(theta)), (r * Math.sin(theta)))





def draw(func: Double => Seq[(Double, Double)], scale: Double)
17
Type Strictness
sealed trait Person



case class Student(name: String) extends Person



case class Alumni(name: String) extends Person



def getBusFare(person : Person): Int = {

person match {

case p: Student => 0

case p: Alumni => 2

}

}
18
Options
// Form asking for person's gender

// a) Female

// b) Male

// c) Don’t want to disclose



def main(args: Array[String]): Unit = {

val answers = Seq(Some("Female"), Some("Male"), Some(null), None)



answers.map(a => a match {

case Some(gender) =>

if(gender == null){

"the didn't want to disclose his or her gender"

} else {

s"The user's gender is $gender"

}

case None => "the user didn't pick a value on the form"

})

}
19
What’s next?
20
Keep Learning
• Slides: http://www.slideshare.net/JorgePaez15/getting-functional-with-scala
• Deploying a Scala server to Bluemix: https://www.ibm.com/innovation/milab/how-
to-run-a-scala-web-app-on-ibm-bluemix/
• Free class: https://www.coursera.org/learn/progfun1
Thank You

@jorgelpaez19
@IBM_MIL

More Related Content

PDF
Rubyconfindia2018 - GPU accelerated libraries for Ruby
DOCX
Application Form
KEY
RHadoop, R meets Hadoop
PDF
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
PDF
T3chFest 2016 - The polyglot programmer
PDF
Post-Free: Life After Free Monads
PPTX
Introduction to Monads in Scala (1)
PDF
Atomically { Delete Your Actors }
Rubyconfindia2018 - GPU accelerated libraries for Ruby
Application Form
RHadoop, R meets Hadoop
The Easy-Peasy-Lemon-Squeezy, Statically-Typed, Purely Functional Programming...
T3chFest 2016 - The polyglot programmer
Post-Free: Life After Free Monads
Introduction to Monads in Scala (1)
Atomically { Delete Your Actors }

What's hot (20)

PDF
GUL UC3M - Introduction to functional programming
PPT
Python 101 language features and functional programming
PDF
Data Structures in javaScript 2015
PDF
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
PDF
How to extend map? Or why we need collections redesign? - Scalar 2017
PPTX
Taking your side effects aside
PPT
Profiling and optimization
PDF
Beyond tf idf why, what & how
PDF
First-Class Patterns
PDF
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
PDF
The best language in the world
PDF
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
PDF
08. haskell Functions
PPTX
Introduction to Monads in Scala (2)
PDF
Principled Error Handling with FP
PDF
Scala. Introduction to FP. Monads
PDF
Python Performance 101
PDF
Делаем пользовательское Api на базе Shapeless
PDF
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
PDF
Time Series Analysis Sample Code
GUL UC3M - Introduction to functional programming
Python 101 language features and functional programming
Data Structures in javaScript 2015
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
How to extend map? Or why we need collections redesign? - Scalar 2017
Taking your side effects aside
Profiling and optimization
Beyond tf idf why, what & how
First-Class Patterns
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
The best language in the world
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
08. haskell Functions
Introduction to Monads in Scala (2)
Principled Error Handling with FP
Scala. Introduction to FP. Monads
Python Performance 101
Делаем пользовательское Api на базе Shapeless
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Time Series Analysis Sample Code
Ad

Viewers also liked (20)

PDF
Übersicht Glm Workshop 2009
PPT
Neural networks1
PDF
Maven c'est bien, SBT c'est mieux
PDF
Universitélang scala tools
PDF
Les monades Scala, Java 8
PPTX
Introduction to R programming
PDF
Université des langages scala
PDF
Scala Intro
PDF
Lagom, reactive framework
PPTX
Simulation presentation
PDF
Introduction à Scala - Michel Schinz - January 2010
PDF
Scala in Action - Heiko Seeburger
PDF
Paris stormusergroup intrudocution
ODP
Introduction to Spark with Scala
PDF
Hammurabi
PDF
Soutenance ysance
PDF
Scala - A Scalable Language
PDF
Scala at HUJI PL Seminar 2008
PDF
Mémoire de fin d'étude - La big data et les réseaux sociaux
PPTX
The Aviation Insurance Industry Presentation (1)
Übersicht Glm Workshop 2009
Neural networks1
Maven c'est bien, SBT c'est mieux
Universitélang scala tools
Les monades Scala, Java 8
Introduction to R programming
Université des langages scala
Scala Intro
Lagom, reactive framework
Simulation presentation
Introduction à Scala - Michel Schinz - January 2010
Scala in Action - Heiko Seeburger
Paris stormusergroup intrudocution
Introduction to Spark with Scala
Hammurabi
Soutenance ysance
Scala - A Scalable Language
Scala at HUJI PL Seminar 2008
Mémoire de fin d'étude - La big data et les réseaux sociaux
The Aviation Insurance Industry Presentation (1)
Ad

Similar to Getting Functional with Scala (20)

PDF
Refactoring to Macros with Clojure
PPTX
Fosdem2017 Scientific computing on Jruby
PDF
Scala in Places API
PDF
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
PDF
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
PDF
A Recovering Java Developer Learns to Go
PDF
Scala introduction
PDF
Rainer Grimm, “Functional Programming in C++11”
PPTX
PyData NYC 2019
PDF
Emerging Languages: A Tour of the Horizon
KEY
CoffeeScript - A Rubyist's Love Affair
PDF
TreSQL
KEY
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
PPTX
Angular2 for Beginners
PDF
Introduction to Scalding and Monoids
PDF
Writing DSL with Applicative Functors
PPTX
NetPonto - The Future Of C# - NetConf Edition
PDF
Rcpp: Seemless R and C++
PDF
Dive into PySpark
PPTX
High performance GPU computing with Ruby RubyConf 2017
Refactoring to Macros with Clojure
Fosdem2017 Scientific computing on Jruby
Scala in Places API
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
A Recovering Java Developer Learns to Go
Scala introduction
Rainer Grimm, “Functional Programming in C++11”
PyData NYC 2019
Emerging Languages: A Tour of the Horizon
CoffeeScript - A Rubyist's Love Affair
TreSQL
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Angular2 for Beginners
Introduction to Scalding and Monoids
Writing DSL with Applicative Functors
NetPonto - The Future Of C# - NetConf Edition
Rcpp: Seemless R and C++
Dive into PySpark
High performance GPU computing with Ruby RubyConf 2017

Recently uploaded (20)

PPTX
Computer Software and OS of computer science of grade 11.pptx
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PPTX
Operating system designcfffgfgggggggvggggggggg
PDF
Designing Intelligence for the Shop Floor.pdf
PDF
Odoo Companies in India – Driving Business Transformation.pdf
PDF
wealthsignaloriginal-com-DS-text-... (1).pdf
PDF
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
PPTX
Reimagine Home Health with the Power of Agentic AI​
PDF
top salesforce developer skills in 2025.pdf
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PPTX
assetexplorer- product-overview - presentation
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PDF
Digital Strategies for Manufacturing Companies
PDF
Internet Downloader Manager (IDM) Crack 6.42 Build 41
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
Wondershare Filmora 15 Crack With Activation Key [2025
Computer Software and OS of computer science of grade 11.pptx
CHAPTER 2 - PM Management and IT Context
Design an Analysis of Algorithms II-SECS-1021-03
Operating system designcfffgfgggggggvggggggggg
Designing Intelligence for the Shop Floor.pdf
Odoo Companies in India – Driving Business Transformation.pdf
wealthsignaloriginal-com-DS-text-... (1).pdf
Adobe Premiere Pro 2025 (v24.5.0.057) Crack free
Reimagine Home Health with the Power of Agentic AI​
top salesforce developer skills in 2025.pdf
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
2025 Textile ERP Trends: SAP, Odoo & Oracle
assetexplorer- product-overview - presentation
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Digital Strategies for Manufacturing Companies
Internet Downloader Manager (IDM) Crack 6.42 Build 41
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PTS Company Brochure 2025 (1).pdf.......
Navsoft: AI-Powered Business Solutions & Custom Software Development
Wondershare Filmora 15 Crack With Activation Key [2025

Getting Functional with Scala

  • 1. Getting Functional with Scala An Introduction to Functional Programming and the Scala Programming Language September, 2016
  • 2. 2 Software Engineer at IBM Mobile Innovation Lab @jorgelpaez19 Jorge Paez
  • 3. 3 My IBM Journey… So far www-01.ibm.com/employment/us/extremeblue https://www.ibm.com/innovation/milab/
  • 4. 4 Where will yours start? http://www-03.ibm.com/employment/entrylevel_campus.html
  • 5. 5 What is Functional Programming?
  • 6. 6 Function Not a Function
  • 7. 7 Core Ideas • Data is immutable • PURE function are our basic building block • Use expressions over instructions • First class functions • Type-strictness
  • 9. 9 Benefits! • Simpler concurrency/parallelism • Optimizations with caching or memoization • Easier debugging • Cleaner/less verbose code • Encourages code re-use • Promotes Test Driven Development
  • 10. 10 Pure vs Impure // In Main.java public String getFormal(String name) {
 name = "Mr." + name;
 return name;
 } // In Main.scala def getFormal(name: String) = s"Mr. $name!"
  • 11. 11 Expressions vs Instructions // In Main.java public double[] convertToMeters(double[] measurements) {
 double[] metricMeasurements = 
 new double[measurements.length];
 
 for(int i = 0; i < measurements.length; ++i) {
 metricMeasurements[i] =
 measurements[i] * CONVERSION_FACTOR;
 }
 
 return metricMeasurements;
 } // In Main.scala def convertToMeters(measurements: Seq[Double]) =
 measurements.map(n => n * CONVERSION_FACTOR)
  • 12. 12 First Class Functions def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double)
  • 13. 13 First Class Functions def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double) 
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 
 

  • 14. 14 First Class Functions def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double) 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 

  • 15. 15 First Class Functions def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double) def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double)
  • 16. 16 First Class Functions def main(args: Array[String]): Unit = {
 draw(getCurve(0, 90), 1)
 draw(getCurve, 2)
 }
 
 def getCurve: Double => Seq[(Double, Double)] =
 (r) => (0 to 360).map(theta => getCartesianCoordinates(r, theta))
 
 def getCurve(thetaO: Int, thetaF: Int): Double => Seq[(Double, Double)] =
 (r) => (thetaO to thetaF).map(theta => getCartesianCoordinates(r, theta))
 
 def getCartesianCoordinates(r: Double, theta: Double): (Double, Double) =
 ((r * Math.cos(theta)), (r * Math.sin(theta)))
 
 
 def draw(func: Double => Seq[(Double, Double)], scale: Double)
  • 17. 17 Type Strictness sealed trait Person
 
 case class Student(name: String) extends Person
 
 case class Alumni(name: String) extends Person
 
 def getBusFare(person : Person): Int = {
 person match {
 case p: Student => 0
 case p: Alumni => 2
 }
 }
  • 18. 18 Options // Form asking for person's gender
 // a) Female
 // b) Male
 // c) Don’t want to disclose
 
 def main(args: Array[String]): Unit = {
 val answers = Seq(Some("Female"), Some("Male"), Some(null), None)
 
 answers.map(a => a match {
 case Some(gender) =>
 if(gender == null){
 "the didn't want to disclose his or her gender"
 } else {
 s"The user's gender is $gender"
 }
 case None => "the user didn't pick a value on the form"
 })
 }
  • 20. 20 Keep Learning • Slides: http://www.slideshare.net/JorgePaez15/getting-functional-with-scala • Deploying a Scala server to Bluemix: https://www.ibm.com/innovation/milab/how- to-run-a-scala-web-app-on-ibm-bluemix/ • Free class: https://www.coursera.org/learn/progfun1