SlideShare una empresa de Scribd logo
1 de 30
Descargar para leer sin conexión
Functional programming in Ruby




                           Alex Teut
                                 aka
                           @jaturken
Functional Programming

  A programming paradigm that treats
computation as the evaluation of functions
   and avoids state and mutable data.
Imperative Programming

  A programming paradigm that treats
computation as the evaluation of functions
   and avoids state and mutable data.
A programming paradigm that describes
computation in terms of statements that
       change a program state.
The difference

  A programming paradigm that treats
computation as the evaluation of functions
   and avoids state and mutable data.
A programming paradigm that describes
computation in terms of statements that
       change a program state.
Imperative functions can have side effects
  that may change the value of program
     state, so this approach looses...
Referential transparency




The same language expression can result
   in different values at different times
 depending on the state of the executing
                  program.
Pure Functions

●   Always evaluates the same result value
    given the same argument values
●   Evaluation of the result does not cause
    any side effect
Pure

●   sin
●   to_s
●   Enumerable#select
●   Enumerable#collect
●   Array#uniq
●   Hash#merge
Non-pure

Non-determinated:       Side-effects:
●   Date.today      ●   print
●   rand            ●   require
●   Externall API   ●   Enumerable#select!
    calls           ●   Enumerable#collect!
                    ●   Array#uniq!
                    ●   Hash#merge!
                    ●   Hash#delete
Pure Functions is Good

●   Can be cached/memoized
●   Order of calculation does not matter
●   Any evaluation strategy can be used
●   Chain can be easily paralellized
Functional Programming




X=X+1
Functional Programming




X=X+1
Iterations without iterator?
Recursion
Example
Tail Call




A special case of recursion when recursive
 call of function itself is its last operation.
Example
Expand example code




tail_call_factorial:     non_tail_call_factorial:
(fact-tail 3 1)          (fact 3)
(fact-tail 2 3)          (* 3 (fact 2))
(fact-tail 1 6)          (* 3 (* 2 (fact 1)))
6                        (* 3 (* 2 1))
                         (* 3 2)
                         6
Tail Call Optimization




Tail call is equivalent to iteration. So Tail
Call Optimization(TCO) is evaluating of
          Tail Call as an iteration.
 In Ruby TCO is turned off by default.
First Class Objects

●   can be stored in values and data
    structures
●   can be passed as a parameter to an
    expression
●   can be returned as the result of a
    expression
Higher-order Functions




   Functions that can either take other
functions as arguments or return them as
                 results
Closure




  A function or reference to a function
together with a referencing environment
Example
Currying




 The technique of transforming a function
that takes multiple arguments to a chain of
  functions each with a single argument.
Without currying
With currying
Lazy Evaluation




 An evaluation strategy which delays the
evaluation of an expression until its value
                is needed
Example
Advantages of Functional
                        Programming

●   Reliability
●   Parallelizm
●   Easy unit testing
●   Easy debugging
Disadvantages of Functional
                       Programming

●   Seems to be more difficult
●   Typically less efficient
●   Garbage collector needed
●   Difficulties with IO methods and states
Thank you for attention!

Más contenido relacionado

La actualidad más candente

Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAminaRepo
 
Functional Programming In PHP I
Functional Programming In PHP IFunctional Programming In PHP I
Functional Programming In PHP IUmut IŞIK
 
PHP = PHunctional Programming
PHP = PHunctional ProgrammingPHP = PHunctional Programming
PHP = PHunctional ProgrammingLuis Atencio
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppteShikshak
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scalaStratio
 
Intro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala MontrealIntro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala Montrealfelixtrepanier
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patternszupzup.org
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macrosAnand Kumar
 
Functional Programming in Python
Functional Programming in PythonFunctional Programming in Python
Functional Programming in PythonHaim Michael
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++Jenish Patel
 
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsNotes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsJay Baxi
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - IUgur Yeter
 
C sharp fundamentals Part I
C sharp fundamentals Part IC sharp fundamentals Part I
C sharp fundamentals Part IDevMix
 

La actualidad más candente (20)

Introduction to Functional programming
Introduction to Functional programmingIntroduction to Functional programming
Introduction to Functional programming
 
Python algorithm
Python algorithmPython algorithm
Python algorithm
 
Basics of cpp
Basics of cppBasics of cpp
Basics of cpp
 
Aaa ped-2- Python: Basics
Aaa ped-2- Python: BasicsAaa ped-2- Python: Basics
Aaa ped-2- Python: Basics
 
Functional Programming In PHP I
Functional Programming In PHP IFunctional Programming In PHP I
Functional Programming In PHP I
 
Verilog Tasks and functions
Verilog Tasks and functionsVerilog Tasks and functions
Verilog Tasks and functions
 
PHP = PHunctional Programming
PHP = PHunctional ProgrammingPHP = PHunctional Programming
PHP = PHunctional Programming
 
Lecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.pptLecture21 categoriesof userdefinedfunctions.ppt
Lecture21 categoriesof userdefinedfunctions.ppt
 
Functions
FunctionsFunctions
Functions
 
Functional programming in scala
Functional programming in scalaFunctional programming in scala
Functional programming in scala
 
Intro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala MontrealIntro to Functional Programming @ Scala Montreal
Intro to Functional Programming @ Scala Montreal
 
Functional Programming Principles & Patterns
Functional Programming Principles & PatternsFunctional Programming Principles & Patterns
Functional Programming Principles & Patterns
 
Operators in java
Operators in javaOperators in java
Operators in java
 
Inline functions & macros
Inline functions & macrosInline functions & macros
Inline functions & macros
 
Functional Programming in Python
Functional Programming in PythonFunctional Programming in Python
Functional Programming in Python
 
Inline function in C++
Inline function in C++Inline function in C++
Inline function in C++
 
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - BasicsNotes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
Notes: Verilog Part 1 - Overview - Hierarchical Modeling Concepts - Basics
 
Java 8 Functional Programming - I
Java 8 Functional Programming - IJava 8 Functional Programming - I
Java 8 Functional Programming - I
 
C sharp fundamentals Part I
C sharp fundamentals Part IC sharp fundamentals Part I
C sharp fundamentals Part I
 
Ppl
PplPpl
Ppl
 

Destacado

Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional styleNiranjan Sarade
 
Functional programming ruby mty
Functional programming   ruby mtyFunctional programming   ruby mty
Functional programming ruby mtyAdrianGzz2112
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!Gautam Rege
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional ProgrammingGeison Goes
 
Ruby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examplesRuby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examplesNiranjan Sarade
 
Functional Programming with Ruby
Functional Programming with RubyFunctional Programming with Ruby
Functional Programming with Rubytokland
 
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEIC
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEICTỔNG HỢP KỸ NĂNG LÀM BÀI TOEIC
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEICQuy Nguyen
 
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012TUANTOEIC990
 
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEIC
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEICTừ vựng TOEIC Part 7 thông dụng nhất - ORI TOEIC
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEICTran Diem
 

Destacado (10)

Functional programming and ruby in functional style
Functional programming and ruby in functional styleFunctional programming and ruby in functional style
Functional programming and ruby in functional style
 
Functional programming ruby mty
Functional programming   ruby mtyFunctional programming   ruby mty
Functional programming ruby mty
 
RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!RubyConf Portugal 2014 - Why ruby must go!
RubyConf Portugal 2014 - Why ruby must go!
 
Ruby Functional Programming
Ruby Functional ProgrammingRuby Functional Programming
Ruby Functional Programming
 
Ruby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examplesRuby's Arrays and Hashes with examples
Ruby's Arrays and Hashes with examples
 
Luyen thi toeic cap toc part 5 - ngoai ngu 360
 Luyen thi toeic cap toc part 5 - ngoai ngu 360 Luyen thi toeic cap toc part 5 - ngoai ngu 360
Luyen thi toeic cap toc part 5 - ngoai ngu 360
 
Functional Programming with Ruby
Functional Programming with RubyFunctional Programming with Ruby
Functional Programming with Ruby
 
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEIC
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEICTỔNG HỢP KỸ NĂNG LÀM BÀI TOEIC
TỔNG HỢP KỸ NĂNG LÀM BÀI TOEIC
 
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012
Phuong phap nghe T636 cho Part 3 Part 4 New TOEIC_ TuanToeic.com_Tuantology_2012
 
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEIC
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEICTừ vựng TOEIC Part 7 thông dụng nhất - ORI TOEIC
Từ vựng TOEIC Part 7 thông dụng nhất - ORI TOEIC
 

Similar a Functional Programming in Ruby

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scaladatamantra
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programmingKonrad Szydlo
 
Python functional programming
Python functional programmingPython functional programming
Python functional programmingGeison Goes
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#Riccardo Terrell
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural ProgrammingDeepam Aggarwal
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmTech Triveni
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#Tadeusz Balcer
 
Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Alain Lompo
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to javaSadhanaParameswaran
 
Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Calvin Cheng
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean codeEman Mohamed
 
Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrxIlia Idakiev
 
Functional programming
Functional programmingFunctional programming
Functional programmingPiumiPerera7
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the EffortBoldRadius Solutions
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programmingNico Ludwig
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojureJuan-Manuel Gimeno
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改Jeff Lee
 
Introduction to functional programming, with Elixir
Introduction to functional programming,  with ElixirIntroduction to functional programming,  with Elixir
Introduction to functional programming, with Elixirkirandanduprolu
 

Similar a Functional Programming in Ruby (20)

Functional programming in Scala
Functional programming in ScalaFunctional programming in Scala
Functional programming in Scala
 
Introduction to functional programming
Introduction to functional programmingIntroduction to functional programming
Introduction to functional programming
 
Python functional programming
Python functional programmingPython functional programming
Python functional programming
 
Why functional programming in C# & F#
Why functional programming in C# & F#Why functional programming in C# & F#
Why functional programming in C# & F#
 
Monolithic and Procedural Programming
Monolithic and Procedural ProgrammingMonolithic and Procedural Programming
Monolithic and Procedural Programming
 
Why should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming ParadigmWhy should a Java programmer shifts towards Functional Programming Paradigm
Why should a Java programmer shifts towards Functional Programming Paradigm
 
Functional Programming in C#
Functional Programming in C#Functional Programming in C#
Functional Programming in C#
 
Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...Designing function families and bundles with java's behaviors parameterisatio...
Designing function families and bundles with java's behaviors parameterisatio...
 
Learn To Code: Introduction to java
Learn To Code: Introduction to javaLearn To Code: Introduction to java
Learn To Code: Introduction to java
 
Feature toggling
Feature togglingFeature toggling
Feature toggling
 
Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)Functional Programming for OO Programmers (part 1)
Functional Programming for OO Programmers (part 1)
 
Sharable of qualities of clean code
Sharable of qualities of clean codeSharable of qualities of clean code
Sharable of qualities of clean code
 
Predictable reactive state management - ngrx
Predictable reactive state management - ngrxPredictable reactive state management - ngrx
Predictable reactive state management - ngrx
 
Functional programming
Functional programmingFunctional programming
Functional programming
 
Functional Programming - Worth the Effort
Functional Programming - Worth the EffortFunctional Programming - Worth the Effort
Functional Programming - Worth the Effort
 
(3) cpp procedural programming
(3) cpp procedural programming(3) cpp procedural programming
(3) cpp procedural programming
 
Logic programming in python
Logic programming in pythonLogic programming in python
Logic programming in python
 
Functional programming in clojure
Functional programming in clojureFunctional programming in clojure
Functional programming in clojure
 
Reactive cocoa 101改
Reactive cocoa 101改Reactive cocoa 101改
Reactive cocoa 101改
 
Introduction to functional programming, with Elixir
Introduction to functional programming,  with ElixirIntroduction to functional programming,  with Elixir
Introduction to functional programming, with Elixir
 

Functional Programming in Ruby

  • 1. Functional programming in Ruby Alex Teut aka @jaturken
  • 2. Functional Programming A programming paradigm that treats computation as the evaluation of functions and avoids state and mutable data.
  • 3. Imperative Programming A programming paradigm that treats computation as the evaluation of functions and avoids state and mutable data. A programming paradigm that describes computation in terms of statements that change a program state.
  • 4. The difference A programming paradigm that treats computation as the evaluation of functions and avoids state and mutable data. A programming paradigm that describes computation in terms of statements that change a program state. Imperative functions can have side effects that may change the value of program state, so this approach looses...
  • 5. Referential transparency The same language expression can result in different values at different times depending on the state of the executing program.
  • 6. Pure Functions ● Always evaluates the same result value given the same argument values ● Evaluation of the result does not cause any side effect
  • 7. Pure ● sin ● to_s ● Enumerable#select ● Enumerable#collect ● Array#uniq ● Hash#merge
  • 8. Non-pure Non-determinated: Side-effects: ● Date.today ● print ● rand ● require ● Externall API ● Enumerable#select! calls ● Enumerable#collect! ● Array#uniq! ● Hash#merge! ● Hash#delete
  • 9. Pure Functions is Good ● Can be cached/memoized ● Order of calculation does not matter ● Any evaluation strategy can be used ● Chain can be easily paralellized
  • 15. Tail Call A special case of recursion when recursive call of function itself is its last operation.
  • 17. Expand example code tail_call_factorial: non_tail_call_factorial: (fact-tail 3 1) (fact 3) (fact-tail 2 3) (* 3 (fact 2)) (fact-tail 1 6) (* 3 (* 2 (fact 1))) 6 (* 3 (* 2 1)) (* 3 2) 6
  • 18. Tail Call Optimization Tail call is equivalent to iteration. So Tail Call Optimization(TCO) is evaluating of Tail Call as an iteration. In Ruby TCO is turned off by default.
  • 19. First Class Objects ● can be stored in values and data structures ● can be passed as a parameter to an expression ● can be returned as the result of a expression
  • 20. Higher-order Functions Functions that can either take other functions as arguments or return them as results
  • 21. Closure A function or reference to a function together with a referencing environment
  • 23. Currying The technique of transforming a function that takes multiple arguments to a chain of functions each with a single argument.
  • 26. Lazy Evaluation An evaluation strategy which delays the evaluation of an expression until its value is needed
  • 28. Advantages of Functional Programming ● Reliability ● Parallelizm ● Easy unit testing ● Easy debugging
  • 29. Disadvantages of Functional Programming ● Seems to be more difficult ● Typically less efficient ● Garbage collector needed ● Difficulties with IO methods and states
  • 30. Thank you for attention!