Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What advantage are Clojure's 1.4 reader literals, and why won't sample compile?

Tags:

clojure

I have two questions. First, what do I need to do to get the following code to compile, and what are the Clojure 1.4 reader literals giving me that I cannot get from macros?

I understand I am overwriting the reader with the local data_readers.clj file, but where can I read further about these reader literals in more detail than in the Clojure 1.4 release notes, which I've already visited?

Here is the code being compiled with error.

java.lang.RuntimeException: No reader function for tag ?=, 

project.clj

(defproject repl-test "0.0.1-SNAPSHOT"
  :description "TODO: add summary of your project"
  :dependencies [[org.clojure/clojure "1.4.0"]
                 [org.clojure/clojure-contrib "1.2.0"]
                 [clojure-csv/clojure-csv "1.3.2"]
                 [org.clojure/tools.cli "0.1.0"]
                 [util "1.0.2-SNAPSHOT"]
                 [clj-http "0.1.3"]]
   :aot [repl-test.core]
   :main repl-test.core)

data_readers.clj (located at the top of my lein project repl-test)

{
 ?= repl-test.core/debug-print
 str repl-test.core/expand-sexp
}

Should data_readers.clj go somewhere else? If so, where?

Pertinent parts of core.clj

(ns repl-test.core
  (:gen-class)
  (:require [clojure.string :as str])
  (:require [util.core :as utl])
  (:use clojure-csv.core))

(defn debug-print
  "Gauche debug print"
  [x]
  `(let [res# ~x]
     (println "?=" res#)
     res#))

(defn expand-sexp
  "Expand S-exp in string"
  [s]
  (let [ls (map-indexed #(if (even? %) %2 (read-string %2))
                        (str/split s #"`"))]
    `(apply str (list ~@ls))))

(defn -main 
  [& args]
  (println (map inc #?=(range 10)))
 
  (let [i 100]
    (println #str"i = `i`")
    (println #str"(+ 1 2 3) = `(+ 1 2 3)`")))

The example code came from here.

Thanks.

like image 810
octopusgrabbus Avatar asked Aug 18 '12 22:08

octopusgrabbus


1 Answers

Reader literals let you create your own kinds of literals. Things like:

(< #meter 2 #inch 5)
(mass #molecule "H2O")

Unlike regular macros, reader literals are handled by the reader. So you can use them in s-expression based data files, not just code.

The literals are listed in data_readers.clj which must be at the root of your class path. That is in the src directory of a lein project.

like image 86
Alex Jasmin Avatar answered Nov 13 '22 12:11

Alex Jasmin