funtional ruby - mikhail bortnyk

67
FUNCTIONAL RUBY

Upload: ruby-meditation

Post on 07-Jan-2017

44 views

Category:

Technology


1 download

TRANSCRIPT

FUNCTIONAL RUBY

ABOUT ME

• Mikhail Bortnyk

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

• Work for Amoniac OÜ

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

• Work for Amoniac OÜ

• Ruby developer

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

• Work for Amoniac OÜ

• Ruby developer

• Language researcher

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

• Work for Amoniac OÜ

• Ruby developer

• Language researcher

• kottans.org co-founder

ABOUT ME

• Mikhail Bortnyk

• Too old for this shit

• Work for Amoniac OÜ

• Ruby developer

• Language researcher

• kottans.org co-founder

• twitter @mikhailbortnyk

LOOK FOR JACKIE*

SPECIAL OFFER

*НАЕБЫВАЮ

PART ONE WHY FUNCTIONAL?

SIDE EFFECTS

PROBLEM 1.1

• your objects store state

SIDE EFFECTS

PROBLEM 1.1

• your objects store state

• your code modifies state

SIDE EFFECTS

PROBLEM 1.1

• your objects store state

• your code modifies state

• your other code modifies state too

SIDE EFFECTS

PROBLEM 1.1

• your objects store state

• your code modifies state

• your other code modifies state too

• summary: mess

SIDE EFFECTS

PROBLEM 1.1

• your objects store state

• your code modifies state

• your other code modifies state too

• summary: mess

DATA/CODE ENTITY SHARING

PROBLEM 1.2

• your model stores both your logic and data

DATA/CODE ENTITY SHARING

PROBLEM 1.2

• your model stores both your logic and data

• nuff said

DATA/CODE ENTITY SHARING

PROBLEM 1.2

• your model stores both your logic and data

• nuff said

HYPE

PROBLEM 1.3

• Erlang

HYPE

PROBLEM 1.3

• Erlang

• Haskell

HYPE

PROBLEM 1.3

• Erlang

• Haskell

• Scala

HYPE

PROBLEM 1.3

• Erlang

• Haskell

• Scala

• OCaml

HYPE

PROBLEM 1.3

• Erlang

• Haskell

• Scala

• OCaml

• Lisp

HYPE

PROBLEM 1.3

• Erlang

• Haskell

• Scala

• OCaml

• Lisp

• Javascript (sic!)

HYPE

PROBLEM 1.3

• Erlang

• Haskell

• Scala

• OCaml

• Lisp

• Javascript (sic!)

• Ruby (sic!)

PART TWO EASY (NOT REALLY) LEVEL

LIMITS ARE FREEING

CLEAN YOUR RUBY

• David Copeland article “Adventures in functional programming with Ruby”

LIMITS ARE FREEING

CLEAN YOUR RUBY

• David Copeland article “Adventures in functional programming with Ruby”

• Loops are just functions

LIMITS ARE FREEING

CLEAN YOUR RUBY

• David Copeland article “Adventures in functional programming with Ruby”

• Loops are just functions

• Data structures are just functions

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

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

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

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)

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

SPECIAL KNOWLEDGE

WHAT’S WRONG WITH OO-CODE?

• WTF is “class”

SPECIAL KNOWLEDGE

WHAT’S WRONG WITH OO-CODE?

• WTF is “class”

• WTF are .new and initialize

SPECIAL KNOWLEDGE

WHAT’S WRONG WITH OO-CODE?

• WTF is “class”

• WTF are .new and initialize

• API of class

SPECIAL KNOWLEDGE

WHAT’S WRONG WITH OO-CODE?

• WTF is “class”

• WTF are .new and initialize

• API of class

• WTF are @-variables

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

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”

NO HIDDEN MAGIC

FUNCTIONAL PROGRAMMING CODE

• how to define function

NO HIDDEN MAGIC

FUNCTIONAL PROGRAMMING CODE

• how to define function

• how to call function

TO DON’T SHOOT YOUR LEG

SAFETY RULES

• do not modify, create new

TO DON’T SHOOT YOUR LEG

SAFETY RULES

• do not modify, create new

• BUY MORE RAM

TO DON’T SHOOT YOUR LEG

SAFETY RULES

• do not modify, create new

• BUY MORE RAM

• functions should not depend on environment

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

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

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

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

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

— 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.

PART THREE I AM DEVELOPER, I DON’T WANT

TO LEARN, I WANT PATTERN MATCHING AND IMMUTABILITY

STILL EVOLVING!

FUNCTIONAL-RUBY GEM

• created by Jerry D’Antonio

STILL EVOLVING!

FUNCTIONAL-RUBY GEM

• created by Jerry D’Antonio

• inspired by Erlang, Clojure, Haskell and Functional Java

STILL EVOLVING!

FUNCTIONAL-RUBY GEM

• created by Jerry D’Antonio

• inspired by Erlang, Clojure, Haskell and Functional Java

• has records, unions and tuples

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

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

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

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

FUNCTIONAL-RUBY GEM SHORT OVERVIEW

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

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

RECORDS

FUNCTIONAL-RUBY GEM

Name = Functional::Record.new(:first, :middle, :last, :suffix) do mandatory :first, :last default :first, 'J.' default :last, 'Doe' end

QUESTION

Q&A

THANK YOU