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.

Funtional Ruby - Mikhail Bortnyk

111 views

Published on

Ruby Meditation 12 - 19.11.2016
His theme is Functional Ruby. How to forgot pain of side effects and start to write your Erlang in Ruby. Или просто очередная серебряная пуля для задач, которые вам никогда не попадутся.
#rubymeditation

Published in: Technology

Funtional Ruby - Mikhail Bortnyk

  1. 1. FUNCTIONAL RUBY
  2. 2. ABOUT ME • Mikhail Bortnyk
  3. 3. ABOUT ME • Mikhail Bortnyk • Too old for this shit
  4. 4. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ
  5. 5. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer
  6. 6. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher
  7. 7. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher • kottans.org co-founder
  8. 8. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher • kottans.org co-founder • twitter @mikhailbortnyk
  9. 9. LOOK FOR JACKIE* SPECIAL OFFER *НАЕБЫВАЮ
  10. 10. PART ONE WHY FUNCTIONAL?
  11. 11. SIDE EFFECTS PROBLEM 1.1 • your objects store state
  12. 12. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state
  13. 13. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too
  14. 14. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too • summary: mess
  15. 15. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too • summary: mess
  16. 16. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data
  17. 17. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data • nuff said
  18. 18. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data • nuff said
  19. 19. HYPE PROBLEM 1.3 • Erlang
  20. 20. HYPE PROBLEM 1.3 • Erlang • Haskell
  21. 21. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala
  22. 22. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml
  23. 23. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp
  24. 24. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp • Javascript (sic!)
  25. 25. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp • Javascript (sic!) • Ruby (sic!)
  26. 26. PART TWO EASY (NOT REALLY) LEVEL
  27. 27. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby”
  28. 28. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions
  29. 29. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions • Data structures are just functions
  30. 30. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions • Data structures are just functions • Objects are just functions
  31. 31. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions • Data structures are just functions • Objects are just functions • Namespaces are just functions
  32. 32. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions • Data structures are just functions • Objects are just functions • Namespaces are just functions • P.S. Ruby HAS Tail Call Optimization
  33. 33. ROUGH HACK TAIL-CALL OPTIMIZATION def fact(n, acc=1) return acc if n <= 1 fact(n-1, n*acc) end RubyVM::InstructionSequence.compile_option = { tailcall_optimization: true, trace_instruction: false } fact(1000)
  34. 34. FUNCTIONAL VS OBJECT-ORIENTED SIDE TO SIDE COMPARISON new_person = ->(name, birthdate, gender, title, id=nil) { return ->(attribute) { return id if attribute == :id return name if attribute == :name return birthdate if attribute == :birthdate return gender if attribute == :gender return title if attribute == :title nil } } class Person attr_reader :id, :name, :birthdate, :gender, :title def initialize(name, birthdate, gender, title, id=nil) @id = id @name = name @birthdate = birthdate @gender = gender @title = title end end
  35. 35. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class”
  36. 36. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize
  37. 37. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class
  38. 38. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class • WTF are @-variables
  39. 39. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class • WTF are @-variables • difference between class and instance
  40. 40. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class • WTF are @-variables • difference between class and instance • WTF is “attr_reader”
  41. 41. NO HIDDEN MAGIC FUNCTIONAL PROGRAMMING CODE • how to define function
  42. 42. NO HIDDEN MAGIC FUNCTIONAL PROGRAMMING CODE • how to define function • how to call function
  43. 43. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new
  44. 44. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM
  45. 45. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment
  46. 46. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment • BUY EVEN MORE RAM
  47. 47. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment • BUY EVEN MORE RAM • avoid returns
  48. 48. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment • BUY EVEN MORE RAM • avoid returns • use lambdas
  49. 49. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment • BUY EVEN MORE RAM • avoid returns • use lambdas • DO NOT FORGET TO ORDER RAM RIGHT NOW
  50. 50. SIDE TO SIDE RULES COMPARISON FUNCTIONAL VS OBJECT-ORIENTED • how to perform tasks and how to track changes
 • state changes are important • order of execution is important • flow controlled by loops, conditionals, function calls • instances of structures and classes • focus on what information is needed and what transformations required • state changes are non-existent • order of execution is low- important • flow controlled by function calls including recursion • functions are first class objects, data collections
  51. 51. — Greenspun’s tenth rule of programming ANY SUFFICIENTLY COMPLICATED C OR FORTRAN PROGRAM CONTAINS AN AD-HOC, INFORMALLY-SPECIFIED, BUG-RIDDEN, SLOW IMPLEMENTATION OF HALF OF COMMON LISP. ” “
  52. 52. PART THREE I AM DEVELOPER, I DON’T WANT TO LEARN, I WANT PATTERN MATCHING AND IMMUTABILITY
  53. 53. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio
  54. 54. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java
  55. 55. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples
  56. 56. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples • has protocols
  57. 57. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples • has protocols • has Erlang-style pattern matching
  58. 58. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples • has protocols • has Erlang-style pattern matching • has function memoization
  59. 59. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples • has protocols • has Erlang-style pattern matching • has function memoization • supports MRI, JRuby and Rubinius
  60. 60. FUNCTIONAL-RUBY GEM SHORT OVERVIEW
  61. 61. PATTERN MATCHING AND TYPE CHECKING FUNCTIONAL-RUBY GEM class Yoga include Functional::PatternMatching include Functional::TypeCheck defn(:where_is_sun) do puts "o" end defn(:where_is_sun, 14) do puts "88!" end defn(:where_is_sun, _) do |name| puts "o, #{name}!" end defn(:where_is_sun, _) do |name| puts "Are you in wrong district, #{name.rude_name}?" end.when { |name| Type?(name, Moskal) } defn(:where_is_sun, _, _) do |name, surname| "o, #{name} #{surname}!" end end
  62. 62. MEMOIZATION FUNCTIONAL-RUBY GEM class Factors include Functional::Memo def self.sum_of(number) of(number).reduce(:+) end def self.of(number) (1..number).select {|i| factor?(number, i)} end def self.factor?(number, potential) number % potential == 0 end memoize(:sum_of) memoize(:of) end
  63. 63. RECORDS FUNCTIONAL-RUBY GEM Name = Functional::Record.new(:first, :middle, :last, :suffix) do mandatory :first, :last default :first, 'J.' default :last, 'Doe' end
  64. 64. QUESTION
  65. 65. Q&A
  66. 66. THANK YOU

×