SlideShare una empresa de Scribd logo
1 de 78
Descargar para leer sin conexión
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

Más contenido relacionado

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

Último

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
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 

Último (20)

GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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
 
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
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
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
 
Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024Manulife - Insurer Innovation Award 2024
Manulife - Insurer Innovation Award 2024
 
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live StreamsTop 5 Benefits OF Using Muvi Live Paywall For Live Streams
Top 5 Benefits OF Using Muvi Live Paywall For Live Streams
 
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
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
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?
 
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
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
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
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdfUnderstanding Discord NSFW Servers A Guide for Responsible Users.pdf
Understanding Discord NSFW Servers A Guide for Responsible Users.pdf
 
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law DevelopmentsTrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
TrustArc Webinar - Stay Ahead of US State Data Privacy Law Developments
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
HTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation StrategiesHTML Injection Attacks: Impact and Mitigation Strategies
HTML Injection Attacks: Impact and Mitigation Strategies
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 

Purely functional data structures demystified