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

What's hot

GUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programmingGUL UC3M - Introduction to functional programming
GUL UC3M - Introduction to functional programming
David Muñoz Díaz
 
Python 101 language features and functional programming
Python 101 language features and functional programmingPython 101 language features and functional programming
Python 101 language features and functional programming
Lukasz Dynowski
 
Data Structures in javaScript 2015
Data Structures in javaScript 2015Data Structures in javaScript 2015
Data Structures in javaScript 2015
Nir Kaufman
 
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...
R + Hadoop = Big Data Analytics. How Revolution Analytics' RHadoop Project Al...Revolution Analytics
 
ITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function ProgrammingITT 2015 - Saul Mora - Object Oriented Function Programming
ITT 2015 - Saul Mora - Object Oriented Function Programming
Istanbul Tech Talks
 
How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017How to extend map? Or why we need collections redesign? - Scalar 2017
How to extend map? Or why we need collections redesign? - Scalar 2017
Szymon Matejczyk
 
Taking your side effects aside
Taking your side effects asideTaking your side effects aside
Taking your side effects aside
💡 Tomasz Kogut
 
Profiling and optimization
Profiling and optimizationProfiling and optimization
Profiling and optimizationg3_nittala
 
Beyond tf idf why, what & how
Beyond tf idf why, what & howBeyond tf idf why, what & how
Beyond tf idf why, what & how
lucenerevolution
 
First-Class Patterns
First-Class PatternsFirst-Class Patterns
First-Class Patterns
John De Goes
 
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Side by Side - Scala and Java Adaptations of Martin Fowler’s Javascript Refac...
Philip Schwarz
 
The best language in the world
The best language in the worldThe best language in the world
The best language in the world
David Muñoz Díaz
 
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional ProgrammingZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
ZIO Schedule: Conquering Flakiness & Recurrence with Pure Functional Programming
John De Goes
 
Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)Introduction to Monads in Scala (2)
Introduction to Monads in Scala (2)stasimus
 
Principled Error Handling with FP
Principled Error Handling with FPPrincipled Error Handling with FP
Principled Error Handling with FP
Luka Jacobowitz
 
Scala. Introduction to FP. Monads
Scala. Introduction to FP. MonadsScala. Introduction to FP. Monads
Scala. Introduction to FP. Monads
Kirill Kozlov
 
Python Performance 101
Python Performance 101Python Performance 101
Python Performance 101Ankur Gupta
 
Делаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе ShapelessДелаем пользовательское Api на базе Shapeless
Делаем пользовательское Api на базе Shapeless
Вадим Челышов
 
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
[Let'Swift 2019] 실용적인 함수형 프로그래밍 워크샵
Wanbok Choi
 

What's hot (20)

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

Viewers also liked

Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009
Mark Heckmann
 
Neural networks1
Neural networks1Neural networks1
Neural networks1
Mohan Raj
 
Maven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieuxMaven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieux
Fabrice Sznajderman
 
Universitélang scala tools
Universitélang scala toolsUniversitélang scala tools
Universitélang scala tools
Fabrice Sznajderman
 
Les monades Scala, Java 8
Les monades Scala, Java 8Les monades Scala, Java 8
Les monades Scala, Java 8
Fabrice Sznajderman
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
Victor Ordu
 
Université des langages scala
Université des langages   scalaUniversité des langages   scala
Université des langages scala
Fabrice Sznajderman
 
Scala Intro
Scala IntroScala Intro
Scala Intro
Paolo Platter
 
Lagom, reactive framework
Lagom, reactive frameworkLagom, reactive framework
Lagom, reactive framework
Fabrice Sznajderman
 
Simulation presentation
Simulation presentationSimulation presentation
Simulation presentation
Aseem Chakrabarthy
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
JUG Lausanne
 
Scala in Action - Heiko Seeburger
Scala in Action - Heiko SeeburgerScala in Action - Heiko Seeburger
Scala in Action - Heiko Seeburger
JAX London
 
Paris stormusergroup intrudocution
Paris stormusergroup intrudocutionParis stormusergroup intrudocution
Paris stormusergroup intrudocutionParis_Storm_UG
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
Mahmoud Shiri Varamini
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with Scala
Himanshu Gupta
 
Hammurabi
HammurabiHammurabi
Hammurabi
Mario Fusco
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable Language
Mario Gleichmann
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Yardena Meymann
 
Mémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociauxMémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociaux
Chloé Marty
 

Viewers also liked (20)

Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009Übersicht Glm Workshop 2009
Übersicht Glm Workshop 2009
 
Neural networks1
Neural networks1Neural networks1
Neural networks1
 
Maven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieuxMaven c'est bien, SBT c'est mieux
Maven c'est bien, SBT c'est mieux
 
Universitélang scala tools
Universitélang scala toolsUniversitélang scala tools
Universitélang scala tools
 
Les monades Scala, Java 8
Les monades Scala, Java 8Les monades Scala, Java 8
Les monades Scala, Java 8
 
Introduction to R programming
Introduction to R programmingIntroduction to R programming
Introduction to R programming
 
Université des langages scala
Université des langages   scalaUniversité des langages   scala
Université des langages scala
 
Scala Intro
Scala IntroScala Intro
Scala Intro
 
Lagom, reactive framework
Lagom, reactive frameworkLagom, reactive framework
Lagom, reactive framework
 
Simulation presentation
Simulation presentationSimulation presentation
Simulation presentation
 
Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010Introduction à Scala - Michel Schinz - January 2010
Introduction à Scala - Michel Schinz - January 2010
 
Scala in Action - Heiko Seeburger
Scala in Action - Heiko SeeburgerScala in Action - Heiko Seeburger
Scala in Action - Heiko Seeburger
 
Paris stormusergroup intrudocution
Paris stormusergroup intrudocutionParis stormusergroup intrudocution
Paris stormusergroup intrudocution
 
An Intoduction to R
An Intoduction to RAn Intoduction to R
An Intoduction to R
 
Introduction to Spark with Scala
Introduction to Spark with ScalaIntroduction to Spark with Scala
Introduction to Spark with Scala
 
Hammurabi
HammurabiHammurabi
Hammurabi
 
Soutenance ysance
Soutenance ysanceSoutenance ysance
Soutenance ysance
 
Scala - A Scalable Language
Scala - A Scalable LanguageScala - A Scalable Language
Scala - A Scalable Language
 
Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008Scala at HUJI PL Seminar 2008
Scala at HUJI PL Seminar 2008
 
Mémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociauxMémoire de fin d'étude - La big data et les réseaux sociaux
Mémoire de fin d'étude - La big data et les réseaux sociaux
 

Similar to Getting Functional with Scala

Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Fosdem2017 Scientific computing on Jruby
Fosdem2017  Scientific computing on JrubyFosdem2017  Scientific computing on Jruby
Fosdem2017 Scientific computing on Jruby
Prasun Anand
 
Scala in Places API
Scala in Places APIScala in Places API
Scala in Places API
Łukasz Bałamut
 
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...GECon2017_Cpp  a monster that no one likes but that will outlast them all _Ya...
GECon2017_Cpp a monster that no one likes but that will outlast them all _Ya...
GECon_Org Team
 
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them allGECon 2017: C++ - a Monster that no one likes but that will outlast them all
GECon 2017: C++ - a Monster that no one likes but that will outlast them all
Yauheni Akhotnikau
 
A Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to GoA Recovering Java Developer Learns to Go
A Recovering Java Developer Learns to Go
Matt Stine
 
Scala introduction
Scala introductionScala introduction
Scala introduction
vito jeng
 
Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”Rainer Grimm, “Functional Programming in C++11”
Rainer Grimm, “Functional Programming in C++11”
Platonov Sergey
 
PyData NYC 2019
PyData NYC 2019PyData NYC 2019
PyData NYC 2019
Li Jin
 
Emerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the HorizonEmerging Languages: A Tour of the Horizon
Emerging Languages: A Tour of the Horizon
Alex Payne
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
Mark
 
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDBScala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
Scala Days 2011 - Rogue: A Type-Safe DSL for MongoDB
jorgeortiz85
 
Angular2 for Beginners
Angular2 for BeginnersAngular2 for Beginners
Angular2 for Beginners
Oswald Campesato
 
Introduction to Scalding and Monoids
Introduction to Scalding and MonoidsIntroduction to Scalding and Monoids
Introduction to Scalding and Monoids
Hugo Gävert
 
Writing DSL with Applicative Functors
Writing DSL with Applicative FunctorsWriting DSL with Applicative Functors
Writing DSL with Applicative Functors
David Galichet
 
NetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf EditionNetPonto - The Future Of C# - NetConf Edition
NetPonto - The Future Of C# - NetConf Edition
Paulo Morgado
 
Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Rcpp: Seemless R and C++
Rcpp: Seemless R and C++Romain Francois
 
Dive into PySpark
Dive into PySparkDive into PySpark
Dive into PySpark
Mateusz Buśkiewicz
 
High performance GPU computing with Ruby RubyConf 2017
High performance GPU computing with Ruby  RubyConf 2017High performance GPU computing with Ruby  RubyConf 2017
High performance GPU computing with Ruby RubyConf 2017
Prasun Anand
 

Similar to Getting Functional with Scala (20)

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

Recently uploaded

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
rickgrimesss22
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
Georgi Kodinov
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
Juraj Vysvader
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
Cyanic lab
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
Tendenci - The Open Source AMS (Association Management Software)
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
Globus
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Anthony Dahanne
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Globus
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
NYGGS Automation Suite
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
Donna Lenk
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
vrstrong314
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
AMB-Review
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
IES VE
 

Recently uploaded (20)

AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptxTop Features to Include in Your Winzo Clone App for Business Growth (4).pptx
Top Features to Include in Your Winzo Clone App for Business Growth (4).pptx
 
2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx2024 RoOUG Security model for the cloud.pptx
2024 RoOUG Security model for the cloud.pptx
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Corporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMSCorporate Management | Session 3 of 3 | Tendenci AMS
Corporate Management | Session 3 of 3 | Tendenci AMS
 
Understanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSageUnderstanding Globus Data Transfers with NetSage
Understanding Globus Data Transfers with NetSage
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Prosigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology SolutionsProsigns: Transforming Business with Tailored Technology Solutions
Prosigns: Transforming Business with Tailored Technology Solutions
 
Vitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume MontevideoVitthal Shirke Microservices Resume Montevideo
Vitthal Shirke Microservices Resume Montevideo
 
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Enterprise Resource Planning System in Telangana
Enterprise Resource Planning System in TelanganaEnterprise Resource Planning System in Telangana
Enterprise Resource Planning System in Telangana
 
Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"Navigating the Metaverse: A Journey into Virtual Evolution"
Navigating the Metaverse: A Journey into Virtual Evolution"
 
top nidhi software solution freedownload
top nidhi software solution freedownloadtop nidhi software solution freedownload
top nidhi software solution freedownload
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdfDominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
Dominate Social Media with TubeTrivia AI’s Addictive Quiz Videos.pdf
 
Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 

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