SlideShare une entreprise Scribd logo
1  sur  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]

Contenu connexe

Tendances

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - ReferenceMohammed Sikander
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 
Operators
OperatorsOperators
OperatorsKamran
 
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)IoT Code Lab
 
Python Basic Operators
Python Basic OperatorsPython Basic Operators
Python Basic OperatorsSoba Arjun
 
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, 2014Reuven Lerner
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionEelco Visser
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScripttmont
 
Introduction to Recursion (Python)
Introduction to Recursion (Python)Introduction to Recursion (Python)
Introduction to Recursion (Python)Thai Pangsakulyanont
 
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 ProgrammingAdam Getchell
 
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 AtencioLuis Atencio
 
GE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingGE8151 Problem Solving and Python Programming
GE8151 Problem Solving and Python ProgrammingMuthu Vinayagam
 
Advance python programming
Advance python programming Advance python programming
Advance python programming Jagdish Chavan
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1Abdul Haseeb
 
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 JavascriptDoug Sparling
 

Tendances (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
 

Similaire à Ruby Functions as Data

Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009David Pollak
 
SeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovySeaJUG March 2004 - Groovy
SeaJUG March 2004 - GroovyTed 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.pdfRahul04August
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basicsmsemenistyi
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Guillaume Laforge
 
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
 
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#Skills Matter
 
Refactoring to Macros with Clojure
Refactoring to Macros with ClojureRefactoring to Macros with Clojure
Refactoring to Macros with ClojureDmitry Buzdin
 
Composition in JavaScript
Composition in JavaScriptComposition in JavaScript
Composition in JavaScriptJosh Mock
 
perl usage at database applications
perl usage at database applicationsperl usage at database applications
perl usage at database applicationsJoe Jiang
 
CoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairCoffeeScript - A Rubyist's Love Affair
CoffeeScript - A Rubyist's Love AffairMark
 
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 EffectiveNaresha K
 
Useful javascript
Useful javascriptUseful javascript
Useful javascriptLei Kang
 
Ruby Language - A quick tour
Ruby Language - A quick tourRuby Language - A quick tour
Ruby Language - A quick touraztack
 
Working effectively with legacy code
Working effectively with legacy codeWorking effectively with legacy code
Working effectively with legacy codeShriKant Vashishtha
 

Similaire à Ruby Functions as Data (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
 

Dernier

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 3652toLead Limited
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...Fwdays
 
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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsRizwan Syed
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenHervé Boutemy
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostZilliz
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 

Dernier (20)

Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365Ensuring Technical Readiness For Copilot in Microsoft 365
Ensuring Technical Readiness For Copilot in Microsoft 365
 
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks..."LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
"LLMs for Python Engineers: Advanced Data Analysis and Semantic Kernel",Oleks...
 
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
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
Scanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL CertsScanning the Internet for External Cloud Exposures via SSL Certs
Scanning the Internet for External Cloud Exposures via SSL Certs
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
DevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache MavenDevoxxFR 2024 Reproducible Builds with Apache Maven
DevoxxFR 2024 Reproducible Builds with Apache Maven
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage CostLeverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
Leverage Zilliz Serverless - Up to 50X Saving for Your Vector Storage Cost
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 

Ruby Functions as Data

  • 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.