SlideShare a Scribd company logo
Functional Programming
in Scala
in a Nutshell
1
Why Scala?
2
Java is too Verbose
// construct a empty list
List<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
// introduce a counting index, i
for (int i = 0; i<list.size(); i++) {
Integer element = list.get(i);
System.out.println(element);
}
3
4
Scala is Concise
(1 :: 2 :: 3 :: Nil).foreach(println)
5
Railway Programming
6
... in Elegant Functional Programming Way
val openerO = Some(Opener)
val wineO = Some(Wine(vintage = 1997))
val contentsO =
for {
opener ← openerO
wine ← wineO
} yield opener.open(wine)
// contentsO: Option[Contents] = Some(contentsOfWine)
// no null, no NullPointerException
7
... in Elegant Functional Programming Way
val openerO = Some(Opener)
val wineO = None
val contentsO =
for {
opener ← openerO
wine ← wineO
} yield opener.open(wine)
// contentsO: Option[Contents] = None
// no null, no NullPointerException
8
Scala supports OOP, too
// OOP polymorphism
val newOpener: Opener = new NormalOpener()
val oldOpener: Opener = new BrokenOpener()
val wine = new Wine()
println(newOpener.open(wine)) // contentsOfWine
println(oldOpener.open(wine)) // Exception occurs!
9
Scala is Compatible with Java
Compatible with Hadoop and Spark
→ Official language for the bigdata community
// joda time based on java
import org.joda.time.DateTime, java.util.Date
val jodaDt: DateTime = new DateTime(new Date())
val month = month = dt.getMonthOfYear()
10
… and Vice Versa
// scala code
object Person {
val MALE = "m";
}
// java code
public class App {
public static void main(String argv[]) {
Person$ person = Person$.MODULE$;
System.out.println(person.MALE());
}
}
11
What is Functional
Programming?
12
It’s NOT map and reduce
&
It’s NOT lambda functions
13
By Definition,
A function is called pure if all its inputs are declared as
inputs - none of them are hidden - and likewise all its
outputs are declared as outputs. 1
— Kris Jenkins
1
It is not a concrete definition but easy and intuitive.
14
Immutability
// value, not variable
val a = 1
a = 2 // error: reassignment to val
// list
val ints1: List[Int] = 1 :: 2 :: Nil
val ints2: List[Int] = ints1 :+ 3
println(ints1) // List(1, 2)
println(ints2) // List(1, 2, 3)
println(ints2 == 1 :: 2 :: 3 :: Nil) // true
15
Immutability
def fibonacci(n : Int): Int = {
// while loop requires temporary varables
var (a, b, i) = (0, 1, 0)
while( i < n ) {
val c = a + b
a = b
b = c
i = i + 1
}
return a
}
16
Immutability
// recursion doesn't requires temporary varables
def fibonacci(n : Int): Int = n match {
case 0 | 1 => n
case _ => fibonacci(n - 1) + fibonacci(n - 2)
}
17
Side Effect, by Definition
A function is said to have a side effect if it modifies
some state outside its scope or has an observable
interaction with its calling functions or the outside
world.
— Side effect (computer science), Wikipedia
18
Side Effect
// this def has a side effect
def currentProgram(guide: TVGuide, channel: Int): Program = {
val schedule = guide.getSchedule(channel)
schedule.programAt(new Date())
}
19
Side Effect
// now it has no more side effects
// and it is immutable
def program(guide: TVGuide, channel: Int, date: Date): Program = {
val schedule = guide.getSchedule(channel)
schedule.programAt(date)
}
20
Let's Start at the Beginning Again
A Program is functional iff it has no side effects.2
2
cf. Definition using Referential Transparency
21
Why Functional Programming?
4
!
Type declaration exposes its action
4
"
Short code & Fewer bugs
4
#
Extremely easy to write unit test
4
$
Easy to maintain
22
When should you use Functional
Programming?
4
!
If you want to develop a product that you need to
manage in the long run
4
"
If you want to write an elegant code
4
#
If you want to build up your project quickly
23
When shoud not you use Functional
Programming?
4
!
If you want to write a script to use once
4
⏳
If your app requires nano-fast performance
4
#
If you do not know about functional programming,
and you need to develop your app in two weeks
4
$
If you want to live by maintaining a program for
whole lifetime
24
For Android Developers
25
For Javascript Developers
26
Further Readings
27
Grammars
4 스칼라 학교!
4 스칼라 공식 도큐먼트 입문
4 Effective Scala by Twitter
4 Effective Scala by Heejong Lee
4 Hacker Rank for Tutorials
4 Exercism
28
What is Functional Programming?
4 함수형 프로그래밍이란 무엇인가?
4 어떤 프로그래밍 언어들이 함수형인가?
29
Textbooks & References
4
!
스칼라로 배우는 함수형 프로그래밍
4 Functional Programming Principles in Scala on
Coursera
4 Big Data Analysis with Scala and Spark on Coursera
4 Scala Excercises
30
Popular Scala Libraries
4
!
Scalaz: Scala library for functional programming
4 Cats: Lightweight, modular, and extensible library
for functional programming
4 Monix: Asynchronous Programming for Scala
4 Circe: A JSON library for Scala powered by Cats
31
Haskell
4 Eta Programming Language, Haskell on the JVM
4 (가장 쉬운) 하스켈 책 : 느긋하지만 우아하고 세련된 함수형 언어
32
Resources
4 Scastie, an interactive playground for Scala
33

More Related Content

What's hot (17)

What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functions
sparkfabrik
 
C# p3
C# p3C# p3
C# p3
Renas Rekany
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Self employed
 
Storage classes
Storage classesStorage classes
Storage classes
Leela Koneru
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
jeffz
 
Jfokus functional groovy
Jfokus functional groovyJfokus functional groovy
Jfokus functional groovy
Andres Almiray
 
Storage class in C Language
Storage class in C LanguageStorage class in C Language
Storage class in C Language
Nitesh Kumar Pandey
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
tanmaymodi4
 
LISP:Loops In Lisp
LISP:Loops In LispLISP:Loops In Lisp
LISP:Loops In Lisp
DataminingTools Inc
 
Storage classes in C
Storage classes in CStorage classes in C
Storage classes in C
Nitesh Bichwani
 
4front en
4front en4front en
4front en
Vitaly Hornik
 
storage class
storage classstorage class
storage class
student
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 
Lecture 7 Templates, Friend Classes
Lecture 7 Templates, Friend ClassesLecture 7 Templates, Friend Classes
Lecture 7 Templates, Friend Classes
bunnykhan
 
Storage classes
Storage classesStorage classes
Storage classes
Puneet Rajput
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
jinaldesailive
 
What is storage class
What is storage classWhat is storage class
What is storage class
Isha Aggarwal
 
Immutability and pure functions
Immutability and pure functionsImmutability and pure functions
Immutability and pure functions
sparkfabrik
 
Storage classes in C
Storage classes in C Storage classes in C
Storage classes in C
Self employed
 
使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架使用.NET构建轻量级分布式框架
使用.NET构建轻量级分布式框架
jeffz
 
Jfokus functional groovy
Jfokus functional groovyJfokus functional groovy
Jfokus functional groovy
Andres Almiray
 
Storage classes in c language
Storage classes in c languageStorage classes in c language
Storage classes in c language
tanmaymodi4
 
storage class
storage classstorage class
storage class
student
 
11 lec 11 storage class
11 lec 11 storage class11 lec 11 storage class
11 lec 11 storage class
kapil078
 
Lecture 7 Templates, Friend Classes
Lecture 7 Templates, Friend ClassesLecture 7 Templates, Friend Classes
Lecture 7 Templates, Friend Classes
bunnykhan
 
OOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NETOOPS With CSharp - Jinal Desai .NET
OOPS With CSharp - Jinal Desai .NET
jinaldesailive
 

Similar to Functional Programming in Scala in a Nutshell (20)

Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Namuk Park
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#
Rainer Stropek
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
Geison Goes
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Igalia
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
Nobody asks "How is JavaScript?"
Nobody asks         "How is JavaScript?"Nobody asks         "How is JavaScript?"
Nobody asks "How is JavaScript?"
Igalia
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
Roberto Casadei
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Remix Your Language Tooling (JSConf.eu 2012)
Remix Your Language Tooling (JSConf.eu 2012)Remix Your Language Tooling (JSConf.eu 2012)
Remix Your Language Tooling (JSConf.eu 2012)
lennartkats
 
Scala ntnu
Scala ntnuScala ntnu
Scala ntnu
Alf Kristian Støyle
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014
Raimon Ràfols
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
PVS-Studio
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Functional Programming in Scala in a Nutshell: Review of Functional Programmi...
Namuk Park
 
The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)The Evolution of Async-Programming (SD 2.0, JavaScript)
The Evolution of Async-Programming (SD 2.0, JavaScript)
jeffz
 
Parallel and Async Programming With C#
Parallel and Async Programming With C#Parallel and Async Programming With C#
Parallel and Async Programming With C#
Rainer Stropek
 
Exciting JavaScript - Part I
Exciting JavaScript - Part IExciting JavaScript - Part I
Exciting JavaScript - Part I
Eugene Lazutkin
 
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)The Evolution of Async-Programming on .NET Platform (.Net China, C#)
The Evolution of Async-Programming on .NET Platform (.Net China, C#)
jeffz
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
Geison Goes
 
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Good news, everybody! Guile 2.2 performance notes (FOSDEM 2016)
Igalia
 
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Build 2016 - B880 - Top 6 Reasons to Move Your C++ Code to Visual Studio 2015
Windows Developer
 
04a intro while
04a intro while04a intro while
04a intro while
hasfaa1017
 
Nobody asks "How is JavaScript?"
Nobody asks         "How is JavaScript?"Nobody asks         "How is JavaScript?"
Nobody asks "How is JavaScript?"
Igalia
 
1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers1183 c-interview-questions-and-answers
1183 c-interview-questions-and-answers
Akash Gawali
 
Functional Programming in Scala: Notes
Functional Programming in Scala: NotesFunctional Programming in Scala: Notes
Functional Programming in Scala: Notes
Roberto Casadei
 
Functional Programming With Scala
Functional Programming With ScalaFunctional Programming With Scala
Functional Programming With Scala
Knoldus Inc.
 
Remix Your Language Tooling (JSConf.eu 2012)
Remix Your Language Tooling (JSConf.eu 2012)Remix Your Language Tooling (JSConf.eu 2012)
Remix Your Language Tooling (JSConf.eu 2012)
lennartkats
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
Nico Ludwig
 
Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014Improving Android Performance at Droidcon UK 2014
Improving Android Performance at Droidcon UK 2014
Raimon Ràfols
 
Functional programming with Scala
Functional programming with ScalaFunctional programming with Scala
Functional programming with Scala
Neelkanth Sachdeva
 
Logical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by ProfessionalsLogical Expressions in C/C++. Mistakes Made by Professionals
Logical Expressions in C/C++. Mistakes Made by Professionals
PVS-Studio
 
Lecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptxLecture 1_Functions in C.pptx
Lecture 1_Functions in C.pptx
KhurramKhan173
 
Ad

Recently uploaded (20)

Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Introduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUEIntroduction to Typescript - GDG On Campus EUE
Introduction to Typescript - GDG On Campus EUE
Google Developer Group On Campus European Universities in Egypt
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Trends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary MeekerTrends Artificial Intelligence - Mary Meeker
Trends Artificial Intelligence - Mary Meeker
Clive Dickens
 
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...Your startup on AWS - How to architect and maintain a Lean and Mean account J...
Your startup on AWS - How to architect and maintain a Lean and Mean account J...
angelo60207
 
Jeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software DeveloperJeremy Millul - A Talented Software Developer
Jeremy Millul - A Talented Software Developer
Jeremy Millul
 
Improving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevExImproving Developer Productivity With DORA, SPACE, and DevEx
Improving Developer Productivity With DORA, SPACE, and DevEx
Justin Reock
 
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to KnowWhat is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
What is Oracle EPM A Guide to Oracle EPM Cloud Everything You Need to Know
SMACT Works
 
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2Agentic AI: Beyond the Buzz- LangGraph Studio V2
Agentic AI: Beyond the Buzz- LangGraph Studio V2
Shashikant Jagtap
 
If You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FMEIf You Use Databricks, You Definitely Need FME
If You Use Databricks, You Definitely Need FME
Safe Software
 
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
“How Qualcomm Is Powering AI-driven Multimedia at the Edge,” a Presentation f...
Edge AI and Vision Alliance
 
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Creating an Accessible Future-How AI-powered Accessibility Testing is Shaping...
Impelsys Inc.
 
Oracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI ProfessionalOracle Cloud Infrastructure Generative AI Professional
Oracle Cloud Infrastructure Generative AI Professional
VICTOR MAESTRE RAMIREZ
 
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | BluebashMCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
MCP vs A2A vs ACP: Choosing the Right Protocol | Bluebash
Bluebash
 
Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025Mastering AI Workflows with FME - Peak of Data & AI 2025
Mastering AI Workflows with FME - Peak of Data & AI 2025
Safe Software
 
7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf7 Salesforce Data Cloud Best Practices.pdf
7 Salesforce Data Cloud Best Practices.pdf
Minuscule Technologies
 
Dancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptxDancing with AI - A Developer's Journey.pptx
Dancing with AI - A Developer's Journey.pptx
Elliott Richmond
 
Your startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean accountYour startup on AWS - How to architect and maintain a Lean and Mean account
Your startup on AWS - How to architect and maintain a Lean and Mean account
angelo60207
 
How to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptxHow to Detect Outliers in IBM SPSS Statistics.pptx
How to Detect Outliers in IBM SPSS Statistics.pptx
Version 1 Analytics
 
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
“State-space Models vs. Transformers for Ultra-low-power Edge AI,” a Presenta...
Edge AI and Vision Alliance
 
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyesEnd-to-end Assurance for SD-WAN & SASE with ThousandEyes
End-to-end Assurance for SD-WAN & SASE with ThousandEyes
ThousandEyes
 
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025Azure vs AWS  Which Cloud Platform Is Best for Your Business in 2025
Azure vs AWS Which Cloud Platform Is Best for Your Business in 2025
Infrassist Technologies Pvt. Ltd.
 
Ad

Functional Programming in Scala in a Nutshell

  • 3. Java is too Verbose // construct a empty list List<Integer> list = new ArrayList<Integer>(); list.add(1); list.add(2); list.add(3); // introduce a counting index, i for (int i = 0; i<list.size(); i++) { Integer element = list.get(i); System.out.println(element); } 3
  • 4. 4
  • 5. Scala is Concise (1 :: 2 :: 3 :: Nil).foreach(println) 5
  • 7. ... in Elegant Functional Programming Way val openerO = Some(Opener) val wineO = Some(Wine(vintage = 1997)) val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = Some(contentsOfWine) // no null, no NullPointerException 7
  • 8. ... in Elegant Functional Programming Way val openerO = Some(Opener) val wineO = None val contentsO = for { opener ← openerO wine ← wineO } yield opener.open(wine) // contentsO: Option[Contents] = None // no null, no NullPointerException 8
  • 9. Scala supports OOP, too // OOP polymorphism val newOpener: Opener = new NormalOpener() val oldOpener: Opener = new BrokenOpener() val wine = new Wine() println(newOpener.open(wine)) // contentsOfWine println(oldOpener.open(wine)) // Exception occurs! 9
  • 10. Scala is Compatible with Java Compatible with Hadoop and Spark → Official language for the bigdata community // joda time based on java import org.joda.time.DateTime, java.util.Date val jodaDt: DateTime = new DateTime(new Date()) val month = month = dt.getMonthOfYear() 10
  • 11. … and Vice Versa // scala code object Person { val MALE = "m"; } // java code public class App { public static void main(String argv[]) { Person$ person = Person$.MODULE$; System.out.println(person.MALE()); } } 11
  • 13. It’s NOT map and reduce & It’s NOT lambda functions 13
  • 14. By Definition, A function is called pure if all its inputs are declared as inputs - none of them are hidden - and likewise all its outputs are declared as outputs. 1 — Kris Jenkins 1 It is not a concrete definition but easy and intuitive. 14
  • 15. Immutability // value, not variable val a = 1 a = 2 // error: reassignment to val // list val ints1: List[Int] = 1 :: 2 :: Nil val ints2: List[Int] = ints1 :+ 3 println(ints1) // List(1, 2) println(ints2) // List(1, 2, 3) println(ints2 == 1 :: 2 :: 3 :: Nil) // true 15
  • 16. Immutability def fibonacci(n : Int): Int = { // while loop requires temporary varables var (a, b, i) = (0, 1, 0) while( i < n ) { val c = a + b a = b b = c i = i + 1 } return a } 16
  • 17. Immutability // recursion doesn't requires temporary varables def fibonacci(n : Int): Int = n match { case 0 | 1 => n case _ => fibonacci(n - 1) + fibonacci(n - 2) } 17
  • 18. Side Effect, by Definition A function is said to have a side effect if it modifies some state outside its scope or has an observable interaction with its calling functions or the outside world. — Side effect (computer science), Wikipedia 18
  • 19. Side Effect // this def has a side effect def currentProgram(guide: TVGuide, channel: Int): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(new Date()) } 19
  • 20. Side Effect // now it has no more side effects // and it is immutable def program(guide: TVGuide, channel: Int, date: Date): Program = { val schedule = guide.getSchedule(channel) schedule.programAt(date) } 20
  • 21. Let's Start at the Beginning Again A Program is functional iff it has no side effects.2 2 cf. Definition using Referential Transparency 21
  • 22. Why Functional Programming? 4 ! Type declaration exposes its action 4 " Short code & Fewer bugs 4 # Extremely easy to write unit test 4 $ Easy to maintain 22
  • 23. When should you use Functional Programming? 4 ! If you want to develop a product that you need to manage in the long run 4 " If you want to write an elegant code 4 # If you want to build up your project quickly 23
  • 24. When shoud not you use Functional Programming? 4 ! If you want to write a script to use once 4 ⏳ If your app requires nano-fast performance 4 # If you do not know about functional programming, and you need to develop your app in two weeks 4 $ If you want to live by maintaining a program for whole lifetime 24
  • 28. Grammars 4 스칼라 학교! 4 스칼라 공식 도큐먼트 입문 4 Effective Scala by Twitter 4 Effective Scala by Heejong Lee 4 Hacker Rank for Tutorials 4 Exercism 28
  • 29. What is Functional Programming? 4 함수형 프로그래밍이란 무엇인가? 4 어떤 프로그래밍 언어들이 함수형인가? 29
  • 30. Textbooks & References 4 ! 스칼라로 배우는 함수형 프로그래밍 4 Functional Programming Principles in Scala on Coursera 4 Big Data Analysis with Scala and Spark on Coursera 4 Scala Excercises 30
  • 31. Popular Scala Libraries 4 ! Scalaz: Scala library for functional programming 4 Cats: Lightweight, modular, and extensible library for functional programming 4 Monix: Asynchronous Programming for Scala 4 Circe: A JSON library for Scala powered by Cats 31
  • 32. Haskell 4 Eta Programming Language, Haskell on the JVM 4 (가장 쉬운) 하스켈 책 : 느긋하지만 우아하고 세련된 함수형 언어 32
  • 33. Resources 4 Scastie, an interactive playground for Scala 33