在Clojure中定义我自己的阅读器宏 [英] Define my own reader macro in clojure

查看:40
本文介绍了在Clojure中定义我自己的阅读器宏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在clojure中定义自己的阅读器宏:

I would like to define my own reader macro in clojure:

(read-string "ßfoo")
=> (some_func :foo)

有可能吗?

推荐答案

可以通过在类路径顶部的 data_readers.clj 中具有阅读器映射来创建带标记的文字。

It is possible to create tagged literals by having a reader map in data_readers.clj at the top of your classpath.

这必须位于类路径顶部的 data_readers.clj 中(通常为 src 目录)。

This must be in data_readers.clj at the top of your classpath (usually src directory).

{ß reader-demo.core/read-fn}

这进入到reader-demo.core

This goes into reader-demo.core

(defn some-func
  [arg]
  (str "some-func invoked with " arg))

(defn read-fn
  [arg]
  (-> arg
      keyword
      some-func))

调用

#ß foo

将返回

"some-func invoked with :foo"

此技术的描述如下:读者:带标记的文字

请注意,在实践中,应将带标记的文字命名为全部名称空间非命名空间保留给Clojure本身。

Notice that in practice you should namespace your tagged literals as all non-namespaced ones are reserved for Clojure itself.

这篇关于在Clojure中定义我自己的阅读器宏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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