SlideShare a Scribd company logo
1 of 78
Download to read offline
Purely Functional
Data Structures
Demystified
Mohit Thatte
Functional Conf 2014, Bangalore
About Me
Work
The great outdoors
@mohitthatte
github.com/pastafari
Why?
What?
Learnings
Image: User:Matthew,Wikimedia Commons
Programs that use Maps
Towers of Abstraction
Map as ADT
Map implementation
Primitives…
Image: https://www.flickr.com/photos/cverdier/4321365261
Any sufficiently advanced
data structure
is indistinguishable from magic
With apologies to Arthur C. Clarke!
Why?
What?
Learnings
Data Structures
AbstractionsData
Abstract Data Type
Invariants
Stack
push, pop
LIFO
Name
Operations
ADT
Image: Apollo 8,NASA
Change perspective
Think like a
data structure designer
The ADT Challenge
Given an ADT,
give a correct implementation of all operations.
Source: https://www.flickr.com/photos/manicstreetpreacher/5310984196/
“You can program in any
language you want…
as long as it is imperative”
make it easier to reason about code
Why Functional?
Immutability Persistence
x = 5
y = x + 1
pr!nt x
x = [1,2]
y = x << 3
pr!nt x
5 is immutable
[1,2] is NOT!
x = [1,2]
foo(x)
x
("et [x [1 2]]
(foo x)
x)
What is x now?
What is x now?
The ADT Challenge
Given an ADT,
give a correct and functional implementation
of all operations.
Performance matters too.
A lot.
The ADT Challenge
Correct
Performant
Functional
X
The ADT Challenge
Given an ADT,
give a correct, functional and performant
implementation of all operations.
Challenge Accepted!
Why?
What?
Learnings
3 -2
3
-2
The Pair
cons (def x (cons 3 -2))
(head x)
(ta!" x)
3 -2
head
ta!"
Cons, the Magnificent
(defn ta!" [z]
(z (fn [p q] q)))
(defn head [z]
(z (fn [p q] p)))
(defn cons [x y]
(fn [m] (m x y)))
Voila, a list!
3 2 1 nil
(cons 3 (cons 2 (cons 1 n!")))
And so it goes…
Pair List Sets …
A functional ‘update’
3 2 1 nil
3 4
Structural Sharing
Queue ADT
enqueue
peek
dequeue
append an e"ement
f!rst e"ement
rema!n!ng e"ements
Invar!ant
FIFO
Operat!ons
Queue ADT
Ideas?
Structural Decomposition
Use a List
Pattern
Matching
Correct
Functional
Performant
Use 2 lists…
Be clever!
Correct
Functional
Performant
Amortized Analysis
Care about a sequence of operations
rather than worst case
Earn credits each time you don’t do an expensive
operation, spend credits when you do
Why?
What?
Learnings
Map ADT
put
get
assoc!ate a key w!th va"ue
"ookup key, return va"ue
de"ete remove key & va"ue
Invar!ant
Operat!ons
what you put !s what you get
Map ADT
Ideas?
Use a list?
Nope. Too slow.
Use a Tree
Yep!
Source: Wikipedia
All is well…
13
15
17
21
11
9 12
13
… until this happens
15
17
21
स"तuलन का होना ब,त ज़.री 1 !
Balance is essential
Red Black Tree
No red node can have a red child
Every path from root to an empty node
contains the same number of black nodes
Guibas, Sedgewick 1978
Each node is colored Red or Black
Red Black Tree
Source: Wikipedia
Red Black Tree
Source: Wikipedia
7
Red Black Tree
Source: Wikipedia
7
Red Black Tree
Source: Wikipedia
7
Red Black Tree
Source: Wikipedia
7
Red Black Tree
Source: Wikipedia
7
Red Black Tree
Source: Wikipedia
7
z
a b
c
d
a b
y
x
y
x z
c d
y
x
z
x
y
z
a
b c
d
a
b
c d
a
b c
d
y
z
x
Pattern Matching is awesome!
Persistent
Bit-Partitioned
Vector Trie
Phil Bagwell, Rich Hickey
Source: Wikipedia
Image: Jean Niklas L’orange http://hypirion.com/musings/understanding-persistent-vector-pt-1
Why?
What?
Learnings
Abstraction is vital
Study implementations
to understand
tradeoffs.
Structural
Decomposition
Structural
Sharing
Amortized Analysis
!
Laziness
Extreme
Cleverness
DJ Spiewak	©
Further study
Persistent vectors in Clojure
http://hypirion.com/musings/understanding-persistent-vector-pt-1
Red Black Trees
http://www.eternallyconfuzzled.com/tuts/datastructures/jsw_tut_rbtree.aspx
Julienne Walker
Persistent Data Structures
http://debasishg.blogspot.in/2010/06/functional-data-structures-in.html
Coursera
https://www.coursera.org/course/algo
Algorithms, Tim Roughgarden
https://www.coursera.org/course/algs4partI
Algorithms, Wayne &Sedgewick

More Related Content

Similar to Purely functional data structures demystified

How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...
Eugene Kirpichov
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
ecomputernotes
 

Similar to Purely functional data structures demystified (20)

Using Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigDataUsing Topological Data Analysis on your BigData
Using Topological Data Analysis on your BigData
 
How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...How my visualization tools use little memory: A tale of incrementalization an...
How my visualization tools use little memory: A tale of incrementalization an...
 
Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987Compiler2016 by abcdabcd987
Compiler2016 by abcdabcd987
 
Mining social data
Mining social dataMining social data
Mining social data
 
Social Network Analysis Introduction including Data Structure Graph overview.
Social Network Analysis Introduction including Data Structure Graph overview. Social Network Analysis Introduction including Data Structure Graph overview.
Social Network Analysis Introduction including Data Structure Graph overview.
 
AET vs. AED: Unsupervised Representation Learning by Auto-Encoding Transforma...
AET vs. AED: Unsupervised Representation Learning by Auto-Encoding Transforma...AET vs. AED: Unsupervised Representation Learning by Auto-Encoding Transforma...
AET vs. AED: Unsupervised Representation Learning by Auto-Encoding Transforma...
 
computer notes - Data Structures - 5
computer notes - Data Structures - 5computer notes - Data Structures - 5
computer notes - Data Structures - 5
 
Functional Programming with Immutable Data Structures
Functional Programming with Immutable Data StructuresFunctional Programming with Immutable Data Structures
Functional Programming with Immutable Data Structures
 
Functional Programming
Functional ProgrammingFunctional Programming
Functional Programming
 
Machine learning for document analysis and understanding
Machine learning for document analysis and understandingMachine learning for document analysis and understanding
Machine learning for document analysis and understanding
 
Building Interpretable & Secure AI Systems using PyTorch
Building Interpretable & Secure AI Systems using PyTorchBuilding Interpretable & Secure AI Systems using PyTorch
Building Interpretable & Secure AI Systems using PyTorch
 
LASTconf 2018 - System Mapping: Discover, Communicate and Explore the Real Co...
LASTconf 2018 - System Mapping: Discover, Communicate and Explore the Real Co...LASTconf 2018 - System Mapping: Discover, Communicate and Explore the Real Co...
LASTconf 2018 - System Mapping: Discover, Communicate and Explore the Real Co...
 
From Data to Visualization, what happens in between?
From Data to Visualization, what happens in between?From Data to Visualization, what happens in between?
From Data to Visualization, what happens in between?
 
Computer notes - Josephus Problem
Computer notes - Josephus ProblemComputer notes - Josephus Problem
Computer notes - Josephus Problem
 
Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402Gridforum David De Roure Newe Science 20080402
Gridforum David De Roure Newe Science 20080402
 
A general introduction to Spring Data / Neo4J
A general introduction to Spring Data / Neo4JA general introduction to Spring Data / Neo4J
A general introduction to Spring Data / Neo4J
 
Spark Application Development Made Easy
Spark Application Development Made EasySpark Application Development Made Easy
Spark Application Development Made Easy
 
2 Years of Real World FP at REA
2 Years of Real World FP at REA2 Years of Real World FP at REA
2 Years of Real World FP at REA
 
Large Components in the Rearview Mirror
Large Components in the Rearview MirrorLarge Components in the Rearview Mirror
Large Components in the Rearview Mirror
 
Functional Programming in Go
Functional Programming in GoFunctional Programming in Go
Functional Programming in Go
 

Recently uploaded

Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
panagenda
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Safe Software
 

Recently uploaded (20)

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
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
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
 
MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
Emergent Methods: Multi-lingual narrative tracking in the news - real-time ex...
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
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
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 

Purely functional data structures demystified