Your SlideShare is downloading. ×
0
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Lisp Macros in 20 Minutes (Featuring Clojure)

17,307

Published on

"We just started holding 20 minutes presentations during lunch time in the ThoughtWorks Sydney office. For the first session I gave a not-that-short talk on Lisp macros using Clojure. The slides are …

"We just started holding 20 minutes presentations during lunch time in the ThoughtWorks Sydney office. For the first session I gave a not-that-short talk on Lisp macros using Clojure. The slides are below.
It turns out that 20 minutes is too little time to actually acquire content but I think at least we now have some people interested in how metaprogramming can be more than monkey patching."

http://fragmental.tw/2009/01/20/presentation-slides-macros-in-20-minutes/

Published in: Technology
4 Comments
27 Likes
Statistics
Notes
  • Nice presentation! I do like Clojure.

    Though, I must admit Ruby Hack from your example is pretty bloated.
    It propose 2 variants: http://pastebin.com/qVE37ETW

    1. Yes, it is still 9 lines of code longer than Clojure version.

    2. Alternatively we can write similar 'from' in Ruby just in one line, but then we should call 'from' with proc syntax '-> x {x.upcase}' which is less beautiful.

    Code as data is great concept.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • PDF would be great for me as well
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • As I don't have the Flash runtime, and I don't have Keynote, I too would appreciate a PDF version.
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
  • how to save it as pdf?
       Reply 
    Are you sure you want to  Yes  No
    Your message goes here
No Downloads
Views
Total Views
17,307
On Slideshare
0
From Embeds
0
Number of Embeds
10
Actions
Shares
0
Downloads
365
Comments
4
Likes
27
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide


























































































  • Transcript

    • 1. Lisp Macros in 20 minutes (featuring ‘clojure) phillip calçado http://fragmental.tw http://thoughtworks.com --:-- *LISP Macros in 20 minutes* http://fragmental.tw (Presentation)--------------------------------------------------------
    • 2. Clojure
    • 3. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language •software transactional memory •agent-based asynchronous concurrency
    • 4. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language •software transactional memory •agent-based asynchronous concurrency
    • 5. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language Code is Data •software transactional memory •agent-based asynchronous concurrency Data is Code
    • 6. Example: LINQ Envy
    • 7. C# string[] names = { quot;Burkequot;, quot;Connorquot;, quot;Frankquot;, quot;Everettquot;, quot;Albertquot;, quot;Georgequot;, quot;Harrisquot;, quot;Davidquot; }; IEnumerable<string> query = from n in names where n.Length == 5 orderby n select n.ToUpper(); foreach (string item in query) Console.WriteLine(item); }
    • 8. Java - Quaere String[] names={quot;Burkequot;, quot;Connorquot;, quot;Frankquot;, quot;Everettquot;, quot;Albertquot;, quot;Georgequot;, quot;Harrisquot;, quot;Davidquot;}; Iterable<String> query= from(quot;nquot;).in(names). where(eq(quot;n.length()quot;,5). select(quot;n.toUpperCase()quot;); for (String n: query) { System.out.println(n); }
    • 9. Ruby - Quick Hack names = [quot;Burkequot;, quot;Connorquot;, quot;Frankquot;, quot;Everettquot;, quot;Albertquot;, quot;Georgequot;, quot;Harrisquot;, quot;Davidquot;] query = from :n => names do where n.length => 5 orderby n select n.upcase end query.each{|e| puts e }
    • 10. Clojure (def names '(quot;Burkequot;, quot;Connorquot;, quot;Frankquot;, quot;Everettquot;, quot;Albertquot;, quot;Georgequot;, quot;Harrisquot;, quot;Davidquot;)) (def query (from n in names where (= (. n length) 5) orderby n select (. n toUpperCase))) (doseq [n query] (println n))
    • 11. Ruby Hack - Implementation class Parameter def from(binding, &spec) def method_missing(method, *args) var = binding.keys.first method list = binding.values.last end query = Query.new var end query.instance_eval &spec list.select do |a| class Query a.send(query.condition[:method]) == attr_reader :condition, :criteria, :action query.condition[:value] end.sort do |a,b| def initialize(var) if(query.criteria) singleton_class.send(:define_method, var) a.send(query.criteria) <=> b.send(query.criteria) { Parameter.new } else end a <=> b end def singleton_class; class << self; self; end; end end.map do |a| a.send(query.action) def where(cond) end @condition = {:method => cond.keys.first, :value => end cond.values.last} end def orderby(criteria) @criteria = criteria unless criteria.kind_of? Parameter end def select(action) @action = action end end a <=> b end end.map do |a| a.send(query.action) end end
    • 12. Clojure - Implementation (defmacro from [var _ coll _ condition _ ordering _ desired-map] `(map (fn [~var] ~desired-map) (sort-by (fn[~var] ~ordering) (filter (fn[~var] ~condition) ~coll))))
    • 13. Code is Data Data is Code “ InfoQ: [...] many modern programming languages like Ruby are claiming big influences from Lisp Have you seen those languages or do you have any ideas about the current state of programming languages? McCarthy: [...] I don't know enough for example about Ruby to know in what way it's related to Lisp. Does it use, for example, list structures as data? InfoQ: No. McCarthy: So if you want to compute with sums and products, you have to parse every time? InfoQ: Yes. McCarthy: So, in that respect Ruby still isn't up to where Lisp was in 1960. Adapted From: http://www.infoq.com/interviews/mccarthy-elephant-2000*
    • 14. Everything is a (List)
    • 15. (1 2 3 4 5) (+ 1 2) (+ (- 3 2) 10)
    • 16. List { (1 2 3 4 5) Number
    • 17. List { (+ 1 2) Function Number
    • 18. List { (+ (- 3 2) 10) { List Function Number
    • 19. { (defn- run-variant[variant] (let [result (wrap-and-run List (:impl variant) (:args variant))] (struct-map variant-result :args (:args variant) :result (first result) :exception (second result))))
    • 20. Code is Data Data is Code
    • 21. Example: Implementing If
    • 22. (defn they-are-the-same [] (println quot;They are the same!quot;)) (defn they-are-different [] (println quot;They are different!quot;)) (my-if (= 2 2) (they-are-the-same) (they-are-different))
    • 23. First Try: Function (defn my-if [condition succ fail] (cond condition succ :else fail)) user> ;;;; (my-if (= 2 2) (they-are-the- same) (they-are ... They are the same! They are different!
    • 24. Second Try: Macro (defmacro my-if [condition succ fail] (cond condition succ :else fail)) user> ;;;; (my-if (= 2 2) (they-are-the- same) (they-are ... They are the same!
    • 25. Why? Dump Function Arguments (defn my-if [condition succ fail] (println quot;Parameters are: quot; condition succ fail)) user> user> ;;;; (my-if (= 2 2) (they-are-the- same) (they-are ... They are the same! They are different! Parameters are: true nil nil
    • 26. Why? Dump Macro Arguments (defmacro my-if [condition succ fail] (println quot;Parameters are: quot; condition succ fail)) user> user> ;;;; (My-if (= 2 2) (they-are-the- same) (they-are ... Parameters are: (= 2 2) (they-are-the- same) (they-are-different)
    • 27. Macro Expansion (println (macroexpand-1 '(my-if (= 2 2) (they-are-the-same) (they-are-different))) user> user> (they-are-the-same)
    • 28. (my-if (= 2 2) (they-are-the-same) (they-are-different)) (defmacro my-if [condition succ fail] (cond condition succ :else fail)) (they-are-the-same)
    • 29. Revisiting LINQ
    • 30. Clojure - Implementation (defmacro from [var _ coll _ condition _ ordering _ desired-map] `(map (fn [~var] ~desired-map) (sort-by (fn[~var] ~ordering) (filter (fn[~var] ~condition) ~coll))))
    • 31. (def query (from n in names where (= (. n length) 5) orderby n select (. n toUpperCase))) (defmacro from [var _ coll _ condition _ ordering _ desired-map] `(map (fn [~var] ~desired-map) (sort-by (fn[~var] ~ordering) (filter (fn[~var] ~condition) ~coll)))) (map (fn [n] (. n toUpperCase)) (sort-by (fn [n] n) (filter (fn [n] (= (. n length) 5)) names)))
    • 32. More? http://www.pragprog.com/titles/shcloj/ programming-clojure http://www.lisperati.com/casting.html http://groups.google.com/group/clojure http://www.gigamonkeys.com/book/ http://mitpress.mit.edu/sicp/ http://github.com/pcalcado/fato/tree/master

    ×