Lisp Macros in 20 Minutes (Featuring Clojure)

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    6 Favorites

    Lisp Macros in 20 Minutes (Featuring Clojure) - Presentation 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 = { \"Burke\", \"Connor\", \"Frank\", \"Everett\", \"Albert\", \"George\", \"Harris\", \"David\" }; 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={\"Burke\", \"Connor\", \"Frank\", \"Everett\", \"Albert\", \"George\", \"Harris\", \"David\"}; Iterable<String> query= from(\"n\").in(names). where(eq(\"n.length()\",5). select(\"n.toUpperCase()\"); for (String n: query) { System.out.println(n); }
    9. Ruby - Quick Hack names = [\"Burke\", \"Connor\", \"Frank\", \"Everett\", \"Albert\", \"George\", \"Harris\", \"David\"] query = from :n => names do where n.length => 5 orderby n select n.upcase end query.each{|e| puts e }
    10. Clojure (def names '(\"Burke\", \"Connor\", \"Frank\", \"Everett\", \"Albert\", \"George\", \"Harris\", \"David\")) (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 \"They are the same!\")) (defn they-are-different [] (println \"They are different!\")) (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 \"Parameters are: \" 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 \"Parameters are: \" 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

    + Phillip CalçadoPhillip Calçado, 10 months ago

    custom

    2801 views, 6 favs, 4 embeds more stats

    "We just started holding 20 minutes presentations d more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 2801
      • 2647 on SlideShare
      • 154 from embeds
    • Comments 0
    • Favorites 6
    • Downloads 76
    Most viewed embeds
    • 147 views on http://fragmental.tw
    • 4 views on http://gilpa.blogspot.com
    • 2 views on http://static.slideshare.net
    • 1 views on http://74.125.159.132

    more

    All embeds
    • 147 views on http://fragmental.tw
    • 4 views on http://gilpa.blogspot.com
    • 2 views on http://static.slideshare.net
    • 1 views on http://74.125.159.132

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories