Skip to content

Instantly share code, notes, and snippets.

@bayan bayan/gist:3331552
Created Aug 12, 2012

What would you like to do?
Ruby's array of words and symbols shortcuts in Clojure - an example of how macros can be used to implement language level features that are present in other languages
;; Ruby has a short cut to specify an array of words: %w(apple bee carrot) → ["apple", "bee", "carrot"]
;; This can't be implemented in Ruby itself, and requires the language designers to implement this (in C).
;; Clojure, being a Lisp, allows anyone to add such a feature directly using a macro:
(defmacro %w [& args] `(map str '~args))
(%w apple bee carrot)
;; → ("apple" "bee" "carrot")
;; What about a list of Clojure keywords (known as symbols in Ruby)?
;; This is coming in Ruby 2.0. But again, Clojure programmers wouldn't need to wait for the language designers:
(defmacro %i [& args] `(map keyword '~args))
(%i apple bee carrot)
;; → (:apple :bee :carrot)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.