SlideShare ist ein Scribd-Unternehmen logo
1 von 72
Downloaden Sie, um offline zu lesen
Down the (Clojure) 
Rabbit Hole 
FP Days 2014 
@cgrand
Who am I? 
• Independent software dev 
• Early adopter (6+ years) 
• Authored a couple of libs 
• Co-authored Clojure Programming
Where I come from 
• Growing pains: Logo, 8-bit BASIC, Assembly, 
Pascal, C/C++ 
• Enlightenment: Caml Light 
• Dark ages: Entreprise Java
A New Hope 
• Wait, there’s more to Javascript than copy-pasting 
incantations!? 
• 1st class functions? Party on!
It can’t go wrong… 
• Concurrent server-side Javascript 
• Left me with a lot of cicatricial tissue
Wish list 
• Functional 
• Dynamic 
• JVM 
• Meta-programming 
• Concurrency story
Wish list 
✓ Functional 
✓ Dynamic 
✓ JVM 
✓ Meta-programming 
✓ Concurrency story
When the pupil is ready 
to learn, 
a teacher will appear.
A B
A B
A B
A B
Parentheses!
Pragmatic Parentheses! 
• Don’t commit to a syntax 
• Don’t spend time budget on syntax 
• Open to extension 
• Metaprogramming and serialization for free!
Immutability
Persistent Data Structures 
• More than immutable 
• Want! for years 
• But didn’t know the word to look for
What is immutable 
with log ops?
Numbers!
Okasaki and base-10 cubes 
• Persistent Data Structures as 
numeral systems 
• Base-10 cubes
Collections as numbers 
• inc is cons 
• random access 
list 
• log 472 
• elementary 
school material!
Data structures for free 
• Devise a twisted numeral system (no zero, extra 
digits, ambiguous, etc.) 
• Get a persistent data structure for free!
ROMANA 
DATASTRVCTVRA
Strict 2-3 Finger Trees 
• 4 digits: 0, 1, 2, 3 
• Positional AND symmetric 
• 2n+1 digits 
• dk, k ≤ n has weight 2k, k ≥ n has weight 22n-k 
• 0 and 1 only allowed for the most significant digit
Inc by the right 
0 
1 
2 
3 
202 
203 
212 
213 
222 
223 
232 
233 
22022 
22023 
22032 
22033 
22122 
22123
Dec by the left 
22123 
33023 
23023 
32023 
22023 
333 
233 
323 
223 
313 
213 
303 
203 
202 
3 
2 
1 
0
Back to data
Dynamic + Persistent 
• Low ceremony modeling: 
• easily map external data to internal values 
• 1:1 most of the time 
• one model end-to-end 
• or several identical ones…
Evolution 
• As you gain knowledge 
• Introduce layers 
• Recursive process 
• Models for each layer may diverge 
• Converters are enough 
• Must Ignore semantics
Loose Coupling 
• Sharing values ≠ sharing mutable objects 
• Sharing schemas ≠ sharing classes 
• Coupling is caused by behavior and mutability 
• Dumber is better
Who Needs Encapsulation? 
• When things can’t change, why continuously check 
invariants? 
• Use validators to enforce invariants 
• Safe publication 
• How a value is built is of no importance
Beware of Postel’s Law 
Be conservative in what you send, be liberal in what 
you accept. 
This defines de facto validity 
Hard to reverse-engineer, not declarative enough
Modularity 
• Easily serializable values (map, vectors etc.) 
• Low coupling 
• Allows for an easier transition from internal module 
to external service
Data as API
Data as API 
• Blind spots 
• process must be agreed upon out of band 
• process is closed 
• coupling to a version of the process 
• Bring objects and behavior back!
Who needs objects? 
• We have closures! (or the other way round) 
• Let’s put closures in the map! 
• But closures don’t print! 
• Pesky behaviors…
How do you call a system 
that sends data and 
process in-band?
A web server!
Data+Process=HTML 
• Content is data 
• Links and forms define processes 
• Javascript too but is beyond the Turing horizon
Anatomy of a Form 
<form action="http://example.org/xyz" method=post> 
<input type=hidden name=secret value="0xDECAF"> 
<label>First name: <input name=fname></label> 
</form>
Anatomy of a Form 
Function pointer 
<form action="http://example.org/xyz" method=post> 
<input type=hidden name=secret value="0xDECAF"> 
<label>First name: <input name=fname></label> 
</form>
Anatomy of a Form 
Function pointer 
Invocation 
Protocol 
impl. 
<form action="http://example.org/xyz" method=post> 
<input type=hidden name=secret value="0xDECAF"> 
<label>First name: <input name=fname></label> 
</form>
Anatomy of a Form 
Function pointer 
Invocation 
Protocol 
impl. 
<form action="http://example.org/xyz" method=post> 
<input type=hidden name=secret value="0xDECAF"> 
<label>First name: <input name=fname></label> 
</form> 
Closed over 
environment
A form is a closure!
Hypermedia API? 
• Cambrian explosion (HAL, Siren, Uber…) but no 
uptake 
• A good starting point may be to look at closures 
that are good form candidates 
• named arguments 
• partials 
• arguments shuffling/renaming
What’s to be gained? 
• Not only you get data as usual 
• That you can manipulate as you wish from your 
side of the fence 
• But you also get dynamic introspection of the other 
side!
Let’s pretend it exists 
{:todos 
[{:desc "Invoice Client XXX" 
:mark-as-done (form-inspired-serialization-goes-here) 
:delete (form-inspired-serialization-goes-here)} 
{:desc "Work on Enliven" 
:mark-as-done (form-inspired-serialization-goes-here) 
:delete (form-inspired-serialization-goes-here)}] 
:create (form-inspired-serialization-goes-here)} 
• all closures are lifted out of the data and mapped 
by names to generic functions 
• names should be namespaced and shared
Benefits 
• Coupling between producer and consumer is now 
only an agreements on names and schemas 
• The process is dynamic 
• REPL experience 
• Worth exploring: closures as arguments
Abusive Simplifications 
• Using closure for endpoint is an abuse 
• HTTP or synchronicity are not required 
• May apply to a messaging system 
• as long as some fns are adressable
Computation
Sequences 
• Iteration model 
• Should not be confused with collections 
• or only very locally 
• The (original) way to compose computations
Indices are a smell 
• Indices may look efficient 
• but that’s only because we picked data structures 
for which they are
Pet peeve: strings 
• Strings as chunks of 16 bits chars 
• Waste of space because many code points < 256 
• chars are not even characters… 
• Forces you to copy and decode bytes to chars
Pet peeve: strings 
• An abstraction with cursors (local moves/searches) 
but no indexed lookup 
• Would allow to deal with encoded UTF-8 directly 
• AFAIK all algorithms that requires indexed lookup 
(eg Boyer Moore) could work on bytes and be 
composed with encoding
Enliven 
• Work in progress templating library 
• I explored composing the encoding with the 
template: static parts are precomputed as bytes, 
possibly in direct ByteBuffers 
• I even tried composing with gzip
GZON 
• Directly emits compressed JSON 
• Avoids repeated conversions and writes of 
commons expressions (constants, field names, 
templates values) 
• Avoids having to find repetitions right after having 
emitted them 
• Coming to Github soon
Towards Efficiency 
• By avoiding unnecessary copies and allocations 
• By composing/merging computations
In Clojure too 
• Sequences 
• Chunked sequences 
• Reducers 
• Transducers
Efficiency perspectives 
• GPUs 
• Cache-Oblivious data structures
Ordering considered 
harmful
Ordering 
• Incidental vs Essential 
• Incidental ordering breaks local reasoning 
• deeply nested ifs 
• short-circuiting and/or 
• pattern matching clauses
Short-circuiting and/or 
• Refactor with care! 
• Swapping two expressions may: 
• Cause exceptions 
(and (not= x 0) (/ 1 x)) 
• Change the returned value 
(and (pred x) (lookup x))
Short-circuiting and/or 
• You can’t know when order matters 
• The compiler can’t either
Regexes 
=> (re-seq #"a|ab" "abababab") 
("a" "a" "a" "a") 
• Choice is ordered 
• You can’t locally fix a regex
CFG vs RDP 
• L ::= "a" L "a" | "a" 
• Which strings are matched?
CFG vs RDP 
• L ::= "a" L "a" | "a" 
• Which strings are matched? 
• CFG: any string of 2N+1 "a"s 
• RDP: any string of 2^N-1 "a"s
CFG vs RDP 
• L ::= "a" | "a" L "a" 
• Which strings are matched?
CFG vs RDP 
• L ::= "a" | "a" L "a" 
• Which strings are matched? 
• CFG: any string of 2N+1 "a"s 
• RDP: just "a"
miniKanren/core.logic 
• Depending on the ordering of your disjunctions and 
conjunctions... 
• ...your program may run endlessly without ever 
returning a single answer 
• Defaults fixed for disjunction in the draft of the 
Reasoned Schemer 2nd edition and in core.logic 
• Fair conjunction is harder 
• ASP (Answer Set Programming) is really pure
ACId is declarative 
• Associative 
• Ordering is not execution 
• Commutative 
• Accidental ordering should not matter 
• Idempotent 
• Factorisation as an optimisation
ACId in practice 
• I designed Enliven following ACId principles 
• Easier to reason about a template than with Enlive 
• Way faster: as fast as print 
• Point-free
Misc 
• Local state is the danger, global state is fine 
• Nano VMs are fun
Thanks!

Weitere ähnliche Inhalte

Was ist angesagt?

Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey Morenets
Alex Tumanoff
 

Was ist angesagt? (20)

cb streams - gavin pickin
cb streams - gavin pickincb streams - gavin pickin
cb streams - gavin pickin
 
Ruby Xml Mapping
Ruby Xml MappingRuby Xml Mapping
Ruby Xml Mapping
 
David buksbaum a-briefintroductiontocsharp
David buksbaum a-briefintroductiontocsharpDavid buksbaum a-briefintroductiontocsharp
David buksbaum a-briefintroductiontocsharp
 
C++ Actor Model - You’ve Got Mail ...
C++ Actor Model - You’ve Got Mail ...C++ Actor Model - You’ve Got Mail ...
C++ Actor Model - You’ve Got Mail ...
 
Model with actors and implement with Akka
Model with actors and implement with AkkaModel with actors and implement with Akka
Model with actors and implement with Akka
 
Serialization and performance by Sergey Morenets
Serialization and performance by Sergey MorenetsSerialization and performance by Sergey Morenets
Serialization and performance by Sergey Morenets
 
jTransfo quickie at JavaZone 2015
jTransfo quickie at JavaZone 2015jTransfo quickie at JavaZone 2015
jTransfo quickie at JavaZone 2015
 
Recent nlp trends
Recent nlp trendsRecent nlp trends
Recent nlp trends
 
Introduction to JavaScript
Introduction to JavaScriptIntroduction to JavaScript
Introduction to JavaScript
 
2018 12-kube con-ballerinacon
2018 12-kube con-ballerinacon2018 12-kube con-ballerinacon
2018 12-kube con-ballerinacon
 
Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)Serialization (Avro, Message Pack, Kryo)
Serialization (Avro, Message Pack, Kryo)
 
Actors and Threads
Actors and ThreadsActors and Threads
Actors and Threads
 
Invitation to the dark side of Ruby
Invitation to the dark side of RubyInvitation to the dark side of Ruby
Invitation to the dark side of Ruby
 
2CPP04 - Objects and Classes
2CPP04 - Objects and Classes2CPP04 - Objects and Classes
2CPP04 - Objects and Classes
 
Maccro Strikes Back
Maccro Strikes BackMaccro Strikes Back
Maccro Strikes Back
 
Functional programming for the Advanced Beginner
Functional programming for the Advanced BeginnerFunctional programming for the Advanced Beginner
Functional programming for the Advanced Beginner
 
CPP13 - Object Orientation
CPP13 - Object OrientationCPP13 - Object Orientation
CPP13 - Object Orientation
 
Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014Concurrency and Multithreading Demistified - Reversim Summit 2014
Concurrency and Multithreading Demistified - Reversim Summit 2014
 
Actor Patterns and NATS - Boulder Meetup
Actor Patterns and NATS - Boulder MeetupActor Patterns and NATS - Boulder Meetup
Actor Patterns and NATS - Boulder Meetup
 
Persistent Data Structures And Managed References
Persistent Data Structures And Managed ReferencesPersistent Data Structures And Managed References
Persistent Data Structures And Managed References
 

Ähnlich wie FP Days: Down the Clojure Rabbit Hole

VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
Eonblast
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQL
Don Demcsak
 
Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)
Don Demcsak
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
sandeshshahapur
 

Ähnlich wie FP Days: Down the Clojure Rabbit Hole (20)

Voldemort Nosql
Voldemort NosqlVoldemort Nosql
Voldemort Nosql
 
Stig: Social Graphs & Discovery at Scale
Stig: Social Graphs & Discovery at ScaleStig: Social Graphs & Discovery at Scale
Stig: Social Graphs & Discovery at Scale
 
ScalaClean at ScalaSphere 2019
ScalaClean at ScalaSphere 2019ScalaClean at ScalaSphere 2019
ScalaClean at ScalaSphere 2019
 
Data Diffing Based Software Architecture Patterns
Data Diffing Based Software Architecture PatternsData Diffing Based Software Architecture Patterns
Data Diffing Based Software Architecture Patterns
 
Killing Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORMKilling Shark-Riding Dinosaurs with ORM
Killing Shark-Riding Dinosaurs with ORM
 
Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)Spark and cassandra (Hulu Talk)
Spark and cassandra (Hulu Talk)
 
Performance and Abstractions
Performance and AbstractionsPerformance and Abstractions
Performance and Abstractions
 
VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012VoltDB and Erlang - Tech planet 2012
VoltDB and Erlang - Tech planet 2012
 
Jay Kreps on Project Voldemort Scaling Simple Storage At LinkedIn
Jay Kreps on Project Voldemort Scaling Simple Storage At LinkedInJay Kreps on Project Voldemort Scaling Simple Storage At LinkedIn
Jay Kreps on Project Voldemort Scaling Simple Storage At LinkedIn
 
The Return of the Living Datalog
The Return of the Living DatalogThe Return of the Living Datalog
The Return of the Living Datalog
 
RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015RxJS and Reactive Programming - Modern Web UI - May 2015
RxJS and Reactive Programming - Modern Web UI - May 2015
 
Dapper: the microORM that will change your life
Dapper: the microORM that will change your lifeDapper: the microORM that will change your life
Dapper: the microORM that will change your life
 
Intro to Big Data and NoSQL
Intro to Big Data and NoSQLIntro to Big Data and NoSQL
Intro to Big Data and NoSQL
 
Introduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregationIntroduction to CQRS - command and query responsibility segregation
Introduction to CQRS - command and query responsibility segregation
 
How to choose a database
How to choose a databaseHow to choose a database
How to choose a database
 
CPP19 - Revision
CPP19 - RevisionCPP19 - Revision
CPP19 - Revision
 
Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)Big Data (NJ SQL Server User Group)
Big Data (NJ SQL Server User Group)
 
gdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptxgdscWorkShopJavascriptintroductions.pptx
gdscWorkShopJavascriptintroductions.pptx
 
Distributed Model Validation with Epsilon
Distributed Model Validation with EpsilonDistributed Model Validation with Epsilon
Distributed Model Validation with Epsilon
 
Select Stars: A DBA's Guide to Azure Cosmos DB (SQL Saturday Oslo 2018)
Select Stars: A DBA's Guide to Azure Cosmos DB (SQL Saturday Oslo 2018)Select Stars: A DBA's Guide to Azure Cosmos DB (SQL Saturday Oslo 2018)
Select Stars: A DBA's Guide to Azure Cosmos DB (SQL Saturday Oslo 2018)
 

Kürzlich hochgeladen

%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
masabamasaba
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
masabamasaba
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
chiefasafspells
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
shinachiaurasa2
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Medical / Health Care (+971588192166) Mifepristone and Misoprostol tablets 200mg
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
VictoriaMetrics
 

Kürzlich hochgeladen (20)

WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
WSO2CON 2024 - Cloud Native Middleware: Domain-Driven Design, Cell-Based Arch...
 
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
%+27788225528 love spells in Knoxville Psychic Readings, Attraction spells,Br...
 
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
%+27788225528 love spells in new york Psychic Readings, Attraction spells,Bri...
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
%in Rustenburg+277-882-255-28 abortion pills for sale in Rustenburg
 
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
Love witchcraft +27768521739 Binding love spell in Sandy Springs, GA |psychic...
 
The title is not connected to what is inside
The title is not connected to what is insideThe title is not connected to what is inside
The title is not connected to what is inside
 
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
W01_panagenda_Navigating-the-Future-with-The-Hitchhikers-Guide-to-Notes-and-D...
 
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
Abortion Pill Prices Tembisa [(+27832195400*)] 🏥 Women's Abortion Clinic in T...
 
WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?WSO2CON 2024 - Does Open Source Still Matter?
WSO2CON 2024 - Does Open Source Still Matter?
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
Shapes for Sharing between Graph Data Spaces - and Epistemic Querying of RDF-...
 
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
Large-scale Logging Made Easy: Meetup at Deutsche Bank 2024
 
%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto%in Soweto+277-882-255-28 abortion pills for sale in soweto
%in Soweto+277-882-255-28 abortion pills for sale in soweto
 
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park %in ivory park+277-882-255-28 abortion pills for sale in ivory park
%in ivory park+277-882-255-28 abortion pills for sale in ivory park
 
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
WSO2Con2024 - From Code To Cloud: Fast Track Your Cloud Native Journey with C...
 
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdfPayment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
Payment Gateway Testing Simplified_ A Step-by-Step Guide for Beginners.pdf
 
AI & Machine Learning Presentation Template
AI & Machine Learning Presentation TemplateAI & Machine Learning Presentation Template
AI & Machine Learning Presentation Template
 
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
Devoxx UK 2024 - Going serverless with Quarkus, GraalVM native images and AWS...
 
What Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the SituationWhat Goes Wrong with Language Definitions and How to Improve the Situation
What Goes Wrong with Language Definitions and How to Improve the Situation
 

FP Days: Down the Clojure Rabbit Hole

  • 1. Down the (Clojure) Rabbit Hole FP Days 2014 @cgrand
  • 2. Who am I? • Independent software dev • Early adopter (6+ years) • Authored a couple of libs • Co-authored Clojure Programming
  • 3. Where I come from • Growing pains: Logo, 8-bit BASIC, Assembly, Pascal, C/C++ • Enlightenment: Caml Light • Dark ages: Entreprise Java
  • 4. A New Hope • Wait, there’s more to Javascript than copy-pasting incantations!? • 1st class functions? Party on!
  • 5. It can’t go wrong… • Concurrent server-side Javascript • Left me with a lot of cicatricial tissue
  • 6. Wish list • Functional • Dynamic • JVM • Meta-programming • Concurrency story
  • 7. Wish list ✓ Functional ✓ Dynamic ✓ JVM ✓ Meta-programming ✓ Concurrency story
  • 8. When the pupil is ready to learn, a teacher will appear.
  • 9. A B
  • 10. A B
  • 11. A B
  • 12. A B
  • 14. Pragmatic Parentheses! • Don’t commit to a syntax • Don’t spend time budget on syntax • Open to extension • Metaprogramming and serialization for free!
  • 16. Persistent Data Structures • More than immutable • Want! for years • But didn’t know the word to look for
  • 17. What is immutable with log ops?
  • 19. Okasaki and base-10 cubes • Persistent Data Structures as numeral systems • Base-10 cubes
  • 20. Collections as numbers • inc is cons • random access list • log 472 • elementary school material!
  • 21. Data structures for free • Devise a twisted numeral system (no zero, extra digits, ambiguous, etc.) • Get a persistent data structure for free!
  • 23. Strict 2-3 Finger Trees • 4 digits: 0, 1, 2, 3 • Positional AND symmetric • 2n+1 digits • dk, k ≤ n has weight 2k, k ≥ n has weight 22n-k • 0 and 1 only allowed for the most significant digit
  • 24. Inc by the right 0 1 2 3 202 203 212 213 222 223 232 233 22022 22023 22032 22033 22122 22123
  • 25. Dec by the left 22123 33023 23023 32023 22023 333 233 323 223 313 213 303 203 202 3 2 1 0
  • 27. Dynamic + Persistent • Low ceremony modeling: • easily map external data to internal values • 1:1 most of the time • one model end-to-end • or several identical ones…
  • 28. Evolution • As you gain knowledge • Introduce layers • Recursive process • Models for each layer may diverge • Converters are enough • Must Ignore semantics
  • 29. Loose Coupling • Sharing values ≠ sharing mutable objects • Sharing schemas ≠ sharing classes • Coupling is caused by behavior and mutability • Dumber is better
  • 30. Who Needs Encapsulation? • When things can’t change, why continuously check invariants? • Use validators to enforce invariants • Safe publication • How a value is built is of no importance
  • 31. Beware of Postel’s Law Be conservative in what you send, be liberal in what you accept. This defines de facto validity Hard to reverse-engineer, not declarative enough
  • 32. Modularity • Easily serializable values (map, vectors etc.) • Low coupling • Allows for an easier transition from internal module to external service
  • 34. Data as API • Blind spots • process must be agreed upon out of band • process is closed • coupling to a version of the process • Bring objects and behavior back!
  • 35. Who needs objects? • We have closures! (or the other way round) • Let’s put closures in the map! • But closures don’t print! • Pesky behaviors…
  • 36. How do you call a system that sends data and process in-band?
  • 38. Data+Process=HTML • Content is data • Links and forms define processes • Javascript too but is beyond the Turing horizon
  • 39. Anatomy of a Form <form action="http://example.org/xyz" method=post> <input type=hidden name=secret value="0xDECAF"> <label>First name: <input name=fname></label> </form>
  • 40. Anatomy of a Form Function pointer <form action="http://example.org/xyz" method=post> <input type=hidden name=secret value="0xDECAF"> <label>First name: <input name=fname></label> </form>
  • 41. Anatomy of a Form Function pointer Invocation Protocol impl. <form action="http://example.org/xyz" method=post> <input type=hidden name=secret value="0xDECAF"> <label>First name: <input name=fname></label> </form>
  • 42. Anatomy of a Form Function pointer Invocation Protocol impl. <form action="http://example.org/xyz" method=post> <input type=hidden name=secret value="0xDECAF"> <label>First name: <input name=fname></label> </form> Closed over environment
  • 43. A form is a closure!
  • 44. Hypermedia API? • Cambrian explosion (HAL, Siren, Uber…) but no uptake • A good starting point may be to look at closures that are good form candidates • named arguments • partials • arguments shuffling/renaming
  • 45. What’s to be gained? • Not only you get data as usual • That you can manipulate as you wish from your side of the fence • But you also get dynamic introspection of the other side!
  • 46. Let’s pretend it exists {:todos [{:desc "Invoice Client XXX" :mark-as-done (form-inspired-serialization-goes-here) :delete (form-inspired-serialization-goes-here)} {:desc "Work on Enliven" :mark-as-done (form-inspired-serialization-goes-here) :delete (form-inspired-serialization-goes-here)}] :create (form-inspired-serialization-goes-here)} • all closures are lifted out of the data and mapped by names to generic functions • names should be namespaced and shared
  • 47. Benefits • Coupling between producer and consumer is now only an agreements on names and schemas • The process is dynamic • REPL experience • Worth exploring: closures as arguments
  • 48. Abusive Simplifications • Using closure for endpoint is an abuse • HTTP or synchronicity are not required • May apply to a messaging system • as long as some fns are adressable
  • 50. Sequences • Iteration model • Should not be confused with collections • or only very locally • The (original) way to compose computations
  • 51. Indices are a smell • Indices may look efficient • but that’s only because we picked data structures for which they are
  • 52. Pet peeve: strings • Strings as chunks of 16 bits chars • Waste of space because many code points < 256 • chars are not even characters… • Forces you to copy and decode bytes to chars
  • 53. Pet peeve: strings • An abstraction with cursors (local moves/searches) but no indexed lookup • Would allow to deal with encoded UTF-8 directly • AFAIK all algorithms that requires indexed lookup (eg Boyer Moore) could work on bytes and be composed with encoding
  • 54. Enliven • Work in progress templating library • I explored composing the encoding with the template: static parts are precomputed as bytes, possibly in direct ByteBuffers • I even tried composing with gzip
  • 55. GZON • Directly emits compressed JSON • Avoids repeated conversions and writes of commons expressions (constants, field names, templates values) • Avoids having to find repetitions right after having emitted them • Coming to Github soon
  • 56. Towards Efficiency • By avoiding unnecessary copies and allocations • By composing/merging computations
  • 57. In Clojure too • Sequences • Chunked sequences • Reducers • Transducers
  • 58. Efficiency perspectives • GPUs • Cache-Oblivious data structures
  • 60. Ordering • Incidental vs Essential • Incidental ordering breaks local reasoning • deeply nested ifs • short-circuiting and/or • pattern matching clauses
  • 61. Short-circuiting and/or • Refactor with care! • Swapping two expressions may: • Cause exceptions (and (not= x 0) (/ 1 x)) • Change the returned value (and (pred x) (lookup x))
  • 62. Short-circuiting and/or • You can’t know when order matters • The compiler can’t either
  • 63. Regexes => (re-seq #"a|ab" "abababab") ("a" "a" "a" "a") • Choice is ordered • You can’t locally fix a regex
  • 64. CFG vs RDP • L ::= "a" L "a" | "a" • Which strings are matched?
  • 65. CFG vs RDP • L ::= "a" L "a" | "a" • Which strings are matched? • CFG: any string of 2N+1 "a"s • RDP: any string of 2^N-1 "a"s
  • 66. CFG vs RDP • L ::= "a" | "a" L "a" • Which strings are matched?
  • 67. CFG vs RDP • L ::= "a" | "a" L "a" • Which strings are matched? • CFG: any string of 2N+1 "a"s • RDP: just "a"
  • 68. miniKanren/core.logic • Depending on the ordering of your disjunctions and conjunctions... • ...your program may run endlessly without ever returning a single answer • Defaults fixed for disjunction in the draft of the Reasoned Schemer 2nd edition and in core.logic • Fair conjunction is harder • ASP (Answer Set Programming) is really pure
  • 69. ACId is declarative • Associative • Ordering is not execution • Commutative • Accidental ordering should not matter • Idempotent • Factorisation as an optimisation
  • 70. ACId in practice • I designed Enliven following ACId principles • Easier to reason about a template than with Enlive • Way faster: as fast as print • Point-free
  • 71. Misc • Local state is the danger, global state is fine • Nano VMs are fun