简体   繁体   English

如何让 Clojure 使用多个库? Clojure 相当于 Java 中的 folder.* 是什么?

[英]How do you get Clojure to use multiple libraries? What's the Clojure equivalent to folder.* in Java?

I'm curious how to get multiple files to import as part of a larger folder system?我很好奇如何将多个文件作为更大文件夹系统的一部分导入?

Say I'm using leiningen and I provide the:dependencies keyword in my project.clj file with the right argument for leiningen to find my library.假设我正在使用 leiningen,并且我在我的 project.clj 文件中提供了:dependencies 关键字,并为 leiningen 找到我的库提供了正确的参数。 I understand that I can simply provide my source file with "(:use [folder.file])" and it'll know to reference that specific file when I call functions from it, but how do I get it to do that for ALL files in that folder?我知道我可以简单地为我的源文件提供“(:use [folder.file])”并且当我从中调用函数时它会知道引用该特定文件,但是我如何让它为所有那个文件夹里的文件? What's the Clojure way of doing this thing you can do in Java with "file.*"? Clojure 做这件事的方法是什么,你可以在 Java 中用“file.*”做这件事?

Clojure does not have wildcard requires of namespaces (nor wildcard imports of Java classes). Clojure 没有通配符要求的命名空间(也没有通配符导入 Java 类)。 It just really isn't necessary in Clojure because namespaces tend to be fairly large, flat collections of related functions -- because we don't have classes like Java, we don't tend to have thousands of files.在 Clojure 中确实没有必要,因为命名空间往往相当大,相关函数的平坦 collections ——因为我们没有像 Java 这样的类,我们没有趋向于数千个文件。

At work, we have a pretty large Clojure codebase: 113k lines.在工作中,我们有一个相当大的 Clojure 代码库:113k 行。 It's broken down like this:它是这样分解的:

Clojure source 361 files 89724 total loc,
    3557 fns, 939 of which are private,
    570 vars, 30 macros, 90 atoms,
    85 agents, 24 protocols, 67 records,
    858 specs, 33 function specs.
Clojure tests 379 files 23795 total loc,
    4 specs, 1 function specs.

So the average source namespace is ~250 lines and has ~10 functions in it.因此,平均源名称空间约为 250 行,其中包含约 10 个函数。 We have a few namespaces that are over 1,000 lines (and one that's over 2,000 lines).我们有一些超过 1,000 行的命名空间(还有一个超过 2,000 行)。 A few namespaces require in a "lot" of other namespaces but that's pretty rare: I'd say most of our namespaces only bring in about half a dozen namespaces.一些命名空间需要“很多”其他命名空间,但这非常罕见:我想说我们的大多数命名空间只引入了大约六个命名空间。

There's also no hierarchy in Clojure namespaces. Clojure 命名空间中也没有层次结构。 It's common for a project to adopt a unique prefix for its namespaces, such as next.jdbc for the most popular JDBC library, and even when it has "sub-namespaces", such as next.jdbc.connection that's just another grouping -- there's no semantic of nesting or grouping.一个项目为其命名空间采用唯一前缀是很常见的,例如最流行的next.jdbc库的 next.jdbc,即使它具有“子命名空间”,例如next.jdbc.connection --ZCA87C7Z没有嵌套或分组的语义。

Clojure's explicit "require" brings some benefits. Clojure 的明确“要求”带来了一些好处。 The intent is clear, the effects are less vulnerable to future additions to the directory, you can more easily refactor without tools, and if you use tools you get more on-topic live-typing suggestions.意图很明确,效果不太容易受到将来添加到目录的影响,您可以更轻松地在没有工具的情况下进行重构,并且如果您使用工具,您可以获得更多关于主题的实时输入建议。

But "require" is certainly not the only way to introduce symbols to a namespace;但是“require”肯定不是将符号引入命名空间的唯一方法。 Namespaces are open.命名空间是开放的。 you may suit yourself: For example: Zach Tellman's "potemkin" library ( https://github.com/clj-commons/potemkin ) can fuse symbols from multiple namespaces into one that can be require'd.您可能适合自己:例如:Zach Tellman 的“potemkin”库( https://github.com/clj-commons/potemkin )可以将来自多个命名空间的符号融合到一个可以被要求的命名空间中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM