SlideShare a Scribd company logo
1 of 105
Don’t Drive on the
Railroad Tracks
Eugene Wallingford
University of Northern Iowa
November 17, 2010
Thursday, November 18, 2010
In the small, you know this.
It is no big deal.
In the large, this is different.
It changes how you think
about problems and data.
Two Claims
Thursday, November 18, 2010
you know this
Thursday, November 18, 2010
def addSalesTax( price )
price * 1.07
end
Thursday, November 18, 2010
def addSalesTax( price )
price = price * 1.07
end
Thursday, November 18, 2010
def addSalesTax( price )
price = price * 1.07
end
X
Thursday, November 18, 2010
def addSalesTax( price )
tax = price * 0.07
price = price + tax
end
Thursday, November 18, 2010
def addSalesTax( price )
tax = price * 0.07
price = price + tax
end X
Thursday, November 18, 2010
side effects
Thursday, November 18, 2010
side effectsX
Thursday, November 18, 2010
Thursday, November 18, 2010
Thursday, November 18, 2010
Thursday, November 18, 2010
def addSalesTax( price )
price * 1.07
end
Thursday, November 18, 2010
sort -m access01-ips access02-ips 
| uniq -d 
| wc -l
Thursday, November 18, 2010
wc(“-l”,
uniq(“-d”,
sort(“-m”,
access01-ips,
access02-ips)))
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
["a","b","c","d"]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
["a","b","c","d"]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
zip( ["a","b","c","d"] )
Thursday, November 18, 2010
[[1, "a"],
[2, "b"],
[3, "c"],
[4, "d"]]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
{ |x| x.odd? }
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
[ 1 , 3 ]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
select { |x| x.odd? }
Thursday, November 18, 2010
[ 1, 3 ]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
{ |x| x.odd? }
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
[ 1 , 2 , 3 , 4 ]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
partition { |x| x.odd? }
Thursday, November 18, 2010
[ [ 1, 3 ], [ 2, 4 ] ]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
{ |x| x * x }
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
[ 1 , 4 , 9 , 16 ]
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
map { |x| x * x }
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
[ 1 , 4 , 9 , 16 ]
^2^2^2^2
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ]
1 + 2 + 3 + 4 => 10
Thursday, November 18, 2010
1 + 2 + 3 + 4
==
((1 + 2) + 3) + 4
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
inject { |x,y| x + y }
Thursday, November 18, 2010
[ 1 , 2 , 3 , 4 ].
inject { |x,y| x + y }
fold the list with +
Thursday, November 18, 2010
{ |x| x.odd? }
{ |x| x * x }
{ |x,y| x + y }
Thursday, November 18, 2010
functions
are
first-class values
Thursday, November 18, 2010
# Python
for item in iterable_collection:
# do something with item
# Ruby
set.each do |item|
# do something with item
end
Thursday, November 18, 2010
next steps
Thursday, November 18, 2010
implies
recursion
over
persistent
data structures
Thursday, November 18, 2010
number ::= 0
| 1 + number
Thursday, November 18, 2010
list ::= empty
| item + list
Thursday, November 18, 2010
tree ::= empty
| item + tree + tree
Thursday, November 18, 2010
induction
implies
recursion
Thursday, November 18, 2010
what
versus
how
Thursday, November 18, 2010
number ::= 0
| 1 + number
if n = 0
do something
else
solve for 1
solve for n-1
combine
Thursday, November 18, 2010
number ::= 0
| 1 + number
Thursday, November 18, 2010
number ::= 0
| 1 + number
decrease and conquer
Thursday, November 18, 2010
number ::= 0
| 1 + number
sequential
Thursday, November 18, 2010
number ::= 0
| number/2
+
number/2
Thursday, November 18, 2010
number ::= 0
| number/2
+
number/2
divide and conquer
Thursday, November 18, 2010
number ::= 0
| number/2
+
number/2
parallel
Thursday, November 18, 2010
tree ::= empty
| item + tree + tree
divide and conquer
parallel
Thursday, November 18, 2010
MapReduce
map an operator
over each item
reduce (fold)
the resulting list
Thursday, November 18, 2010
[ 8, 4, 1, 6, 7, 2, 5, 3 ]
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
Thursday, November 18, 2010
[ 8, 4, 1, 6, 7, 2, 5, 3 ].
map { |x| [x] }
[[8], [4], [1], [6],
[7], [2], [5], [3]]
make a list of each item
Thursday, November 18, 2010
[[8], [4], [1], [6],
[7], [2], [5], [3]].
inject { |x,y| merge(x,y) }
merge the sorted lists, pairwise
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
Thursday, November 18, 2010
[ 8, 4, 1, 6, 7, 2, 5, 3 ]
.map { |x| [x] }
.inject { |x,y| merge(x,y) }
map/reduce
[ 1, 2, 3, 4, 5, 6, 7, 8 ]
Thursday, November 18, 2010
Implications for Parallelism
merge(a,b) == merge(b,a)
&&
merge(a, merge(b,c))
==
merge(merge(a, b),c)
merges can be done independently
Thursday, November 18, 2010
Thursday, November 18, 2010
really getting it
Thursday, November 18, 2010
class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
end
Thursday, November 18, 2010
class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
end
Thursday, November 18, 2010
class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
end
Thursday, November 18, 2010
class Proc
def self.compose(f, g)
lambda { |*args| f[g[*args]] }
end
end
combinator
Thursday, November 18, 2010
A combinator is a function
that takes functions as input
and computes its result
by composing those functions. *
* and nothing else.
There are no free variables.
Thursday, November 18, 2010
combinator is to functional programming
as
framework is to object-oriented programming
Thursday, November 18, 2010
combinator is to functional programming
as
framework is to object-oriented programming
the next level of abstraction
Thursday, November 18, 2010
A Common Pattern...
widget.collection
.select { |a_table|
a_table.widgets_column_name =~ regex }
.map { |a_table|
widget.attribute_present?(a_table.widgets_column_name) &&
{ a_table.label
=> widget.send(a_table.widgets_column_name) }
|| {} }
.inject(&:merge)
Thursday, November 18, 2010
Combinators in Action
suppose we want to find
the square of the sum
of all the odd numbers
between 1 and 100
Thursday, November 18, 2010
(1..100)
Thursday, November 18, 2010
(1..100).select(&:odd?)
Thursday, November 18, 2010
(1..100).select(&:odd?).inject(&:+)
Thursday, November 18, 2010
lambda { |x| x * x }.call(
(1..100).select(&:odd?).inject(&:+))
Thursday, November 18, 2010
lambda { |x| x * x }.call(
(1..100).select(&:odd?).inject(&:+))
Thursday, November 18, 2010
A permuting combinator
composes two functions
in reverse order.
Instead of f(g(x), we want g(f(x)).
Thursday, November 18, 2010
(1..100).select(&:odd?).inject(&:+)
.callWithSelf(lambda { |x| x * x })
Thursday, November 18, 2010
(1..100).select(&:odd?).inject(&:+)
.into (lambda { |x| x * x })
Thursday, November 18, 2010
(1..100)
.select(&:odd?)
.inject(&:+)
.into(lambda { |x| x * x })
Thursday, November 18, 2010
class Object
def into expr = nil
expr.nil? ? yield(self) : expr.to_proc.call(self)
end
end
Thursday, November 18, 2010
Um, what about Scala?
Thursday, November 18, 2010
case class Thrush[A](x: A) {
def into[B](g: A => B): B = {
g(x)
}
}
Thursday, November 18, 2010
Thrush((1 to 100)
.filter(_ % 2 != 0)
.foldLeft(0)(_ + _))
.into((x: Int) => x * x)
Thursday, November 18, 2010
accounts
.filter(_ belongsTo "John S.")
.map(_.calculateInterest)
.filter(_ > threshold)
.foldLeft(0)(_ + _)
.into {x: Int =>
updateBooks journalize
(Ledger.INTEREST, x)
}
Thursday, November 18, 2010
Thursday, November 18, 2010
more?
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Mutual Recursion
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Mutual Recursion
Accumulator Passing
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Mutual Recursion
Accumulator Passing
Local Procedure
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Mutual Recursion
Accumulator Passing
Local Procedure
Program Derivation
Thursday, November 18, 2010
functional design patterns
Structural Recursion
Interface Procedure
Mutual Recursion
Accumulator Passing
Local Procedure
Program Derivation
Tail-Recursive State Machine
Continuation Passing
Control Abstraction
Thursday, November 18, 2010
Isn’t all this recursion
so inefficient
as to be impractical?
Thursday, November 18, 2010
This is the 21st century.
Thursday, November 18, 2010
garbage collection
Thursday, November 18, 2010
tail-call elimination
Thursday, November 18, 2010
def foo(...) = {
if (n is base case)
return some value
else
foo(...)
}
Thursday, November 18, 2010
<Scheme indulgence>
Thursday, November 18, 2010
def factorial(n: Int) = {
def loop(n: Int, acc: Int): Int =
if (n <= 0)
acc
else
loop(n - 1, acc * n)
loop(n, 1)
}
Thursday, November 18, 2010
return ’done
Thursday, November 18, 2010
If I had asked people
what they wanted,
they would have said
'faster horses'.
Henry Ford
Thursday, November 18, 2010
An invention has to make
sense in the world
in which it is finished,
not the world
in which it was started.
Ray Kurzweil
Thursday, November 18, 2010
http://www.youtube.com/watch?v=c_5GpBgsang
http://weblog.raganwald.com/2008/01/no-detail-too-small.html
http://debasishg.blogspot.com/2009/09/thrush-combinator-in-scala.html
http://fupeg.blogspot.com/2009/04/tail-recursion-in-scala.html
http://www.cs.uni.edu/~wallingf/patterns/recursion.html
http://www.cs.uni.edu/~wallingf/patterns/envoy.pdf
http://mitpress.mit.edu/sicp/
http://sicpinclojure.com/
resources to study
Thursday, November 18, 2010

More Related Content

Recently uploaded

Recently uploaded (20)

Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
Mastering MySQL Database Architecture: Deep Dive into MySQL Shell and MySQL R...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Handwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed textsHandwritten Text Recognition for manuscripts and early printed texts
Handwritten Text Recognition for manuscripts and early printed texts
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
Apidays Singapore 2024 - Building Digital Trust in a Digital Economy by Veron...
 
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...Workshop - Best of Both Worlds_ Combine  KG and Vector search for  enhanced R...
Workshop - Best of Both Worlds_ Combine KG and Vector search for enhanced R...
 
Strategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a FresherStrategies for Landing an Oracle DBA Job as a Fresher
Strategies for Landing an Oracle DBA Job as a Fresher
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time AutomationFrom Event to Action: Accelerate Your Decision Making with Real-Time Automation
From Event to Action: Accelerate Your Decision Making with Real-Time Automation
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Tech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdfTech Trends Report 2024 Future Today Institute.pdf
Tech Trends Report 2024 Future Today Institute.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?A Year of the Servo Reboot: Where Are We Now?
A Year of the Servo Reboot: Where Are We Now?
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
Boost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivityBoost PC performance: How more available memory can improve productivity
Boost PC performance: How more available memory can improve productivity
 

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

Dont Drive on the Railroad Tracks