SlideShare una empresa de Scribd logo
1 de 21
Higher-Order Procedures ,[object Object],based on ‘Structure and Interpretation of Computer Programs’ (1985 MIT Press)  by Hal Abelson and Gerald Jay Sussman.  http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ Nathan Murray < [email_address] > v1.0  12/13/06  http://www.natemurray.com
legal The copy in this presentation is taken directly from Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Jay Sussman (MIT Press, 1984; ISBN 0-262-01077-1). Specifically section 1.3 Formulating Abstractions with Higher-Order Procedures. There are a few paraphrases and additional examples added.  The main difference is that the code has been converted from Lisp to Ruby.  The full text of this book and accompanying video lectures can be found at: http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ The video lectures are copyright by Hal Abelson and Gerald Jay Sussman.  The video lectures, and in turn this document, are licensed under a Creative Commons License. http://creativecommons.org/licenses/by-sa/2.0/
•  Procedures are abstractions def  cube (a) a * a * a end
( 3  *  3  *  3 ) (x * x * x) (y * y * y) x 3
[object Object],[object Object],[object Object],[object Object]
some examples ,[object Object]
The first computes the sum of the integers from a through b: def  sum_integers (a, b) return   0   if  a > b a + sum_integers((a +  1 ), b) end sum_integers( 1 ,  10 )  #=> 55
The second computes the sum of the cubes of the integers in the given range: def  sum_cubes (a, b) return   0   if  a > b cube(a) + sum_cubes((a +  1 ), b) end sum_cubes( 1 ,  3 )  #=> 36
The third computes the sum of a sequence of terms in the series: def  pi_sum (a, b) return   0   if  a > b ( 1.0  / ((a +  2 ) * a)) + (pi_sum((a +  4 ), b)) end pi_sum( 1 ,  1000 ) *  8   #=> 3.13959265558978 which converges to  π/8  (very slowly)
a pattern... def  sum_integers (a, b) return   0   if  a > b a + sum_integers((a +  1 ), b) end def  sum_cubes (a, b) return   0   if  a > b cube(a) + sum_cubes((a +  1 ), b) end def  pi_sum (a, b) return   0   if  a > b ( 1.0  / ((a +  2 ) * a)) + (pi_sum((a +  4 ), b)) end
template def  <name> (a, b) return   0   if  a > b <term>(a) + <name>(< next >(a), b) end
summation
def  <name> (a, b) return   0   if  a > b <term>(a) + <name>(< next >(a), b) end def  sum (term, a, the_next, b) return   0   if  a > b term.call(a) + sum(term, the_next.call(a), the_next, b) end
sum cubes def  inc (n) n +  1 end def  sum_cubes (a, b) cube =  self .method( :cube ).to_proc inc  =  self .method( :inc  ).to_proc sum(cube, a, inc, b) end sum_cubes( 1 ,  3 )  #=> 36
sum integers def  identity (x) x end def  sum_integers (a, b) id  =  self .method( :identity ).to_proc inc =  self .method( :inc   ).to_proc sum(id, a, inc, b) end sum_integers( 1 ,  10 )  #=> 55
π  sum def  pi_term (x) ( 1.0  / (x * (x+ 2 ))) end def  pi_next (x) (x +  4 ) end def  pi_sum (a, b) term =  self .method( :pi_term ).to_proc nex  =  self .method( :pi_next ).to_proc sum(term, a, nex, b) end pi_sum( 1 ,  1000 ) *  8   #=> 3.13959265558978 λ
λ  def  pi_sum (a, b) sum(  , a, , b ) end lambda { | x | ( 1.0  / (x * (x+ 2 ))) } lambda { | x | (x +  4 ) }
another example def  even? (i) i %  2  ==  0 end def  filter_evens (list) new_list = [] list.each  do  | element | new_list << element  if  even?(element) end new_list end filter_evens( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] )  #=> [2, 4, 6, 8]
returning procedures def  make_filter (predicate) lambda   do  | list | new_list = [] list.each  do  | element | new_list << element  if  predicate.call(element) end new_list end end filter_odds = make_filter(  lambda {| i | i %  2  !=  0 } ) filter_odds.call(list)  #=> [1, 3, 5, 7, 9]
returning procedures filter_ths = make_filter( lambda   do  | i | i.ordinal =~  / th$ /  ?  true  :  false end ) filter_ths.call(list)  #=> [4, 5, 6, 7, 8, 9, 10] require   ' facet/integer/ordinal ' 10 .ordinal  #=> &quot;10th&quot;
wrap-up ,[object Object],[object Object],[object Object]

Más contenido relacionado

La actualidad más candente

Chpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabChpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabJason Harvey
 
Composite functions
Composite functionsComposite functions
Composite functionsShaun Wilson
 
2.3 slopes and difference quotient t
2.3 slopes and difference quotient t2.3 slopes and difference quotient t
2.3 slopes and difference quotient tmath260
 
Equation of a straight line y b = m(x a)
Equation of a straight line y   b = m(x a)Equation of a straight line y   b = m(x a)
Equation of a straight line y b = m(x a)Shaun Wilson
 
mathemathics + Straight line equation
mathemathics + Straight line equationmathemathics + Straight line equation
mathemathics + Straight line equationeme87
 
Operations on Functions
Operations on FunctionsOperations on Functions
Operations on Functionsswartzje
 
Common symbols used in set theory
Common symbols used in set theoryCommon symbols used in set theory
Common symbols used in set theorysmZaiN1
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula50147146
 
AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007Darren Kuropatwa
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functionshisema01
 
Iit jee question_paper
Iit jee question_paperIit jee question_paper
Iit jee question_paperRahulMishra774
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009ingroy
 
Variables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive PropertyVariables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive Propertyleilanidediosboon
 

La actualidad más candente (19)

Chpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlabChpt13 laplacetransformsmatlab
Chpt13 laplacetransformsmatlab
 
Composite functions
Composite functionsComposite functions
Composite functions
 
2.3 slopes and difference quotient t
2.3 slopes and difference quotient t2.3 slopes and difference quotient t
2.3 slopes and difference quotient t
 
Equation of a straight line y b = m(x a)
Equation of a straight line y   b = m(x a)Equation of a straight line y   b = m(x a)
Equation of a straight line y b = m(x a)
 
Formula1
Formula1Formula1
Formula1
 
mathemathics + Straight line equation
mathemathics + Straight line equationmathemathics + Straight line equation
mathemathics + Straight line equation
 
เซต
เซตเซต
เซต
 
Operations on Functions
Operations on FunctionsOperations on Functions
Operations on Functions
 
Common symbols used in set theory
Common symbols used in set theoryCommon symbols used in set theory
Common symbols used in set theory
 
F(x) terminology
F(x) terminologyF(x) terminology
F(x) terminology
 
Pythagorean theorem and distance formula
Pythagorean theorem and distance formulaPythagorean theorem and distance formula
Pythagorean theorem and distance formula
 
AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007AP Calculus Slides October 17, 2007
AP Calculus Slides October 17, 2007
 
2.4 operations on functions
2.4 operations on functions2.4 operations on functions
2.4 operations on functions
 
Program python
Program pythonProgram python
Program python
 
133467 p3a2
133467 p3a2133467 p3a2
133467 p3a2
 
Iit jee question_paper
Iit jee question_paperIit jee question_paper
Iit jee question_paper
 
Operations With Functions May 25 2009
Operations With Functions May 25 2009Operations With Functions May 25 2009
Operations With Functions May 25 2009
 
Variables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive PropertyVariables, Expressions, and the Distributive Property
Variables, Expressions, and the Distributive Property
 
Vertex
VertexVertex
Vertex
 

Destacado

CST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic StructureCST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic Structurerrichards2
 
Constant strain triangular
Constant strain triangular Constant strain triangular
Constant strain triangular rahul183
 
An Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersAn Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersSuzzanne Uhland
 
Cascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop WorldCascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop WorldCascading
 
Finite element method
Finite element methodFinite element method
Finite element methodMANISH RANJAN
 
Standard Operating Procedures
Standard Operating ProceduresStandard Operating Procedures
Standard Operating Proceduresbiinoida
 
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALESEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALETotango
 
The Customer Success Metrics That Matter
The Customer Success Metrics That MatterThe Customer Success Metrics That Matter
The Customer Success Metrics That MatterOpsPanda
 
SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)Sri Minuty Interest
 
Customer Success Strategy Template
Customer Success Strategy TemplateCustomer Success Strategy Template
Customer Success Strategy TemplateOpsPanda
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsRonald Bartels
 
The 5 Must Have Customer Success Processes
The 5 Must Have Customer Success ProcessesThe 5 Must Have Customer Success Processes
The 5 Must Have Customer Success ProcessesTotango
 
Sale of goods act, 1930
Sale of goods act, 1930Sale of goods act, 1930
Sale of goods act, 1930surjeet tomar
 
Human Resource Management
Human Resource ManagementHuman Resource Management
Human Resource ManagementSuresh Rajan
 
Customer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by GainsightCustomer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by GainsightGainsight
 

Destacado (16)

CST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic StructureCST Review_Atoms and Atomic Structure
CST Review_Atoms and Atomic Structure
 
Constant strain triangular
Constant strain triangular Constant strain triangular
Constant strain triangular
 
An Introduction to Sale Procedures Orders
An Introduction to Sale Procedures OrdersAn Introduction to Sale Procedures Orders
An Introduction to Sale Procedures Orders
 
Cascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop WorldCascading - A Java Developer’s Companion to the Hadoop World
Cascading - A Java Developer’s Companion to the Hadoop World
 
Finite element method
Finite element methodFinite element method
Finite element method
 
Standard Operating Procedures
Standard Operating ProceduresStandard Operating Procedures
Standard Operating Procedures
 
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALESEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
SEVEN STEPS TO CUSTOMER SUCCESS AT SCALE
 
The Customer Success Metrics That Matter
The Customer Success Metrics That MatterThe Customer Success Metrics That Matter
The Customer Success Metrics That Matter
 
SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)SOP (Standard Operational Procedure)
SOP (Standard Operational Procedure)
 
Customer Success Strategy Template
Customer Success Strategy TemplateCustomer Success Strategy Template
Customer Success Strategy Template
 
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) OperationsStandard Operating Procedure (SOP) for Information Technology (IT) Operations
Standard Operating Procedure (SOP) for Information Technology (IT) Operations
 
The 5 Must Have Customer Success Processes
The 5 Must Have Customer Success ProcessesThe 5 Must Have Customer Success Processes
The 5 Must Have Customer Success Processes
 
Sop
SopSop
Sop
 
Sale of goods act, 1930
Sale of goods act, 1930Sale of goods act, 1930
Sale of goods act, 1930
 
Human Resource Management
Human Resource ManagementHuman Resource Management
Human Resource Management
 
Customer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by GainsightCustomer Success Management ( CSM ) Org Structures by Gainsight
Customer Success Management ( CSM ) Org Structures by Gainsight
 

Similar a Higher Order Procedures (in Ruby)

Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)c.titus.brown
 
PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersc.titus.brown
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentationManchireddy Reddy
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data ManipulationChu An
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3Abdul Haseeb
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Languagevsssuresh
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in ScalaTim Dalton
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetSamuel Lampa
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About RubyIan Bishop
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Dr. Volkan OBAN
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.pptkebeAman
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2Hang Zhao
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)riue
 

Similar a Higher Order Procedures (in Ruby) (20)

Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)Pycon 2011 talk (may not be final, note)
Pycon 2011 talk (may not be final, note)
 
PyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filtersPyCon 2011 talk - ngram assembly with Bloom filters
PyCon 2011 talk - ngram assembly with Bloom filters
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python Programming
 
B61301007 matlab documentation
B61301007 matlab documentationB61301007 matlab documentation
B61301007 matlab documentation
 
Basic R Data Manipulation
Basic R Data ManipulationBasic R Data Manipulation
Basic R Data Manipulation
 
Array
ArrayArray
Array
 
Matlab1
Matlab1Matlab1
Matlab1
 
Python programming workshop session 3
Python programming workshop session 3Python programming workshop session 3
Python programming workshop session 3
 
Scala as a Declarative Language
Scala as a Declarative LanguageScala as a Declarative Language
Scala as a Declarative Language
 
Matlab Basic Tutorial
Matlab Basic TutorialMatlab Basic Tutorial
Matlab Basic Tutorial
 
Grokking Monads in Scala
Grokking Monads in ScalaGrokking Monads in Scala
Grokking Monads in Scala
 
iRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat SheetiRODS Rule Language Cheat Sheet
iRODS Rule Language Cheat Sheet
 
Let’s Talk About Ruby
Let’s Talk About RubyLet’s Talk About Ruby
Let’s Talk About Ruby
 
Python Puzzlers
Python PuzzlersPython Puzzlers
Python Puzzlers
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Stacks.ppt
Stacks.pptStacks.ppt
Stacks.ppt
 
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
Optimization and Mathematical Programming in R and ROI - R Optimization Infra...
 
MATLAB-Introd.ppt
MATLAB-Introd.pptMATLAB-Introd.ppt
MATLAB-Introd.ppt
 
Fp in scala part 2
Fp in scala part 2Fp in scala part 2
Fp in scala part 2
 
関数潮流(Function Tendency)
関数潮流(Function Tendency)関数潮流(Function Tendency)
関数潮流(Function Tendency)
 

Último

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...apidays
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
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
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Zilliz
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024The Digital Insurer
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherRemote DBA Services
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MIND CTI
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 

Último (20)

Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
+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...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
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...
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024Manulife - Insurer Transformation Award 2024
Manulife - Insurer Transformation Award 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 

Higher Order Procedures (in Ruby)

  • 1.
  • 2. legal The copy in this presentation is taken directly from Structure and Interpretation of Computer Programs by Hal Abelson and Gerald Jay Sussman (MIT Press, 1984; ISBN 0-262-01077-1). Specifically section 1.3 Formulating Abstractions with Higher-Order Procedures. There are a few paraphrases and additional examples added. The main difference is that the code has been converted from Lisp to Ruby. The full text of this book and accompanying video lectures can be found at: http://swiss.csail.mit.edu/classes/6.001/abelson-sussman-lectures/ The video lectures are copyright by Hal Abelson and Gerald Jay Sussman. The video lectures, and in turn this document, are licensed under a Creative Commons License. http://creativecommons.org/licenses/by-sa/2.0/
  • 3. • Procedures are abstractions def cube (a) a * a * a end
  • 4. ( 3 * 3 * 3 ) (x * x * x) (y * y * y) x 3
  • 5.
  • 6.
  • 7. The first computes the sum of the integers from a through b: def sum_integers (a, b) return 0 if a > b a + sum_integers((a + 1 ), b) end sum_integers( 1 , 10 ) #=> 55
  • 8. The second computes the sum of the cubes of the integers in the given range: def sum_cubes (a, b) return 0 if a > b cube(a) + sum_cubes((a + 1 ), b) end sum_cubes( 1 , 3 ) #=> 36
  • 9. The third computes the sum of a sequence of terms in the series: def pi_sum (a, b) return 0 if a > b ( 1.0 / ((a + 2 ) * a)) + (pi_sum((a + 4 ), b)) end pi_sum( 1 , 1000 ) * 8 #=> 3.13959265558978 which converges to π/8 (very slowly)
  • 10. a pattern... def sum_integers (a, b) return 0 if a > b a + sum_integers((a + 1 ), b) end def sum_cubes (a, b) return 0 if a > b cube(a) + sum_cubes((a + 1 ), b) end def pi_sum (a, b) return 0 if a > b ( 1.0 / ((a + 2 ) * a)) + (pi_sum((a + 4 ), b)) end
  • 11. template def <name> (a, b) return 0 if a > b <term>(a) + <name>(< next >(a), b) end
  • 13. def <name> (a, b) return 0 if a > b <term>(a) + <name>(< next >(a), b) end def sum (term, a, the_next, b) return 0 if a > b term.call(a) + sum(term, the_next.call(a), the_next, b) end
  • 14. sum cubes def inc (n) n + 1 end def sum_cubes (a, b) cube = self .method( :cube ).to_proc inc = self .method( :inc ).to_proc sum(cube, a, inc, b) end sum_cubes( 1 , 3 ) #=> 36
  • 15. sum integers def identity (x) x end def sum_integers (a, b) id = self .method( :identity ).to_proc inc = self .method( :inc ).to_proc sum(id, a, inc, b) end sum_integers( 1 , 10 ) #=> 55
  • 16. π sum def pi_term (x) ( 1.0 / (x * (x+ 2 ))) end def pi_next (x) (x + 4 ) end def pi_sum (a, b) term = self .method( :pi_term ).to_proc nex = self .method( :pi_next ).to_proc sum(term, a, nex, b) end pi_sum( 1 , 1000 ) * 8 #=> 3.13959265558978 λ
  • 17. λ  def pi_sum (a, b) sum( , a, , b ) end lambda { | x | ( 1.0 / (x * (x+ 2 ))) } lambda { | x | (x + 4 ) }
  • 18. another example def even? (i) i % 2 == 0 end def filter_evens (list) new_list = [] list.each do | element | new_list << element if even?(element) end new_list end filter_evens( [ 1 , 2 , 3 , 4 , 5 , 6 , 7 , 8 ] ) #=> [2, 4, 6, 8]
  • 19. returning procedures def make_filter (predicate) lambda do | list | new_list = [] list.each do | element | new_list << element if predicate.call(element) end new_list end end filter_odds = make_filter( lambda {| i | i % 2 != 0 } ) filter_odds.call(list) #=> [1, 3, 5, 7, 9]
  • 20. returning procedures filter_ths = make_filter( lambda do | i | i.ordinal =~ / th$ / ? true : false end ) filter_ths.call(list) #=> [4, 5, 6, 7, 8, 9, 10] require ' facet/integer/ordinal ' 10 .ordinal #=> &quot;10th&quot;
  • 21.