Skip to content

Instantly share code, notes, and snippets.

@bayan
bayan / gist:3331552
Created August 12, 2012 11:57
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")
@bayan
bayan / i18n.clj
Created August 15, 2012 12:41
Transliterajure - Clojure macros to internationalize source code
;; English is the defacto standard when it comes to programming languages.
;; It is used to derive the names of special forms (e.g. if, def, etc) and core functions (find, map, etc).
;;
;; What about when you can't or just don't want to write in English?
;; For example: http://en.wikipedia.org/wiki/Language_policy_in_France
;;
;; How hard would it be to write code using another language?
;; Preprocessing source files using substitution before compiling/running won't allow us to run the code from a REPL.
;; Let's see what we can do in Clojure.
;;
@bayan
bayan / gist:3382884
Created August 17, 2012 21:27
Clojure DSL to generate static HTML
;; Generating static HTML using Clojure macros
;; It is possible to write a DSL in Clojure that generates HTML markup without the need to write a separate parser and compiler (e.g. HAML).
;; Our aim here would be to write code that converts the following Clojure code into the HTML below it
;; (html
;; (head)
;; (body
;; (h1 "An example")
### Keybase proof
I hereby claim:
* I am bayan on github.
* I am bkhalili (https://keybase.io/bkhalili) on keybase.
* I have a public key ASA6Uza5arh0TkVrTwC5AYii5ThWrtDUEyjrXtwlLNAD8wo
To claim this, I am signing this object: