Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.
Lisp Macros
                   in 20 minutes
                           (featuring ‘clojure)

                            ...
Clojure
Clojure


•homoiconic
•fairly functional
•runtime polymorphism
•jvm language
•software transactional memory
•agent-based a...
Clojure


•homoiconic
•fairly functional
•runtime polymorphism
•jvm language
•software transactional memory
•agent-based a...
Clojure


•homoiconic
•fairly functional
•runtime polymorphism
•jvm language
    Code is Data
•software transactional memo...
Example:
LINQ Envy
C#


    string[] names = { quot;Burkequot;, quot;Connorquot;, quot;Frankquot;,
                       quot;Everettquot;, ...
Java - Quaere


String[] names={quot;Burkequot;, quot;Connorquot;, quot;Frankquot;,
                       quot;Everettquo...
Ruby - Quick Hack


names = [quot;Burkequot;, quot;Connorquot;, quot;Frankquot;,
         quot;Everettquot;, quot;Albertqu...
Clojure


(def names '(quot;Burkequot;, quot;Connorquot;, quot;Frankquot;,
                       quot;Everettquot;, quot;...
Ruby Hack - Implementation
class Parameter                                            def from(binding, &spec)
  def metho...
Clojure - Implementation




(defmacro from [var _ coll _ condition _ ordering _ desired-map]
  `(map (fn [~var] ~desired-...
Code is Data
                     Data is Code
 “         InfoQ: [...] many modern programming languages like Ruby are cla...
Everything is
  a (List)
(1 2 3 4 5)


   (+ 1 2)


(+ (- 3 2) 10)
List


{
(1 2 3 4 5)


  Number
List


           {
       (+ 1 2)


Function     Number
List

     {
     (+ (- 3 2) 10)

       {   List
Function          Number
{
           (defn- run-variant[variant]
             (let [result
               (wrap-and-run


List
                 (:...
Code is Data
Data is Code
Example:
Implementing
     If
(defn they-are-the-same []
  (println quot;They are the same!quot;))

(defn they-are-different []
  (println quot;They are...
First Try: Function



(defn my-if [condition succ fail]
  (cond
   condition succ
   :else fail))



user>
;;;; (my-if (=...
Second Try: Macro



(defmacro my-if [condition succ fail]
  (cond
   condition succ
   :else fail))



user>
;;;; (my-if ...
Why? Dump Function Arguments



(defn my-if [condition succ fail]
  (println quot;Parameters are: quot; condition
succ fai...
Why? Dump Macro Arguments



(defmacro my-if [condition succ fail]
  (println quot;Parameters are: quot; condition
succ fa...
Macro Expansion



(println (macroexpand-1 '(my-if (= 2 2)
		         (they-are-the-same)
		         (they-are-different))...
(my-if (= 2 2)
       (they-are-the-same)
       (they-are-different))




(defmacro my-if [condition succ fail]
  (cond
 ...
Revisiting
  LINQ
Clojure - Implementation




(defmacro from [var _ coll _ condition _ ordering _ desired-map]
  `(map (fn [~var] ~desired-...
(def query
      (from n in names
      where (= (. n length) 5)
      orderby n
      select (. n toUpperCase)))



(defm...
More?
http://www.pragprog.com/titles/shcloj/
programming-clojure

http://www.lisperati.com/casting.html

http://groups.goo...
Lisp Macros in 20 Minutes (Featuring Clojure)
Lisp Macros in 20 Minutes (Featuring Clojure)
Upcoming SlideShare
Loading in …5
×
Upcoming SlideShare
Codemash-Clojure.pdf
Next
Download to read offline and view in fullscreen.

Share

Lisp Macros in 20 Minutes (Featuring Clojure)

Download to read offline

"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/

Related Books

Free with a 30 day trial from Scribd

See all

Related Audiobooks

Free with a 30 day trial from Scribd

See all

Lisp Macros in 20 Minutes (Featuring Clojure)

  1. 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. 2. Clojure
  3. 3. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language •software transactional memory •agent-based asynchronous concurrency
  4. 4. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language •software transactional memory •agent-based asynchronous concurrency
  5. 5. Clojure •homoiconic •fairly functional •runtime polymorphism •jvm language Code is Data •software transactional memory •agent-based asynchronous concurrency Data is Code
  6. 6. Example: LINQ Envy
  7. 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. 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. 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. 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. 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. 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. 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. 14. Everything is a (List)
  15. 15. (1 2 3 4 5) (+ 1 2) (+ (- 3 2) 10)
  16. 16. List { (1 2 3 4 5) Number
  17. 17. List { (+ 1 2) Function Number
  18. 18. List { (+ (- 3 2) 10) { List Function Number
  19. 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. 20. Code is Data Data is Code
  21. 21. Example: Implementing If
  22. 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. 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. 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. 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. 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. 27. Macro Expansion (println (macroexpand-1 '(my-if (= 2 2) (they-are-the-same) (they-are-different))) user> user> (they-are-the-same)
  28. 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. 29. Revisiting LINQ
  30. 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. 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. 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
  • AnilkumarPeri

    Aug. 27, 2017
  • nutron3

    May. 24, 2016
  • ifuleyou49

    Feb. 5, 2016
  • feiwong50767

    Jul. 16, 2015
  • raw_anurag

    Jul. 30, 2014
  • hiteshsharma0102

    Jul. 30, 2014
  • lninyo

    Aug. 7, 2013
  • vmlinz

    Apr. 18, 2013
  • doppioslash

    Jan. 26, 2013
  • imflop

    Feb. 23, 2012
  • CarlosSantilliFilho

    Feb. 7, 2012
  • trifly

    Jan. 17, 2012
  • stonegao

    Oct. 4, 2011
  • seymores

    Sep. 29, 2011
  • danielqo

    Aug. 4, 2011
  • yyfrankyy

    Jun. 26, 2011
  • caronchen

    Jan. 11, 2011
  • mrluanma

    Nov. 13, 2010
  • betterk

    Oct. 26, 2010
  • khasan

    Aug. 13, 2010

"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/

Views

Total views

22,070

On Slideshare

0

From embeds

0

Number of embeds

2,235

Actions

Downloads

409

Shares

0

Comments

0

Likes

31

×