20170317 functional programming in julia
Post on 21-Mar-2017
119 Views
Preview:
TRANSCRIPT
Functional programming in JuliaJulia 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?…01234…
…-10123…
f
add_one[int]
01234…
[int]-10123…
f
Function is mapping[int]
01234…
[int]-10123…
f
Function is mapping[int]
01234…
[int]-10123…
if x == -1 return 0elseif x == 0 return 1elseif x == 1 return 2elseif x == 2 return 3elseif x == 3 return 4
Function is mapping
[Name]AliceBobChrisDavidEve…
[Customer]
Cus1Cus2Cus3Cus4Cus5
…
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]
0123…
[int]-1012… These are types.
“int” is type.“Customer” is type.“Function” is also type.
Types separate data from behavior
DataBehavior
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
fg
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
1234
numbers map x -> x + 5
Higher-order function
1234
numbers resultsmap
x -> x + 5
Higher-order function
1234
numbers
6789
resultsmap
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) endend
Higher-order function
1234
numbers
34
resultsfilter
x -> x > 2
Higher-order function
1234
numbers
10
resultreduce
(x, y) -> x + y
Higher-order function
1234
numbersreduce
(x, y) -> x + y 334
reduce
(x, y) -> x + y 64
Higher-order function
10
result
reduce
(x, y) -> x + y64
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
gf
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
top related