clojure需要语法原理 [英] clojure require syntax rationale

查看:133
本文介绍了clojure需要语法原理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很难理解(因此记住)clojure需要在此处描述的语法: http ://clojuredocs.org/clojure_core/1.3.0/clojure.core/require

I'm having a hard time understanding (and therefore remembering) the clojure require syntax described here: http://clojuredocs.org/clojure_core/1.3.0/clojure.core/require. It seems both counter intuitive and non-uniform.

例如,在下面为什么需要引用这个向量:

For example, in the following why is this vector required to be quoted:

(require '[clj-json.core :as json])

反直觉,因为通常向量在clojure中没有引用(列表被引用,向量被视为数据)。

Counter intuitive because normally vectors are not quoted in clojure (lists are quoted and vectors are treated as data).

且不统一,因为在这种情况下向量不是引号:

And non-uniform because in this case the vector is NOT quoted:

(ns xxx 
    (:require [clj-json.core :as json]))


$ b b

我意识到require函数和ns宏中的:require 用法只是光学上类似,但仍然。

I realize that the require function and the :require usage inside the ns macro are only optically similar, but still.

还有其他奇怪的东西,例如我可以这样做:

There are other pieces of weirdness as well, for example I can do this:

(require '(clj-json.core))

这样做:

(require '(clj-json.core :as json))

有人可以回答这些问题:

Can someone answer these questions:


  1. 第一个例子为什么向量需要引用?

  2. 为什么不需要在ns宏中引用?

  3. 标记不允许:as

  1. in the first example why does the vector need to be quoted?
  2. why does it not need to be quoted in the ns macro?
  3. why does the list notation not allow :as?

我想知道是否有原因

推荐答案


  1. 在第一个例子中为什么需要引用向量?

require 是一个函数,所以你必须引用它们,以防止评估。否则,这些符号将在当前上下文中查找,可能导致错误或可能的意外行为。顺便说一下,我认为这是不正确的:反直觉,因为通常的向量不在clojure引用(列表被引用和向量被视为数据)。

require is a function, so you have to quote them in order to prevent evaluation. Otherwise those symbols will be looked up in the current context, likely resulting in an error or possibly unexpected behavior. By the way, I think this not quite right: "Counter intuitive because normally vectors are not quoted in clojure (lists are quoted and vectors are treated as data)."

这可能是对你直观,但是引用向量(或地图或集合)没有错误。我经常在快速测试某些东西在REPL,不想为地图或向量中的每个关键字输入,例如:

It may be counter intuitive to you, however there's nothing wrong with quoting vectors (or maps or sets). I often do when quickly testing something at the REPL, and don't want to type a : for each keyword in a map or vector, for instance:

user=> '{a 1 b 2 c 3}

而不是:

user=> {:a 1 :b 2 :c 3}


  1. 为什么不需要在ns宏中加引号?

宏不评估他们的参数,它们比正常的函数调用更懒惰,所以符号( clj -json.core json )不需要引用以防止评估。

Macros don't evaluate their arguments, they're sort of lazier than normal function calls, so the symbols (clj-json.core and json) don't need to be quoted to prevent evaluation.


  1. 为什么列表符号不允许:as?

对不起,我不知道这个答案。

啊,我看了一下 require 并找到。还有另一种形式支持:

Ah, I took a look at the docs for require and found out. There's another form supported:

以下将加载库clojure.zip和clojure.set
缩写为's'。

"The following would load the libraries clojure.zip and clojure.set abbreviated as 's'."

(require '(clojure zip [set :as s]))

这篇关于clojure需要语法原理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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