SlideShare ist ein Scribd-Unternehmen logo
1 von 67
Downloaden Sie, um offline zu lesen
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

Weitere ähnliche Inhalte

Was ist angesagt?

Project Tools in Web Development
Project Tools in Web DevelopmentProject Tools in Web Development
Project Tools in Web Development
kmloomis
 
Lessons from Branch's launch
Lessons from Branch's launchLessons from Branch's launch
Lessons from Branch's launch
aflock
 
LeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than developmentLeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than development
John McCaffrey
 
Scala for android
Scala for androidScala for android
Scala for android
Tack Mobile
 
PRDC-ror-trilogy-part1
PRDC-ror-trilogy-part1PRDC-ror-trilogy-part1
PRDC-ror-trilogy-part1
Amir Barylko
 
Devops kc meetup_5_20_2013
Devops kc meetup_5_20_2013Devops kc meetup_5_20_2013
Devops kc meetup_5_20_2013
Aaron Blythe
 

Was ist angesagt? (18)

TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o MalTDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
TDC2016SP - Otimização Prematura: a Raíz de Todo o Mal
 
Refactoring RIA Unleashed 2011
Refactoring RIA Unleashed 2011Refactoring RIA Unleashed 2011
Refactoring RIA Unleashed 2011
 
Social dev camp_2011
Social dev camp_2011Social dev camp_2011
Social dev camp_2011
 
Project Tools in Web Development
Project Tools in Web DevelopmentProject Tools in Web Development
Project Tools in Web Development
 
Lessons from Branch's launch
Lessons from Branch's launchLessons from Branch's launch
Lessons from Branch's launch
 
LeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than developmentLeanStartup:Research is cheaper than development
LeanStartup:Research is cheaper than development
 
OOP in JS
OOP in JSOOP in JS
OOP in JS
 
AWS Users Meetup April 2015
AWS Users Meetup April 2015AWS Users Meetup April 2015
AWS Users Meetup April 2015
 
Conexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização PrematuraConexão Kinghost - Otimização Prematura
Conexão Kinghost - Otimização Prematura
 
Scala for android
Scala for androidScala for android
Scala for android
 
Premature optimisation: The Root of All Evil
Premature optimisation: The Root of All EvilPremature optimisation: The Root of All Evil
Premature optimisation: The Root of All Evil
 
PRDC-ror-trilogy-part1
PRDC-ror-trilogy-part1PRDC-ror-trilogy-part1
PRDC-ror-trilogy-part1
 
Better Framework Better Life
Better Framework Better LifeBetter Framework Better Life
Better Framework Better Life
 
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All EvilThe Open Commerce Conference - Premature Optimisation: The Root of All Evil
The Open Commerce Conference - Premature Optimisation: The Root of All Evil
 
Effectively Using UI Automation
Effectively Using UI AutomationEffectively Using UI Automation
Effectively Using UI Automation
 
Developing Complex WordPress Sites without Fear of Failure (with MVC)
Developing Complex WordPress Sites without Fear of Failure (with MVC)Developing Complex WordPress Sites without Fear of Failure (with MVC)
Developing Complex WordPress Sites without Fear of Failure (with MVC)
 
Sensus Uses Liferay to Strengthen Their Global Web Presence
Sensus Uses Liferay to Strengthen Their Global Web PresenceSensus Uses Liferay to Strengthen Their Global Web Presence
Sensus Uses Liferay to Strengthen Their Global Web Presence
 
Devops kc meetup_5_20_2013
Devops kc meetup_5_20_2013Devops kc meetup_5_20_2013
Devops kc meetup_5_20_2013
 

Andere mochten auch

Andere mochten auch (6)

Ruby Gems and Native Extensions - Stas Volovyk
Ruby Gems and Native Extensions - Stas VolovykRuby Gems and Native Extensions - Stas Volovyk
Ruby Gems and Native Extensions - Stas Volovyk
 
Evident Secrets of Successful Application - Max Goncharov
Evident Secrets of Successful Application - Max GoncharovEvident Secrets of Successful Application - Max Goncharov
Evident Secrets of Successful Application - Max Goncharov
 
Functional Web Apps with WebMachine Framework - Mikhail Bortnyk
Functional Web Apps with WebMachine Framework - Mikhail BortnykFunctional Web Apps with WebMachine Framework - Mikhail Bortnyk
Functional Web Apps with WebMachine Framework - Mikhail Bortnyk
 
Rodauth: Clean Authentication - Valentine Ostakh
Rodauth: Clean Authentication - Valentine OstakhRodauth: Clean Authentication - Valentine Ostakh
Rodauth: Clean Authentication - Valentine Ostakh
 
Lets build a game (in 24 min) by Ivan Zarea
Lets build a game (in 24 min) by Ivan ZareaLets build a game (in 24 min) by Ivan Zarea
Lets build a game (in 24 min) by Ivan Zarea
 
Say Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick SuttererSay Goodbye to Procedural Programming - Nick Sutterer
Say Goodbye to Procedural Programming - Nick Sutterer
 

Ähnlich wie Funtional Ruby - Mikhail Bortnyk

Ähnlich wie Funtional Ruby - Mikhail Bortnyk (20)

Adopting Elixir in a 10 year old codebase
Adopting Elixir in a 10 year old codebaseAdopting Elixir in a 10 year old codebase
Adopting Elixir in a 10 year old codebase
 
Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010Meta Programming in Ruby - Code Camp 2010
Meta Programming in Ruby - Code Camp 2010
 
Software Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with ScalaSoftware Engineering Thailand: Programming with Scala
Software Engineering Thailand: Programming with Scala
 
Functional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScriptFunctional programming techniques in regular JavaScript
Functional programming techniques in regular JavaScript
 
Becoming a more Productive Rails Developer
Becoming a more Productive Rails DeveloperBecoming a more Productive Rails Developer
Becoming a more Productive Rails Developer
 
Polyglot Grails
Polyglot GrailsPolyglot Grails
Polyglot Grails
 
Lexing and parsing
Lexing and parsingLexing and parsing
Lexing and parsing
 
Tackling Testing Telephony
Tackling Testing Telephony Tackling Testing Telephony
Tackling Testing Telephony
 
Different Ways of Integrating React into Rails - Pros and Cons
Different Ways of Integrating React into Rails - Pros and ConsDifferent Ways of Integrating React into Rails - Pros and Cons
Different Ways of Integrating React into Rails - Pros and Cons
 
Different ways of integrating React into Rails - Mikhail Bortnyk
Different ways of integrating React into Rails - Mikhail BortnykDifferent ways of integrating React into Rails - Mikhail Bortnyk
Different ways of integrating React into Rails - Mikhail Bortnyk
 
Jquery2012 defs
Jquery2012 defsJquery2012 defs
Jquery2012 defs
 
Scaling with swagger
Scaling with swaggerScaling with swagger
Scaling with swagger
 
Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1Learn Ruby 2011 - Session 1
Learn Ruby 2011 - Session 1
 
Testing gone-right
Testing gone-rightTesting gone-right
Testing gone-right
 
3 years with Clojure
3 years with Clojure3 years with Clojure
3 years with Clojure
 
Inside Wordnik's Architecture
Inside Wordnik's ArchitectureInside Wordnik's Architecture
Inside Wordnik's Architecture
 
AJAX & jQuery - City University WAD Module
AJAX & jQuery - City University WAD ModuleAJAX & jQuery - City University WAD Module
AJAX & jQuery - City University WAD Module
 
Apache Solr - search for everyone!
Apache Solr - search for everyone!Apache Solr - search for everyone!
Apache Solr - search for everyone!
 
Erlang - Dive Right In
Erlang - Dive Right InErlang - Dive Right In
Erlang - Dive Right In
 
Scala in the Wild
Scala in the WildScala in the Wild
Scala in the Wild
 

Mehr von Ruby Meditation

Mehr von Ruby Meditation (20)

Is this Legacy or Revenant Code? - Sergey Sergyenko | Ruby Meditation 30
Is this Legacy or Revenant Code? - Sergey Sergyenko  | Ruby Meditation 30Is this Legacy or Revenant Code? - Sergey Sergyenko  | Ruby Meditation 30
Is this Legacy or Revenant Code? - Sergey Sergyenko | Ruby Meditation 30
 
Life with GraphQL API: good practices and unresolved issues - Roman Dubrovsky...
Life with GraphQL API: good practices and unresolved issues - Roman Dubrovsky...Life with GraphQL API: good practices and unresolved issues - Roman Dubrovsky...
Life with GraphQL API: good practices and unresolved issues - Roman Dubrovsky...
 
Where is your license, dude? - Viacheslav Miroshnychenko | Ruby Meditation 29
Where is your license, dude? - Viacheslav Miroshnychenko | Ruby Meditation 29Where is your license, dude? - Viacheslav Miroshnychenko | Ruby Meditation 29
Where is your license, dude? - Viacheslav Miroshnychenko | Ruby Meditation 29
 
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
Dry-validation update. Dry-validation vs Dry-schema 1.0 - Aleksandra Stolyar ...
 
How to cook Rabbit on Production - Bohdan Parshentsev | Ruby Meditation 28
How to cook Rabbit on Production - Bohdan Parshentsev | Ruby Meditation 28 How to cook Rabbit on Production - Bohdan Parshentsev | Ruby Meditation 28
How to cook Rabbit on Production - Bohdan Parshentsev | Ruby Meditation 28
 
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
How to cook Rabbit on Production - Serhiy Nazarov | Ruby Meditation 28
 
Reinventing the wheel - why do it and how to feel good about it - Julik Tarkh...
Reinventing the wheel - why do it and how to feel good about it - Julik Tarkh...Reinventing the wheel - why do it and how to feel good about it - Julik Tarkh...
Reinventing the wheel - why do it and how to feel good about it - Julik Tarkh...
 
Performance Optimization 101 for Ruby developers - Nihad Abbasov (ENG) | Ruby...
Performance Optimization 101 for Ruby developers - Nihad Abbasov (ENG) | Ruby...Performance Optimization 101 for Ruby developers - Nihad Abbasov (ENG) | Ruby...
Performance Optimization 101 for Ruby developers - Nihad Abbasov (ENG) | Ruby...
 
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
Use cases for Serverless Technologies - Ruslan Tolstov (RUS) | Ruby Meditatio...
 
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
The Trailblazer Ride from the If Jungle into a Civilised Railway Station - Or...
 
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
What/How to do with GraphQL? - Valentyn Ostakh (ENG) | Ruby Meditation 27
 
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26New features in Rails 6 -  Nihad Abbasov (RUS) | Ruby Meditation 26
New features in Rails 6 - Nihad Abbasov (RUS) | Ruby Meditation 26
 
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
Security Scanning Overview - Tetiana Chupryna (RUS) | Ruby Meditation 26
 
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
Teach your application eloquence. Logs, metrics, traces - Dmytro Shapovalov (...
 
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
Best practices. Exploring - Ike Kurghinyan (RUS) | Ruby Meditation 26
 
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
Road to A/B testing - Alexey Vasiliev (ENG) | Ruby Meditation 25
 
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
Concurrency in production. Real life example - Dmytro Herasymuk | Ruby Medita...
 
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
Data encryption for Ruby web applications - Dmytro Shapovalov (RUS) | Ruby Me...
 
Rails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan GusievRails App performance at the limit - Bogdan Gusiev
Rails App performance at the limit - Bogdan Gusiev
 
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
GDPR. Next Y2K in 2018? - Anton Tkachov | Ruby Meditation #23
 

Kürzlich hochgeladen

Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Vector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptxVector Search -An Introduction in Oracle Database 23ai.pptx
Vector Search -An Introduction in Oracle Database 23ai.pptx
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024FWD Group - Insurer Innovation Award 2024
FWD Group - Insurer Innovation Award 2024
 
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
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdfRising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
Rising Above_ Dubai Floods and the Fortitude of Dubai International Airport.pdf
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 

Funtional Ruby - Mikhail Bortnyk

  • 2.
  • 4. ABOUT ME • Mikhail Bortnyk • Too old for this shit
  • 5. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ
  • 6. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer
  • 7. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher
  • 8. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher • kottans.org co-founder
  • 9. ABOUT ME • Mikhail Bortnyk • Too old for this shit • Work for Amoniac OÜ • Ruby developer • Language researcher • kottans.org co-founder • twitter @mikhailbortnyk
  • 12. SIDE EFFECTS PROBLEM 1.1 • your objects store state
  • 13. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state
  • 14. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too
  • 15. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too • summary: mess
  • 16. SIDE EFFECTS PROBLEM 1.1 • your objects store state • your code modifies state • your other code modifies state too • summary: mess
  • 17. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data
  • 18. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data • nuff said
  • 19. DATA/CODE ENTITY SHARING PROBLEM 1.2 • your model stores both your logic and data • nuff said
  • 22. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala
  • 23. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml
  • 24. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp
  • 25. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp • Javascript (sic!)
  • 26. HYPE PROBLEM 1.3 • Erlang • Haskell • Scala • OCaml • Lisp • Javascript (sic!) • Ruby (sic!)
  • 27. PART TWO EASY (NOT REALLY) LEVEL
  • 28. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby”
  • 29. LIMITS ARE FREEING CLEAN YOUR RUBY • David Copeland article “Adventures in functional programming with Ruby” • Loops are just functions
  • 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
  • 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
  • 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
  • 33. 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
  • 34. 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)
  • 35. 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
  • 36. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class”
  • 37. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize
  • 38. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class
  • 39. SPECIAL KNOWLEDGE WHAT’S WRONG WITH OO-CODE? • WTF is “class” • WTF are .new and initialize • API of class • WTF are @-variables
  • 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
  • 41. 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”
  • 42. NO HIDDEN MAGIC FUNCTIONAL PROGRAMMING CODE • how to define function
  • 43. NO HIDDEN MAGIC FUNCTIONAL PROGRAMMING CODE • how to define function • how to call function
  • 44. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new
  • 45. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM
  • 46. TO DON’T SHOOT YOUR LEG SAFETY RULES • do not modify, create new • BUY MORE RAM • functions should not depend on environment
  • 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
  • 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
  • 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
  • 50. 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
  • 51. 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
  • 52. — 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. ” “
  • 53. PART THREE I AM DEVELOPER, I DON’T WANT TO LEARN, I WANT PATTERN MATCHING AND IMMUTABILITY
  • 54. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio
  • 55. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java
  • 56. STILL EVOLVING! FUNCTIONAL-RUBY GEM • created by Jerry D’Antonio • inspired by Erlang, Clojure, Haskell and Functional Java • has records, unions and tuples
  • 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
  • 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
  • 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
  • 60. 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
  • 62. 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
  • 63. 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
  • 64. RECORDS FUNCTIONAL-RUBY GEM Name = Functional::Record.new(:first, :middle, :last, :suffix) do mandatory :first, :last default :first, 'J.' default :last, 'Doe' end
  • 66. Q&A