SlideShare une entreprise Scribd logo
1  sur  29
Télécharger pour lire hors ligne
Bending Clojure to your Will:
Macros and Domain Specific
Languages
CUFP - Boston, 2013
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
Thursday, 19 September 13
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
• Thoughtworker
• Functional Programming enthusiast
• Clojure Evangelist
• Founder & Organiser of the Sydney
Clojure User Group (clj-syd)
• World traveller
• Fan of Murray’s Beers :)
about:me
Thursday, 19 September 13
Why macros?
Thursday, 19 September 13
It’s fun
Thursday, 19 September 13
Thursday, 19 September 13
It’s powerful
Thursday, 19 September 13
Thursday, 19 September 13
It’s mind bending
Thursday, 19 September 13
Thursday, 19 September 13
If you give someone Fortran, he has Fortran.
If you give someone Lisp, he has any language he pleases.
- Guy Steele
Thursday, 19 September 13
But what are macros?
Thursday, 19 September 13
•Data is code is data
•Programs that write programs
•Magic happens at macro-expansion time
•Most control structures in Clojure are built out of macros
But what are macros?
Thursday, 19 September 13
Macro-expansion time
(defmacro arg-logger [& args]
(prn "Called with: " args)
`(do ~@args))
Thursday, 19 September 13
Macro-expansion time
(arg-logger
(+ 2 3))
Every usage of a macro
(do (+ 2 3))
Gets replaced with its expansion
prior to compilation
Thursday, 19 September 13
Runtime
(arg-logger
(+ 2 3))
;;"Called with: " ((+ 2 3))
Arguments are handed into macros unevaluated
Thursday, 19 September 13
More on that later
Thursday, 19 September 13
Debugging macros
(macroexpand '(cond true
"true"
:else "false"))
;;(if true "true" (clojure.core/cond :else "false"))
Thursday, 19 September 13
Debugging macros
(require '[clojure.walk :as w])
(w/macroexpand-all '(cond true
"true"
:else "false"))
;;(if true "true" (if :else "false" nil))
Thursday, 19 September 13
Quoting
Prevents evaluation
(def my-list (1 2 3))
;java.lang.Integer cannot be cast to clojure.lang.IFn
(def my-list '(1 2 3)) ;Success!
'my-list ;my-list
'(1 2 3) ;(1 2 3)
Thursday, 19 September 13
Syntax-quote
Automatically qualifies all unqualified symbols
`my-list ; user/my-list
`prn ; clojure.core/prn
Note the backtick!
Thursday, 19 September 13
Unquote
Evaluates some forms in a quoted expression
Before unquoting...
`(map even? my-list)
;;(clojure.core/map clojure.core/even? user/my-list)
After...
`(map even? '~my-list)
;;(clojure.core/map clojure.core/even? (quote (1 2 3)))
Thursday, 19 September 13
Unquote-splicing
“Unpacks” a sequence
Before unquote-splicing...
`(+ ~my-list) ;;(clojure.core/+ (1 2 3))
(eval `(+ ~my-list)) ;;java.lang.Integer cannot be cast
to clojure.lang.IFn
Thursday, 19 September 13
Unquote-splicing
After...
`(+ ~@my-list) ;;(clojure.core/+ 1 2 3)
(eval `(+ ~@my-list)) ;;6
Thursday, 19 September 13
All of these are useful inside
macros!
Thursday, 19 September 13
Jam time!
Thursday, 19 September 13
Jam time!
•Get the code - http://bit.ly/cufp-2013-macros
•Make sure you have leiningen 2.x installed
•Run $lein midje or lein midje :autotest from the project root
•Watch the tests fail!
•Fix them :)
Thursday, 19 September 13
Summary
(defmacro macro-name [& args] ...)Defining macros
(def my-list '(1 2 3))Quoting
`(+ my-list) ; (clojure.core/+ user/my-list)Syntax-quote
`(+ ~my-list) ; (clojure.core/+ (1 2 3))Unquote
(macroexpand '...)
(require '[clojure.walk :as w])
(w/macroexpand-all '...)
Debugging
Unquote-splicing `(+ ~@my-list) ; (clojure.core/+ 1 2 3)
Thursday, 19 September 13
Questions?
Leonardo Borges
@leonardo_borges
www.leonardoborges.com
www.thoughtworks.com
Thursday, 19 September 13
Go deeper
Let Over Lambda - http://bit.ly/let-over-lambda
On Lisp - http://bit.ly/on-lisp-book
Thursday, 19 September 13

Contenu connexe

En vedette

Macros in Clojure
Macros in ClojureMacros in Clojure
Macros in Clojure
sohta
 
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なものClojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
sohta
 

En vedette (15)

Patterns
PatternsPatterns
Patterns
 
Clojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVMClojure: Practical functional approach on JVM
Clojure: Practical functional approach on JVM
 
入門ClojureScript
入門ClojureScript入門ClojureScript
入門ClojureScript
 
Macros in Clojure
Macros in ClojureMacros in Clojure
Macros in Clojure
 
Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012Continuation Passing Style and Macros in Clojure - Jan 2012
Continuation Passing Style and Macros in Clojure - Jan 2012
 
Clojure的魅力
Clojure的魅力Clojure的魅力
Clojure的魅力
 
Clojure概览
Clojure概览Clojure概览
Clojure概览
 
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
Stefan Richter - Writing simple, readable and robust code: Examples in Java, ...
 
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なものClojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
Clojureシンタックスハイライター開発から考えるこれからのlispに必要なもの
 
(map Clojure everyday-tasks)
(map Clojure everyday-tasks)(map Clojure everyday-tasks)
(map Clojure everyday-tasks)
 
Clojure: a LISP for the JVM
Clojure: a LISP for the JVMClojure: a LISP for the JVM
Clojure: a LISP for the JVM
 
Introduction to clojure
Introduction to clojureIntroduction to clojure
Introduction to clojure
 
DSL in Clojure
DSL in ClojureDSL in Clojure
DSL in Clojure
 
プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例プログラミング言語Clojureのニャンパスでの活用事例
プログラミング言語Clojureのニャンパスでの活用事例
 
Clojure from ground up
Clojure from ground upClojure from ground up
Clojure from ground up
 

Similaire à Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013

Engineering culture
Engineering cultureEngineering culture
Engineering culture
Pamela Fox
 
실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3
NAVER D2
 
Codemotion 2013 - presentación cocoa pods
Codemotion  2013 -  presentación cocoa podsCodemotion  2013 -  presentación cocoa pods
Codemotion 2013 - presentación cocoa pods
Jorge Maroto
 
Accessibility presentation at Drupal Government Days
Accessibility presentation at Drupal Government DaysAccessibility presentation at Drupal Government Days
Accessibility presentation at Drupal Government Days
Phase2
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
Kyle Oba
 

Similaire à Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013 (13)

Paranoid Android
Paranoid AndroidParanoid Android
Paranoid Android
 
Engineering culture
Engineering cultureEngineering culture
Engineering culture
 
실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3실시간 웹 협업도구 만들기 V0.3
실시간 웹 협업도구 만들기 V0.3
 
2013-06 - Eclipse Kepler Democamps
2013-06 - Eclipse Kepler Democamps2013-06 - Eclipse Kepler Democamps
2013-06 - Eclipse Kepler Democamps
 
Codemotion 2013 - presentación cocoa pods
Codemotion  2013 -  presentación cocoa podsCodemotion  2013 -  presentación cocoa pods
Codemotion 2013 - presentación cocoa pods
 
Accessibility presentation at Drupal Government Days
Accessibility presentation at Drupal Government DaysAccessibility presentation at Drupal Government Days
Accessibility presentation at Drupal Government Days
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...How to write better code: in-depth best practices for writing readable, simpl...
How to write better code: in-depth best practices for writing readable, simpl...
 
Android Security & Penetration Testing
Android Security & Penetration TestingAndroid Security & Penetration Testing
Android Security & Penetration Testing
 
Clojure basics
Clojure basicsClojure basics
Clojure basics
 
A false digital alibi on mac os x
A false digital alibi on mac os xA false digital alibi on mac os x
A false digital alibi on mac os x
 
Introduction to Option monad in Scala
Introduction to Option monad in ScalaIntroduction to Option monad in Scala
Introduction to Option monad in Scala
 
Going to War Over UX
Going to War Over UXGoing to War Over UX
Going to War Over UX
 

Plus de Leonardo Borges

Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
Leonardo Borges
 
Clouds against the Floods (RubyConfBR2011)
Clouds against the Floods (RubyConfBR2011) Clouds against the Floods (RubyConfBR2011)
Clouds against the Floods (RubyConfBR2011)
Leonardo Borges
 

Plus de Leonardo Borges (20)

Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
Realtime collaboration with Clojure - EuroClojure - Barcelona, 2015
 
Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015Parametricity - #cljsyd - May, 2015
Parametricity - #cljsyd - May, 2015
 
From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019From Java to Parellel Clojure - Clojure South 2019
From Java to Parellel Clojure - Clojure South 2019
 
The algebra of library design
The algebra of library designThe algebra of library design
The algebra of library design
 
Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015Futures e abstração - QCon São Paulo 2015
Futures e abstração - QCon São Paulo 2015
 
Functional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event SystemsFunctional Reactive Programming / Compositional Event Systems
Functional Reactive Programming / Compositional Event Systems
 
High Performance web apps in Om, React and ClojureScript
High Performance web apps in Om, React and ClojureScriptHigh Performance web apps in Om, React and ClojureScript
High Performance web apps in Om, React and ClojureScript
 
Programação functional reativa: lidando com código assíncrono
Programação functional reativa: lidando com código assíncronoProgramação functional reativa: lidando com código assíncrono
Programação functional reativa: lidando com código assíncrono
 
Monads in Clojure
Monads in ClojureMonads in Clojure
Monads in Clojure
 
Intro to Clojure's core.async
Intro to Clojure's core.asyncIntro to Clojure's core.async
Intro to Clojure's core.async
 
Functional Reactive Programming in Clojurescript
Functional Reactive Programming in ClojurescriptFunctional Reactive Programming in Clojurescript
Functional Reactive Programming in Clojurescript
 
Clojure/West 2013 in 30 mins
Clojure/West 2013 in 30 minsClojure/West 2013 in 30 mins
Clojure/West 2013 in 30 mins
 
Clojure Reducers / clj-syd Aug 2012
Clojure Reducers / clj-syd Aug 2012Clojure Reducers / clj-syd Aug 2012
Clojure Reducers / clj-syd Aug 2012
 
The many facets of code reuse in JavaScript
The many facets of code reuse in JavaScriptThe many facets of code reuse in JavaScript
The many facets of code reuse in JavaScript
 
Heroku addons development - Nov 2011
Heroku addons development - Nov 2011Heroku addons development - Nov 2011
Heroku addons development - Nov 2011
 
Clouds against the Floods (RubyConfBR2011)
Clouds against the Floods (RubyConfBR2011) Clouds against the Floods (RubyConfBR2011)
Clouds against the Floods (RubyConfBR2011)
 
Clouds Against the Floods
Clouds Against the FloodsClouds Against the Floods
Clouds Against the Floods
 
Arel in Rails 3
Arel in Rails 3Arel in Rails 3
Arel in Rails 3
 
Testing with Spring
Testing with SpringTesting with Spring
Testing with Spring
 
JRuby in The Enterprise
JRuby in The EnterpriseJRuby in The Enterprise
JRuby in The Enterprise
 

Dernier

Dernier (20)

Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
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
 
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
 
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
 
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
 
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
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
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
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
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...
 
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
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024MINDCTI Revenue Release Quarter One 2024
MINDCTI Revenue Release Quarter One 2024
 
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 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...
 
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
 
GenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdfGenAI Risks & Security Meetup 01052024.pdf
GenAI Risks & Security Meetup 01052024.pdf
 
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?
 
Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
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...
 

Clojure Macros Workshop: LambdaJam 2013 / CUFP 2013