Clojure的1.4读取器文字有什么优势,为什么不编译样本? [英] What advantage are Clojure's 1.4 reader literals, and why won't sample compile?

查看:134
本文介绍了Clojure的1.4读取器文字有什么优势,为什么不编译样本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个问题。首先,我需要做什么来获得下面的代码编译,什么是Clojure 1.4读取器文字给我,我不能从宏获取?

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?

我理解我使用本地data_readers.clj文件覆盖了读者,但我在哪里可以阅读更多有关这些读者文字的细节,而不是Clojure 1.4发行说明,我已经访问过了?

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 ?=, 



.clj



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(位于我的lein项目复制测试的顶部)



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

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

应该data_readers.clj去别的地方吗?如果是,在哪里?

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

(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)`")))

来自这里

谢谢。 p>

Thanks.

推荐答案

阅读器文字可以让你创建你自己的各种文字。例如:

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

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

与常规宏不同,读者文字由阅读器处理。因此,您可以在基于s表达式的数据文件中使用它们,而不仅仅是代码。

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

这些文本列在 data_readers.clj ,它必须在你的类路径的根。这是在lein项目的 src 目录中。

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.

这篇关于Clojure的1.4读取器文字有什么优势,为什么不编译样本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆