Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

20170317 functional programming in julia

921 views

Published on

Julia Taiwan 4th meetup

Published in: Technology
  • Be the first to comment

20170317 functional programming in julia

  1. 1. Functional programming in Julia Julia Taiwan發起人 杜岳華
  2. 2. 讓我們來談談programming
  3. 3. Imperative programming (指令式) •How to achieve the goal Sequential Looping Condition Function
  4. 4. Declarative programming (宣告式) •What we want to achieve
  5. 5. Functional programming •What we want to achieve FunctionFunction FunctionFunction
  6. 6. Function is from math
  7. 7. Function is from math
  8. 8. Function is from math
  9. 9. Function is from math
  10. 10. What does it do? … 0 1 2 3 4 … … -1 0 1 2 3 … f
  11. 11. add_one [int] 0 1 2 3 4 … [int] -1 0 1 2 3 … f
  12. 12. Function is mapping [int] 0 1 2 3 4 … [int] -1 0 1 2 3 … f
  13. 13. 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
  14. 14. 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”
  15. 15. Pure function is good
  16. 16. Impure • May change the global state (variable) file = “/path/to/file” function read_file() f = open(file) return readlines(f) end read_file()
  17. 17. Pure • No side effect function read_file(file) f = open(file) return readlines(f) end filename = “/path/to/file” read_file(filename)
  18. 18. Impure • There are hidden and mutable states (variables) inside the class • Sometimes, it is difficult to reason about. customer.setName(newName) name = customer.getName()
  19. 19. Pure • Good to reason about newCustomer = setCustomerName(aCustomer, newName) name = getCustomerName(aCustomer)
  20. 20. More practical benefits • Laziness • Cacheable results • No order dependencies • Easy to parallelize
  21. 21. Types are not class
  22. 22. Types are not class
  23. 23. 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.
  24. 24. Types separate data from behavior Data Behavior
  25. 25. 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.
  26. 26. Functions are things
  27. 27. Functions are things f
  28. 28. Functions are things • Function as input fg
  29. 29. Functions are things • Function as output f g
  30. 30. Functions are things • Function as parameter f g
  31. 31. Lambda expression (function) • Anonymous function • Function as first-class citizen () -> println() x -> x + 5 (x, y, z) -> x + y + z func = x -> x * 2
  32. 32. First-class citizen • 可以被指定給變數 • 可以存在expression內 • 可以以參數的方式指定給function • 可以以回傳值的方式由function回傳 • Second-class • 可以如同function (operator)般被呼叫 • 可以以參數的方式指定給function
  33. 33. Higher-order function • 那我們就可以做點有趣的事!
  34. 34. 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
  35. 35. Higher-order function 1 2 3 4 numbers map x -> x + 5
  36. 36. Higher-order function 1 2 3 4 numbers results map x -> x + 5
  37. 37. Higher-order function 1 2 3 4 numbers 6 7 8 9 results map x -> x + 5
  38. 38. Higher-order function map
  39. 39. Higher-order function map
  40. 40. Higher-order function map
  41. 41. 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
  42. 42. Higher-order function 1 2 3 4 numbers 3 4 results filter x -> x > 2
  43. 43. Higher-order function 1 2 3 4 numbers 10 resultreduce (x, y) -> x + y
  44. 44. Higher-order function 1 2 3 4 numbers reduce (x, y) -> x + y 3 3 4 reduce (x, y) -> x + y 6 4
  45. 45. Higher-order function 10 result reduce (x, y) -> x + y 6 4
  46. 46. 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
  47. 47. Function composition
  48. 48. Function composition f 𝑓 ∘ 𝑔 𝑥 = 𝑔(𝑓(𝑥)) g
  49. 49. g f
  50. 50. Function composition f ∘ g
  51. 51. Function composition map x -> x + 3 filter x -> x > 2
  52. 52. Function composition numbers = [1, 2, 3, 4] results = filter(x -> x > 2, map(x -> x + 3, numbers))
  53. 53. 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
  54. 54. Pipeline? map x -> x + 3 filter x -> x > 2 reduce (x, y) -> x + y
  55. 55. 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()
  56. 56. Reactive programming
  57. 57. 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)
  58. 58. Thank you for attention

×