Skip to content

Instantly share code, notes, and snippets.

@noprompt
Created February 19, 2014 04:52
Show Gist options
  • Save noprompt/9086232 to your computer and use it in GitHub Desktop.
Save noprompt/9086232 to your computer and use it in GitHub Desktop.
How to use slurp from ClojureScript
(ns foo.core
(:refer-clojure :exclude [slurp]))
(defmacro slurp [file]
(clojure.core/slurp file))
;; In CLJS
(ns bar.core
(:require [foo.core :include-macros true :refer [slurp]]))
;; This is possible because we can evaluate *Clojure* code at compile time.
(def project-clj
(slurp "project.clj"))
@FrankApiyo
Copy link

I haven't tried this but I'm skeptical because it does not make any sense to me 😞

@noprompt
Copy link
Author

@FrankApiyo ClojureScript uses Clojure to expand macros. This means the code inside the slurp macro above is being invoked in the Clojure environment and, thus, all of the usual Clojure functions are available. In this case, slurp will return the content of project.clj as a string and "inline" it on line 14.

So if project.clj looked like

(defproject foo)

then line 13 to 14 expands to

(def project-clj
  "(defproject foo)")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment