SlideShare a Scribd company logo
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]

More Related Content

What's hot

CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
F# Presentation
F# PresentationF# Presentation
F# Presentationmrkurt
 
Operators
OperatorsOperators
Operators
Kamran
 
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 Operators
Soba 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, 2014
Reuven Lerner
 
CS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definitionCS4200 2019 | Lecture 2 | syntax-definition
CS4200 2019 | Lecture 2 | syntax-definition
Eelco Visser
 
Introduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScriptIntroduction to Functional Programming in JavaScript
Introduction to Functional Programming in JavaScript
tmont
 
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
 
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
Miguel Angel Teheran Garcia
 
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
Luis Atencio
 
Advanced C - Part 2
Advanced C - Part 2Advanced 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
Muthu Vinayagam
 
Advance python programming
Advance python programming Advance python programming
Advance python programming
Jagdish Chavan
 
175035 cse lab-05
175035 cse lab-05 175035 cse lab-05
175035 cse lab-05
Mahbubay Rabbani Mim
 
Operators in python
Operators in pythonOperators in python
Operators in python
Prabhakaran V M
 
Python programming workshop session 1
Python programming workshop session 1Python programming workshop session 1
Python programming workshop session 1
Abdul 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
 
Exp 3 3 d422
Exp 3 3  d422Exp 3 3  d422
Exp 3 3 d422
Omkar Rane
 

What's hot (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
 

Viewers also liked

Love yourself
Love yourselfLove yourself
Love yourself
Ycs Yang
 
Desfeta Alcoi
Desfeta AlcoiDesfeta Alcoi
Desfeta Alcoisollutia
 
Insomnia
InsomniaInsomnia
Insomnia
nala
 
Bonusrunde
BonusrundeBonusrunde
Bonusrunde
rogerrabbit
 
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE DESAFÍOS Y OPORTUNIDADES
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE  DESAFÍOS Y OPORTUNIDADESSEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE  DESAFÍOS Y OPORTUNIDADES
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE DESAFÍOS Y OPORTUNIDADES
Francisco J. Estrada Vásquez
 
Paradojas de Nuestro Tiempo
Paradojas de Nuestro TiempoParadojas de Nuestro Tiempo
Paradojas de Nuestro Tiempo
Juan Carlos Fernandez
 
Werken met iMovie
Werken met iMovieWerken met iMovie
Werken met iMovie
icte
 
Apple Awareness
Apple AwarenessApple Awareness
Apple Awareness
icte
 
B Day 2008: Earth vs Heaven
B Day 2008: Earth vs HeavenB Day 2008: Earth vs Heaven
B Day 2008: Earth vs HeavenRobinConley
 
Sistema de Responsabilidad Penal Adolescente
Sistema de Responsabilidad Penal AdolescenteSistema de Responsabilidad Penal Adolescente
Sistema de Responsabilidad Penal Adolescente
Francisco J. Estrada Vásquez
 
Presentation du vocabulaire MeSH
Presentation du vocabulaire MeSHPresentation du vocabulaire MeSH
Presentation du vocabulaire MeSHcedocel
 

Viewers also liked (20)

Paintball
PaintballPaintball
Paintball
 
金融
金融金融
金融
 
Love yourself
Love yourselfLove yourself
Love yourself
 
Cdevass2
Cdevass2Cdevass2
Cdevass2
 
Desfeta Alcoi
Desfeta AlcoiDesfeta Alcoi
Desfeta Alcoi
 
Que Em 2008, Você
Que Em 2008, VocêQue Em 2008, Você
Que Em 2008, Você
 
Iemand1
Iemand1Iemand1
Iemand1
 
Insomnia
InsomniaInsomnia
Insomnia
 
Bonusrunde
BonusrundeBonusrunde
Bonusrunde
 
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE DESAFÍOS Y OPORTUNIDADES
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE  DESAFÍOS Y OPORTUNIDADESSEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE  DESAFÍOS Y OPORTUNIDADES
SEMINARIO LEY DE RESPONSABILIDAD PENAL ADOLESCENTE DESAFÍOS Y OPORTUNIDADES
 
Paradojas de Nuestro Tiempo
Paradojas de Nuestro TiempoParadojas de Nuestro Tiempo
Paradojas de Nuestro Tiempo
 
Werken met iMovie
Werken met iMovieWerken met iMovie
Werken met iMovie
 
Apple Awareness
Apple AwarenessApple Awareness
Apple Awareness
 
B Day 2008: Earth vs Heaven
B Day 2008: Earth vs HeavenB Day 2008: Earth vs Heaven
B Day 2008: Earth vs Heaven
 
Desfeta Alcoi
Desfeta AlcoiDesfeta Alcoi
Desfeta Alcoi
 
Nadal2007
Nadal2007Nadal2007
Nadal2007
 
Wikis
WikisWikis
Wikis
 
virusi
virusivirusi
virusi
 
Sistema de Responsabilidad Penal Adolescente
Sistema de Responsabilidad Penal AdolescenteSistema de Responsabilidad Penal Adolescente
Sistema de Responsabilidad Penal Adolescente
 
Presentation du vocabulaire MeSH
Presentation du vocabulaire MeSHPresentation du vocabulaire MeSH
Presentation du vocabulaire MeSH
 

Similar to Functional techniques in Ruby

Groovy
GroovyGroovy
Groovy
Zen Urban
 
Beginning Scala Svcc 2009
Beginning Scala Svcc 2009Beginning Scala Svcc 2009
Beginning Scala Svcc 2009
David 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.pdf
Rahul04August
 
Javascript Basics
Javascript BasicsJavascript Basics
Javascript Basics
msemenistyi
 
Steady with ruby
Steady with rubySteady with ruby
Steady with ruby
Christopher Spring
 
Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008Groovy Introduction - JAX Germany - 2008
Groovy Introduction - JAX Germany - 2008
Guillaume 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#
Skills Matter
 
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 ClojureDmitry Buzdin
 
Composition in JavaScript
Composition in JavaScriptComposition in JavaScript
Composition in JavaScript
Josh 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 Affair
Mark
 
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
Naresha 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
 

Similar to 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
 

Recently uploaded

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Tobias Schneck
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Ramesh Iyer
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
Bhaskar Mitra
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
Product School
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
Product School
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
DianaGray10
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
DianaGray10
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Inflectra
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Product School
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
Thijs Feryn
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
James Anderson
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
Frank van Harmelen
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
RTTS
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
UiPathCommunity
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Thierry Lestable
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
ThousandEyes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
Laura Byrne
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance
 

Recently uploaded (20)

Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
Kubernetes & AI - Beauty and the Beast !?! @KCD Istanbul 2024
 
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
Builder.ai Founder Sachin Dev Duggal's Strategic Approach to Create an Innova...
 
Search and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical FuturesSearch and Society: Reimagining Information Access for Radical Futures
Search and Society: Reimagining Information Access for Radical Futures
 
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
AI for Every Business: Unlocking Your Product's Universal Potential by VP of ...
 
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
From Siloed Products to Connected Ecosystem: Building a Sustainable and Scala...
 
UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4UiPath Test Automation using UiPath Test Suite series, part 4
UiPath Test Automation using UiPath Test Suite series, part 4
 
Connector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a buttonConnector Corner: Automate dynamic content and events by pushing a button
Connector Corner: Automate dynamic content and events by pushing a button
 
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered QualitySoftware Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
Software Delivery At the Speed of AI: Inflectra Invests In AI-Powered Quality
 
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdfFIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
FIDO Alliance Osaka Seminar: The WebAuthn API and Discoverable Credentials.pdf
 
Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...Designing Great Products: The Power of Design and Leadership by Chief Designe...
Designing Great Products: The Power of Design and Leadership by Chief Designe...
 
Accelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish CachingAccelerate your Kubernetes clusters with Varnish Caching
Accelerate your Kubernetes clusters with Varnish Caching
 
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdfFIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
FIDO Alliance Osaka Seminar: Passkeys at Amazon.pdf
 
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...
 
Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*Neuro-symbolic is not enough, we need neuro-*semantic*
Neuro-symbolic is not enough, we need neuro-*semantic*
 
JMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and GrafanaJMeter webinar - integration with InfluxDB and Grafana
JMeter webinar - integration with InfluxDB and Grafana
 
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...
 
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
Empowering NextGen Mobility via Large Action Model Infrastructure (LAMI): pav...
 
Assuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyesAssuring Contact Center Experiences for Your Customers With ThousandEyes
Assuring Contact Center Experiences for Your Customers With ThousandEyes
 
The Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and SalesThe Art of the Pitch: WordPress Relationships and Sales
The Art of the Pitch: WordPress Relationships and Sales
 
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdfFIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
FIDO Alliance Osaka Seminar: Passkeys and the Road Ahead.pdf
 

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.