SlideShare una empresa de Scribd logo
1 de 58
Functional
programming in Julia
Julia Taiwan發起人 杜岳華
讓我們來談談programming
Imperative programming (指令式)
•How to achieve the goal
Sequential Looping Condition Function
Declarative programming (宣告式)
•What we want to achieve
Functional programming
•What we want to achieve
FunctionFunction FunctionFunction
Function is from math
Function is from math
Function is from math
Function is from math
What does it do?
…
0
1
2
3
4
…
…
-1
0
1
2
3
…
f
add_one
[int]
0
1
2
3
4
…
[int]
-1
0
1
2
3
…
f
Function is mapping
[int]
0
1
2
3
4
…
[int]
-1
0
1
2
3
…
f
Function is mapping
[int]
0
1
2
3
4
…
[int]
-1
0
1
2
3
…
if x == -1
return 0
elseif x == 0
return 1
elseif x == 1
return 2
elseif x == 2
return 3
elseif x == 3
return 4
Function is mapping
[Name]
Alice
Bob
Chris
David
Eve
…
[Customer]
Cus1
Cus2
Cus3
Cus4
Cus5
…
if x == Cus1
return “Alice”
elseif x == Cus2
return “Bob”
elseif x == Cus3
return “Chris”
elseif x == Cus4
return “David”
elseif x == Cus5
return “Eve”
Pure function is good
Impure
• May change the global state (variable)
file = “/path/to/file”
function read_file()
f = open(file)
return readlines(f)
end
read_file()
Pure
• No side effect
function read_file(file)
f = open(file)
return readlines(f)
end
filename = “/path/to/file”
read_file(filename)
Impure
• There are hidden and mutable states (variables) inside the class
• Sometimes, it is difficult to reason about.
customer.setName(newName)
name = customer.getName()
Pure
• Good to reason about
newCustomer = setCustomerName(aCustomer, newName)
name = getCustomerName(aCustomer)
More practical benefits
• Laziness
• Cacheable results
• No order dependencies
• Easy to parallelize
Types are not class
Types are not class
Types are not class
[int]
0
1
2
3
…
[int]
-1
0
1
2
… These are types.
“int” is type.
“Customer” is type.
“Function” is also type.
Types separate data from behavior
Data
Behavior
Types are not class
• Types are cheap and classes are expensive!
• Types are static and classes are dynamic.
• Julia use Type, not Class.
• One can perform object-oriented design in Julia and easily write
it in functional style.
Functions are things
Functions are things
f
Functions are things
• Function as input
fg
Functions are things
• Function as output
f g
Functions are things
• Function as parameter
f
g
Lambda expression (function)
• Anonymous function
• Function as first-class citizen
() -> println()
x -> x + 5
(x, y, z) -> x + y + z
func = x -> x * 2
First-class citizen
• 可以被指定給變數
• 可以存在expression內
• 可以以參數的方式指定給function
• 可以以回傳值的方式由function回傳
• Second-class
• 可以如同function (operator)般被呼叫
• 可以以參數的方式指定給function
Higher-order function
• 那我們就可以做點有趣的事!
Higher-order function
• map
numbers = [1, 2, 3, 4]
results = map(x -> x + 5, numbers)
numbers = [1, 2, 3, 4]
results = []
for i=numbers
append!(results, i + 5)
end
Higher-order function
1
2
3
4
numbers
map x -> x + 5
Higher-order function
1
2
3
4
numbers results
map
x -> x + 5
Higher-order function
1
2
3
4
numbers
6
7
8
9
results
map
x -> x + 5
Higher-order function
map
Higher-order function
map
Higher-order function
map
Higher-order function
• filter
numbers = [1, 2, 3, 4]
results = filter(x -> x > 2, numbers)
numbers = [1, 2, 3, 4]
results = []
for i=numbers
if i > 2
append!(results, i)
end
end
Higher-order function
1
2
3
4
numbers
3
4
results
filter
x -> x > 2
Higher-order function
1
2
3
4
numbers
10
resultreduce
(x, y) -> x + y
Higher-order function
1
2
3
4
numbers
reduce
(x, y) -> x + y 3
3
4
reduce
(x, y) -> x + y 6
4
Higher-order function
10
result
reduce
(x, y) -> x + y
6
4
More higher-order functions
• foldl(op, iter)
• foldr(op, iter)
• sum(f, iter)
• count(f, iter)
• any(f, iter)
• all(f, iter)
• foreach(f, iter)
• mapreduce, mapfoldl, mapfoldr
Function composition
Function composition
f
𝑓 ∘ 𝑔 𝑥
= 𝑔(𝑓(𝑥))
g
g
f
Function composition
f ∘ g
Function composition
map
x -> x + 3
filter
x -> x > 2
Function composition
numbers = [1, 2, 3, 4]
results = filter(x -> x > 2, map(x -> x + 3, numbers))
More functional properties
• Recursion is better than iteration
• Relies on the language design
• Lazy evaluation
• Use Task to help you.
• Closure
• OK
• Curry
• OK
• Pattern matching
• use multiple dispatch in Julia
Pipeline?
map
x -> x + 3
filter
x -> x > 2
reduce
(x, y) ->
x + y
Reactive programming in Python
source = [1, 2, 3, 4, 5, 6, 7, 8]
result = Observable.from(source)
.map(lambda x: x ** 2)
.filter(lambda x: x > 20)
.reduce(lambda x, y: x+y)
.subscribe()
Reactive programming
Reactive programming in Julia
• Reactive.jl
• I hope…
source = [1, 2, 3, 4, 5, 6, 7, 8]
result = from(source)
(map, x -> x ^ 2)
(filter, x -> x > 20)
(reduce, (x, y) -> x + y)
(subscribe)
Thank you for attention

Más contenido relacionado

La actualidad más candente

Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
Mario Fusco
 
“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1
SethTisue
 

La actualidad más candente (20)

Swift Tutorial 2
Swift Tutorial  2Swift Tutorial  2
Swift Tutorial 2
 
A taste of Functional Programming
A taste of Functional ProgrammingA taste of Functional Programming
A taste of Functional Programming
 
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”
 
The Ring programming language version 1.5.2 book - Part 21 of 181
The Ring programming language version 1.5.2 book - Part 21 of 181The Ring programming language version 1.5.2 book - Part 21 of 181
The Ring programming language version 1.5.2 book - Part 21 of 181
 
TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.TCO in Python via bytecode manipulation.
TCO in Python via bytecode manipulation.
 
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
 
Scala - where objects and functions meet
Scala - where objects and functions meetScala - where objects and functions meet
Scala - where objects and functions meet
 
Introduction to functional programming using Ocaml
Introduction to functional programming using OcamlIntroduction to functional programming using Ocaml
Introduction to functional programming using Ocaml
 
Building a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGLBuilding a Tagless Final DSL for WebGL
Building a Tagless Final DSL for WebGL
 
Functors, applicatives, monads
Functors, applicatives, monadsFunctors, applicatives, monads
Functors, applicatives, monads
 
Atomically { Delete Your Actors }
Atomically { Delete Your Actors }Atomically { Delete Your Actors }
Atomically { Delete Your Actors }
 
Frsa
FrsaFrsa
Frsa
 
“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1“Tasks” in NetLogo 5.0beta1
“Tasks” in NetLogo 5.0beta1
 
L4 functions
L4 functionsL4 functions
L4 functions
 
Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)Queue Data Structure (w/ php egs)
Queue Data Structure (w/ php egs)
 
Functional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic ProgrammerFunctional Programming Patterns for the Pragmatic Programmer
Functional Programming Patterns for the Pragmatic Programmer
 
functions of C++
functions of C++functions of C++
functions of C++
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018Blazing Fast, Pure Effects without Monads — LambdaConf 2018
Blazing Fast, Pure Effects without Monads — LambdaConf 2018
 
Map, Reduce and Filter in Swift
Map, Reduce and Filter in SwiftMap, Reduce and Filter in Swift
Map, Reduce and Filter in Swift
 

Destacado

El derecho de las niñas y los niños
El derecho de las niñas y los niñosEl derecho de las niñas y los niños
El derecho de las niñas y los niños
Alfredo nobel
 
El derecho de las niñas y los niños
El derecho de las niñas y los niñosEl derecho de las niñas y los niños
El derecho de las niñas y los niños
Mirma Flores
 
Learning by Travelling: what do tourists learn while visiting South African T...
Learning by Travelling: what do tourists learn while visiting South African T...Learning by Travelling: what do tourists learn while visiting South African T...
Learning by Travelling: what do tourists learn while visiting South African T...
Isabella Rega
 

Destacado (14)

20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅20170217 julia小程式到專案發布之旅
20170217 julia小程式到專案發布之旅
 
Livro inglês 6º
Livro inglês 6ºLivro inglês 6º
Livro inglês 6º
 
El derecho de las niñas y los niños
El derecho de las niñas y los niñosEl derecho de las niñas y los niños
El derecho de las niñas y los niños
 
Fundamentos m3 b 1
Fundamentos m3 b 1Fundamentos m3 b 1
Fundamentos m3 b 1
 
20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch20170113 julia’s type system and multiple dispatch
20170113 julia’s type system and multiple dispatch
 
El derecho de las niñas y los niños
El derecho de las niñas y los niñosEl derecho de las niñas y los niños
El derecho de las niñas y los niños
 
Итоги работы Физтех-Союза в 2015 года
Итоги работы Физтех-Союза в 2015 годаИтоги работы Физтех-Союза в 2015 года
Итоги работы Физтех-Союза в 2015 года
 
Learning by Travelling: what do tourists learn while visiting South African T...
Learning by Travelling: what do tourists learn while visiting South African T...Learning by Travelling: what do tourists learn while visiting South African T...
Learning by Travelling: what do tourists learn while visiting South African T...
 
I2an newsletter september 2016
I2an newsletter september 2016I2an newsletter september 2016
I2an newsletter september 2016
 
Mastografía
MastografíaMastografía
Mastografía
 
Indian railways asset utilization
Indian railways asset utilizationIndian railways asset utilization
Indian railways asset utilization
 
ISA 2016: Diseño contínuo e hipótesis de usuário
ISA 2016: Diseño contínuo e hipótesis de usuárioISA 2016: Diseño contínuo e hipótesis de usuário
ISA 2016: Diseño contínuo e hipótesis de usuário
 
PROdruk №3
PROdruk №3PROdruk №3
PROdruk №3
 
Nilai nilai moral
Nilai nilai moralNilai nilai moral
Nilai nilai moral
 

Similar a 20170317 functional programming in julia

Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
Peter Kofler
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
openbala
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
Sergio Gil
 

Similar a 20170317 functional programming in julia (20)

The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202The Ring programming language version 1.8 book - Part 37 of 202
The Ring programming language version 1.8 book - Part 37 of 202
 
Introduction to Functional Programming
Introduction to Functional ProgrammingIntroduction to Functional Programming
Introduction to Functional Programming
 
Functional Programming - Past, Present and Future
Functional Programming - Past, Present and FutureFunctional Programming - Past, Present and Future
Functional Programming - Past, Present and Future
 
Functional Programming Past Present Future
Functional Programming Past Present FutureFunctional Programming Past Present Future
Functional Programming Past Present Future
 
Functional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks weekFunctional Programming in Javascript - IL Tech Talks week
Functional Programming in Javascript - IL Tech Talks week
 
9-Functions.pptx
9-Functions.pptx9-Functions.pptx
9-Functions.pptx
 
Slide PF Pertemuan 2.pptx
Slide PF Pertemuan 2.pptxSlide PF Pertemuan 2.pptx
Slide PF Pertemuan 2.pptx
 
Learn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik CubeLearn JavaScript by modeling Rubik Cube
Learn JavaScript by modeling Rubik Cube
 
Scala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereldScala: Functioneel programmeren in een object georiënteerde wereld
Scala: Functioneel programmeren in een object georiënteerde wereld
 
Yin Yangs of Software Development
Yin Yangs of Software DevelopmentYin Yangs of Software Development
Yin Yangs of Software Development
 
Why Haskell Matters
Why Haskell MattersWhy Haskell Matters
Why Haskell Matters
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011Functional Programming and Haskell - TWBR Away Day 2011
Functional Programming and Haskell - TWBR Away Day 2011
 
Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)Concepts of Functional Programming for Java Brains (2010)
Concepts of Functional Programming for Java Brains (2010)
 
Funkcija, objekt, python
Funkcija, objekt, pythonFunkcija, objekt, python
Funkcija, objekt, python
 
Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)Functional Programming Patterns (BuildStuff '14)
Functional Programming Patterns (BuildStuff '14)
 
Functional programming basics
Functional programming basicsFunctional programming basics
Functional programming basics
 
10. haskell Modules
10. haskell Modules10. haskell Modules
10. haskell Modules
 
Five Languages in a Moment
Five Languages in a MomentFive Languages in a Moment
Five Languages in a Moment
 
Thinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in PythonThinking in Functions: Functional Programming in Python
Thinking in Functions: Functional Programming in Python
 

Más de 岳華 杜

Más de 岳華 杜 (20)

[COSCUP 2023] 我的Julia軟體架構演進之旅
[COSCUP 2023] 我的Julia軟體架構演進之旅[COSCUP 2023] 我的Julia軟體架構演進之旅
[COSCUP 2023] 我的Julia軟體架構演進之旅
 
Julia: The language for future
Julia: The language for futureJulia: The language for future
Julia: The language for future
 
The Language for future-julia
The Language for future-juliaThe Language for future-julia
The Language for future-julia
 
20190907 Julia the language for future
20190907 Julia the language for future20190907 Julia the language for future
20190907 Julia the language for future
 
Metaprogramming in julia
Metaprogramming in juliaMetaprogramming in julia
Metaprogramming in julia
 
Introduction to julia
Introduction to juliaIntroduction to julia
Introduction to julia
 
自然語言處理概覽
自然語言處理概覽自然語言處理概覽
自然語言處理概覽
 
Introduction to machine learning
Introduction to machine learningIntroduction to machine learning
Introduction to machine learning
 
Semantic Segmentation - Fully Convolutional Networks for Semantic Segmentation
Semantic Segmentation - Fully Convolutional Networks for Semantic SegmentationSemantic Segmentation - Fully Convolutional Networks for Semantic Segmentation
Semantic Segmentation - Fully Convolutional Networks for Semantic Segmentation
 
Batch normalization 與他愉快的小伙伴
Batch normalization 與他愉快的小伙伴Batch normalization 與他愉快的小伙伴
Batch normalization 與他愉快的小伙伴
 
從 VAE 走向深度學習新理論
從 VAE 走向深度學習新理論從 VAE 走向深度學習新理論
從 VAE 走向深度學習新理論
 
COSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in JuliaCOSCUP: Foreign Function Call in Julia
COSCUP: Foreign Function Call in Julia
 
COSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in JuliaCOSCUP: Metaprogramming in Julia
COSCUP: Metaprogramming in Julia
 
COSCUP: Introduction to Julia
COSCUP: Introduction to JuliaCOSCUP: Introduction to Julia
COSCUP: Introduction to Julia
 
Introduction to Julia
Introduction to JuliaIntroduction to Julia
Introduction to Julia
 
20180506 Introduction to machine learning
20180506 Introduction to machine learning20180506 Introduction to machine learning
20180506 Introduction to machine learning
 
20171127 當julia遇上資料科學
20171127 當julia遇上資料科學20171127 當julia遇上資料科學
20171127 當julia遇上資料科學
 
20171117 oop and design patterns in julia
20171117 oop and design patterns in julia20171117 oop and design patterns in julia
20171117 oop and design patterns in julia
 
20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia20171014 tips for manipulating filesystem in julia
20171014 tips for manipulating filesystem in julia
 
20170807 julia的簡單而高效資料處理
20170807 julia的簡單而高效資料處理20170807 julia的簡單而高效資料處理
20170807 julia的簡單而高效資料處理
 

Último

Último (20)

🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...Driving Behavioral Change for Information Management through Data-Driven Gree...
Driving Behavioral Change for Information Management through Data-Driven Gree...
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 

20170317 functional programming in julia