SlideShare una empresa de Scribd logo
1 de 25
Functional techniques in Ruby ,[object Object]
The Big Idea Functions are data too.
list  =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] # in a language like Java, C, C++ you could write something like: for (int i  =   0 ; i  <  list.size(); i ++ ){ puts list[i]; } for  i  in  list puts i end list.each {| i | puts i }
{| i | puts i} | i |  # are the parameters of the block puts i  # is the body of the block {}  # define the start and end of the block def   anon_function ( i ) puts i end
Block Proc lambda closure What’s in a name?
# pseudo ruby code class   Array def   each for (i = 0 ; i  <   self .size; i ++ ) yield   self [i] end end end yield   self [i]
yield   1   => {|1| puts  1 } yield   2   => {|2| puts  2 } yield   3   => {|3| puts  3 } yield   4   => {|4| puts  4 } yield   5   => {|5| puts  5 } yield   6   => {|6| puts  6 } yield   7   => {|7| puts  7 } yield   8   => {|8| puts  8 } yield   9   => {|9| puts  9 } yield   10  => {|10| puts  10 } list.each {| i | puts i }
class   Array # still pseudocode def   each ( block ) for (i = 0 ; i  <   self .size; i ++ ) block.call( self [i]) end end end block.call( self [i])
block.call( 1 )  => {|1| puts  1 } block.call( 2 )  => {|2| puts  2 } block.call( 3 )  => {|3| puts  3 } block.call( 4 )  => {|4| puts  4 } block.call( 5 )  => {|5| puts  5 } block.call( 6 )  => {|6| puts  6 } block.call( 7 )  => {|7| puts  7 } block.call( 8 )  => {|8| puts  8 } block.call( 9 )  => {|9| puts  9 } block.call( 10 ) => {|10| puts  10 }
.call has to go somewhere block.class  # => Proc list.each {| i | puts i } == b  =   Proc . new  {| i | puts i } list.each( & b)
b  =  lambda {| i | puts i } list.each( & b) b  =   Proc . new  {| i | puts i } list.each( & b) b  =  proc {| i | puts i } list.each( & b) == ==
Quick Review ,[object Object],[object Object]
Now for the why? ,[object Object]
pattern of computation list.each {| i | puts i } where’s the iteration code?
another example def   even? ( num ) num  %   2   ==   0 end def   reject_evens ( list ) return_list  =   Array . new list.each  do  | item | return_list  <<  item  unless  even?(item) end return_list end def   reject_odds ( list ) return_list  =   Array . new list.each  do  | item | return_list  <<  item  if  even?(item) end return_list end list  =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] reject_evens(list) #=> [1,3,5,7,9] reject_odds(list) #=> [2,4,6,8]
def   reject ( list,  & block ) return_list  =   Array . new list.each  do  item return_list  <<  item  if  block.call(item) end return_list end list  =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] reject(list) {| i | even?(i) } #=> [1,3,5,7,9] reject(list) {| i | !even?(i) } #=> [2,4,6,8]
def   make_rejector ( & block ) lambda  do  | list | return_list  =   Array . new list.each  do  | item | return_list  <<  element  unless  block.call(item) end return_list end end list  =  [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] odd_rejector  =  make_rejector {| i | odd?(i) } odd_rejector.call(list) #=> [2,4,6,8]
Closure ,[object Object],[object Object]
def   make_rejector ( & block ) lambda  do  | list | return_list  =   Array . new list.each  do  | item | return_list  <<  element  if  block.call(item) end return_list end end
example closures def   complement   f lambda {|* args |  not  f.call( * args) } end even?  =  lambda {| n | n  %   2   ==   0  } odd?  =  complement(even?) odd?.call( 1 )  # true odd?.call( 2 )  # false
def   compose   f, g lambda {|* args | f.call(g.call( * args)) } end find  =  lambda {| name |  User .find_by_username(name) } auth  =  lambda {| u |  User .authenticate(u) } find_and_authenticate  =  compose(auth, find) find_and_authenticate.call( &quot;Erock&quot; )  #=> true
in the wild def   returning ( value )  #active_support yield (value) value end returning([])  do  | list | list  <<   1 list  <<   2 end # => [1,2]
respond_to  do  | format | format.html format.js {  render   :action  =>  &quot;index.rjs&quot; } format.xml {  render   :xml  =>  @user .to_xml } end # Rails RESTful routing map.resources  :users   do  | user | user.resources  :blogs end
# Rspec require   'bowling' describe  Bowling   do before( :each )  do @bowling   =   Bowling . new end it  &quot;should score 0 for gutter game&quot;   do 20 .times {  @bowling .hit( 0 ) } @bowling .score.should  ==   0 end end
more info ,[object Object],[object Object],[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

F# Presentation
F# PresentationF# Presentation
F# Presentation
mrkurt
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
Adam Getchell
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
Doug Sparling
 

La actualidad más candente (20)

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
F# Presentation
F# PresentationF# Presentation
F# Presentation
 
Operators
OperatorsOperators
Operators
 
Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)Chapter 1 Basic Programming (Python Programming Lecture)
Chapter 1 Basic Programming (Python Programming Lecture)
 
Python Basic Operators
Python Basic OperatorsPython Basic Operators
Python Basic Operators
 
Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014Functional Python Webinar from October 22nd, 2014
Functional Python Webinar from October 22nd, 2014
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)
 
An Overview Of Python With Functional Programming
An Overview Of Python With Functional ProgrammingAn Overview Of Python With Functional Programming
An Overview Of Python With Functional Programming
 
C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2C# 7.0, 7.1, 7.2
C# 7.0, 7.1, 7.2
 
Functional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis AtencioFunctional Programming in JavaScript by Luis Atencio
Functional Programming in JavaScript by Luis Atencio
 
Advanced C - Part 2
Advanced C - Part 2Advanced C - Part 2
Advanced C - Part 2
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
 
Operators in python
Operators in pythonOperators in python
Operators in python
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
 
An Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with JavascriptAn Introduction to Functional Programming with Javascript
An Introduction to Functional Programming with Javascript
 
Exp 3 3 d422
Exp 3 3  d422Exp 3 3  d422
Exp 3 3 d422
 

Similar a Functional techniques in Ruby

SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
Ted Leung
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
Rahul04August
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
Mark Needham
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
Dmitry Buzdin
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
Joe Jiang
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
Lei Kang
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
aztack
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
ShriKant Vashishtha
 

Similar a Functional techniques in Ruby (20)

Groovy
GroovyGroovy
Groovy
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - Groovy
 
Scala introduction
Scala introductionScala introduction
Scala introduction
 
C++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdfC++ Searching & Sorting5. Sort the following list using the select.pdf
C++ Searching & Sorting5. Sort the following list using the select.pdf
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
 
Scala 2 + 2 > 4
Scala 2 + 2 > 4Scala 2 + 2 > 4
Scala 2 + 2 > 4
 
Steady with ruby
Steady with rubySteady with ruby
Steady with ruby
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
 
Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#Mixing functional and object oriented approaches to programming in C#
Mixing functional and object oriented approaches to programming in C#
 
Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#Mixing Functional and Object Oriented Approaches to Programming in C#
Mixing Functional and Object Oriented Approaches to Programming in C#
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with Clojure
 
Composition in JavaScript
Composition in JavaScriptComposition in JavaScript
Composition in JavaScript
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applications
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love Affair
 
Antlr V3
Antlr V3Antlr V3
Antlr V3
 
Evolving with Java - How to Remain Effective
Evolving with Java - How to Remain EffectiveEvolving with Java - How to Remain Effective
Evolving with Java - How to Remain Effective
 
Useful javascript
Useful javascriptUseful javascript
Useful javascript
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick tour
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy code
 

Último

Último (20)

Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdfLinux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
Linux Foundation Edge _ Overview of FDO Software Components _ Randy at Intel.pdf
 
Top 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development CompaniesTop 10 CodeIgniter Development Companies
Top 10 CodeIgniter Development Companies
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
ASRock Industrial FDO Solutions in Action for Industrial Edge AI _ Kenny at A...
 
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The InsideCollecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
Collecting & Temporal Analysis of Behavioral Web Data - Tales From The Inside
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Vector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptxVector Search @ sw2con for slideshare.pptx
Vector Search @ sw2con for slideshare.pptx
 
Generative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdfGenerative AI Use Cases and Applications.pdf
Generative AI Use Cases and Applications.pdf
 
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
Event-Driven Architecture Masterclass: Integrating Distributed Data Stores Ac...
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Overview of Hyperledger Foundation
Overview of Hyperledger FoundationOverview of Hyperledger Foundation
Overview of Hyperledger Foundation
 
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on ThanabotsContinuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
Continuing Bonds Through AI: A Hermeneutic Reflection on Thanabots
 
WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024WebRTC and SIP not just audio and video @ OpenSIPS 2024
WebRTC and SIP not just audio and video @ OpenSIPS 2024
 
Design Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptxDesign Guidelines for Passkeys 2024.pptx
Design Guidelines for Passkeys 2024.pptx
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdfWhere to Learn More About FDO _ Richard at FIDO Alliance.pdf
Where to Learn More About FDO _ Richard at FIDO Alliance.pdf
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
How we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdfHow we scaled to 80K users by doing nothing!.pdf
How we scaled to 80K users by doing nothing!.pdf
 
2024 May Patch Tuesday
2024 May Patch Tuesday2024 May Patch Tuesday
2024 May Patch Tuesday
 

Functional techniques in Ruby

  • 1.
  • 2. The Big Idea Functions are data too.
  • 3. list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 ] # in a language like Java, C, C++ you could write something like: for (int i = 0 ; i < list.size(); i ++ ){ puts list[i]; } for i in list puts i end list.each {| i | puts i }
  • 4. {| i | puts i} | i | # are the parameters of the block puts i # is the body of the block {} # define the start and end of the block def anon_function ( i ) puts i end
  • 5. Block Proc lambda closure What’s in a name?
  • 6. # pseudo ruby code class Array def each for (i = 0 ; i < self .size; i ++ ) yield self [i] end end end yield self [i]
  • 7. yield 1 => {|1| puts 1 } yield 2 => {|2| puts 2 } yield 3 => {|3| puts 3 } yield 4 => {|4| puts 4 } yield 5 => {|5| puts 5 } yield 6 => {|6| puts 6 } yield 7 => {|7| puts 7 } yield 8 => {|8| puts 8 } yield 9 => {|9| puts 9 } yield 10 => {|10| puts 10 } list.each {| i | puts i }
  • 8. class Array # still pseudocode def each ( block ) for (i = 0 ; i < self .size; i ++ ) block.call( self [i]) end end end block.call( self [i])
  • 9. block.call( 1 ) => {|1| puts 1 } block.call( 2 ) => {|2| puts 2 } block.call( 3 ) => {|3| puts 3 } block.call( 4 ) => {|4| puts 4 } block.call( 5 ) => {|5| puts 5 } block.call( 6 ) => {|6| puts 6 } block.call( 7 ) => {|7| puts 7 } block.call( 8 ) => {|8| puts 8 } block.call( 9 ) => {|9| puts 9 } block.call( 10 ) => {|10| puts 10 }
  • 10. .call has to go somewhere block.class # => Proc list.each {| i | puts i } == b = Proc . new {| i | puts i } list.each( & b)
  • 11. b = lambda {| i | puts i } list.each( & b) b = Proc . new {| i | puts i } list.each( & b) b = proc {| i | puts i } list.each( & b) == ==
  • 12.
  • 13.
  • 14. pattern of computation list.each {| i | puts i } where’s the iteration code?
  • 15. another example def even? ( num ) num % 2 == 0 end def reject_evens ( list ) return_list = Array . new list.each do | item | return_list << item unless even?(item) end return_list end def reject_odds ( list ) return_list = Array . new list.each do | item | return_list << item if even?(item) end return_list end list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] reject_evens(list) #=> [1,3,5,7,9] reject_odds(list) #=> [2,4,6,8]
  • 16. def reject ( list, & block ) return_list = Array . new list.each do item return_list << item if block.call(item) end return_list end list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] reject(list) {| i | even?(i) } #=> [1,3,5,7,9] reject(list) {| i | !even?(i) } #=> [2,4,6,8]
  • 17. def make_rejector ( & block ) lambda do | list | return_list = Array . new list.each do | item | return_list << element unless block.call(item) end return_list end end list = [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 , 9 ] odd_rejector = make_rejector {| i | odd?(i) } odd_rejector.call(list) #=> [2,4,6,8]
  • 18.
  • 19. def make_rejector ( & block ) lambda do | list | return_list = Array . new list.each do | item | return_list << element if block.call(item) end return_list end end
  • 20. example closures def complement f lambda {|* args | not f.call( * args) } end even? = lambda {| n | n % 2 == 0 } odd? = complement(even?) odd?.call( 1 ) # true odd?.call( 2 ) # false
  • 21. def compose f, g lambda {|* args | f.call(g.call( * args)) } end find = lambda {| name | User .find_by_username(name) } auth = lambda {| u | User .authenticate(u) } find_and_authenticate = compose(auth, find) find_and_authenticate.call( &quot;Erock&quot; ) #=> true
  • 22. in the wild def returning ( value ) #active_support yield (value) value end returning([]) do | list | list << 1 list << 2 end # => [1,2]
  • 23. respond_to do | format | format.html format.js { render :action => &quot;index.rjs&quot; } format.xml { render :xml => @user .to_xml } end # Rails RESTful routing map.resources :users do | user | user.resources :blogs end
  • 24. # Rspec require 'bowling' describe Bowling do before( :each ) do @bowling = Bowling . new end it &quot;should score 0 for gutter game&quot; do 20 .times { @bowling .hit( 0 ) } @bowling .score.should == 0 end end
  • 25.