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

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rick Flair
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfMounikaPolabathina
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfNeo4j
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentPim van der Noll
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationKnoldus Inc.
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...AliaaTarek5
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch TuesdayIvanti
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...Wes McKinney
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterMydbops
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsSergiu Bodiu
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersNicole Novielli
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteDianaGray10
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesKari Kakkonen
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity PlanDatabarracks
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityIES VE
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality AssuranceInflectra
 

Último (20)

From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...Rise of the Machines: Known As Drones...
Rise of the Machines: Known As Drones...
 
What is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdfWhat is DBT - The Ultimate Data Build Tool.pdf
What is DBT - The Ultimate Data Build Tool.pdf
 
Connecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdfConnecting the Dots for Information Discovery.pdf
Connecting the Dots for Information Discovery.pdf
 
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native developmentEmixa Mendix Meetup 11 April 2024 about Mendix Native development
Emixa Mendix Meetup 11 April 2024 about Mendix Native development
 
Data governance with Unity Catalog Presentation
Data governance with Unity Catalog PresentationData governance with Unity Catalog Presentation
Data governance with Unity Catalog Presentation
 
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
(How to Program) Paul Deitel, Harvey Deitel-Java How to Program, Early Object...
 
2024 April Patch Tuesday
2024 April Patch Tuesday2024 April Patch Tuesday
2024 April Patch Tuesday
 
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
The Future Roadmap for the Composable Data Stack - Wes McKinney - Data Counci...
 
Scale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL RouterScale your database traffic with Read & Write split using MySQL Router
Scale your database traffic with Read & Write split using MySQL Router
 
DevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platformsDevEX - reference for building teams, processes, and platforms
DevEX - reference for building teams, processes, and platforms
 
A Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software DevelopersA Journey Into the Emotions of Software Developers
A Journey Into the Emotions of Software Developers
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
Take control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test SuiteTake control of your SAP testing with UiPath Test Suite
Take control of your SAP testing with UiPath Test Suite
 
Testing tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examplesTesting tools and AI - ideas what to try with some tool examples
Testing tools and AI - ideas what to try with some tool examples
 
How to write a Business Continuity Plan
How to write a Business Continuity PlanHow to write a Business Continuity Plan
How to write a Business Continuity Plan
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyesAssure Ecommerce and Retail Operations Uptime with ThousandEyes
Assure Ecommerce and Retail Operations Uptime with ThousandEyes
 
Decarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a realityDecarbonising Buildings: Making a net-zero built environment a reality
Decarbonising Buildings: Making a net-zero built environment a reality
 
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance[Webinar] SpiraTest - Setting New Standards in Quality Assurance
[Webinar] SpiraTest - Setting New Standards in Quality Assurance
 

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.