我如何要求并提供clojure文件?

时间:2023-01-02 22:00:56

I have a set of functions saved in .clj.

我有一组保存在.clj中的函数。

How do I Provide selected set of functions and then import these functions in my other file?

如何提供选定的功能集,然后在我的其他文件中导入这些功能?

3 个解决方案

#1


You have a few options.

你有几个选择。

If it’s just a file (not in a package) then in your files, you can just use load. If your file was named “fun.clj”, you would just use the name of the file without the extension:

如果它只是一个文件(不在包中),那么在您的文件中,您可以只使用加载。如果您的文件名为“fun.clj”,则只需使用不带扩展名的文件名:

 (load "fun")

(provided fun.clj was on your classpath). Or

(提供fun.clj在你的类路径上)。要么

 (load "files/fun") 

if it wasn’t on your classpath but in the files directory.

如果它不在你的类路径上,而是在files目录中。

Or you could use load-file and pass it the location of your file:

或者您可以使用load-file并将其传递给文件的位置:

(load-file "./files/fun.clj")

If you wanted to namespace them (put them in a package), then you’d put the ns macro at the beginning of your file, again put it on your classpath. Then you could load it via use or require.

如果你想命名它们(把它们放在一个包中),那么你将ns宏放在文件的开头,再将它放在你的类路径上。然后你可以通过use或require加载它。

Here are the docs for the functions I’ve described:

以下是我所描述的函数的文档:

#2


Besides "load"ing source files, you can also use the leiningen "checkout dependencies" feature. If you have two leiningen projects, say, project A requires B (provider). In the root directory of project A, create a directory called "checkouts". Inside "/checkouts" make a symbolic link to the root directory of project B.

除了“加载”源文件,您还可以使用leiningen“checkout dependencies”功能。如果你有两个leiningen项目,比如说项目A需要B(提供者)。在项目A的根目录中,创建一个名为“checkouts”的目录。 “/ checkouts”里面是项目B的根目录的符号链接。

- project A
  - project.clj
  - checkouts
    - symlink to project B
  - src
  - test

in project A's project.clj, declare project B as a dependency in the :dependencies section as if it were a project in clojars.org. E.g.

在项目A的project.clj中,将项目B声明为:dependencies部分中的依赖项,就像它是clojars.org中的项目一样。例如。

(defproject project-a
:dependencies [[org.clojure/clojure "1.5.1"]
               [project-b "0.1.0"]])

The thing though is, you have to go into project B and type:

但事情是,你必须进入项目B并输入:

lein install

That will compile project B's files into a jar file, which will appear in your ~/.m2 directory, which is kind of like your local clojars.org cache.

这会将项目B的文件编译成一个jar文件,该文件将出现在〜/ .m2目录中,这有点像你的本地clojars.org缓存。

Once you set all this up, in your *.clj src file(s) of project A, you can "require" project B files in the normal way, as if it were from clojars.org:

一旦你设置了所有这些,在你的项目A的* .clj src文件中,你可以正常方式“需要”项目B文件,就像它来自clojars.org:

(ns project-a.core
  (:require [project-b.core :as pb])

This way, you can refer to functions in your project-b.core the usual way like this:

这样,你可以参考你的项目中的函数-b.core通常的方式如下:

pb/myfunction1

In my opinion, this is a pretty good way to share libraries and data between Leiningen projects while trying keep each Leiningen project as independent, self-contained, and minimal as possible.

在我看来,这是在Leiningen项目之间共享库和数据的一种非常好的方式,同时尝试将每个Leiningen项目保持为独立,自包含且尽可能小。

#3


This one solved my problem and I have looked through countless other issues here so I would like to clarify.

这个问题解决了我的问题,我在这里看了无数其他问题,所以我想澄清一下。

The easiest way in emacs (on linux) is to do something like this:

emacs(在linux上)最简单的方法是做这样的事情:

java -cp "lib/*":. clojure.main -e "(do (require 'swank.swank) (swank.swank/start-repl))"

(note the "lib/*":. given to -cp

(注意“lib / *”:。给予-cp

Then you can use M-x slime-connect to connect with this instance.

然后,您可以使用M-x slime-connect连接此实例。

Don't know if it's required, but I have read that it's a good idea to use the same version of clojure, clojure-contrib and swank-clojure on both sides.

不知道是否需要它,但我已经读过,在两侧使用相同版本的clojure,clojure-contrib和swank-clojure是个好主意。

You can also setup the path inside emacs by consing the "." to swank-clojure-classpath.

您还可以通过查询“。”来设置emacs内的路径。 to swank-clojure-classpath。

#1


You have a few options.

你有几个选择。

If it’s just a file (not in a package) then in your files, you can just use load. If your file was named “fun.clj”, you would just use the name of the file without the extension:

如果它只是一个文件(不在包中),那么在您的文件中,您可以只使用加载。如果您的文件名为“fun.clj”,则只需使用不带扩展名的文件名:

 (load "fun")

(provided fun.clj was on your classpath). Or

(提供fun.clj在你的类路径上)。要么

 (load "files/fun") 

if it wasn’t on your classpath but in the files directory.

如果它不在你的类路径上,而是在files目录中。

Or you could use load-file and pass it the location of your file:

或者您可以使用load-file并将其传递给文件的位置:

(load-file "./files/fun.clj")

If you wanted to namespace them (put them in a package), then you’d put the ns macro at the beginning of your file, again put it on your classpath. Then you could load it via use or require.

如果你想命名它们(把它们放在一个包中),那么你将ns宏放在文件的开头,再将它放在你的类路径上。然后你可以通过use或require加载它。

Here are the docs for the functions I’ve described:

以下是我所描述的函数的文档:

#2


Besides "load"ing source files, you can also use the leiningen "checkout dependencies" feature. If you have two leiningen projects, say, project A requires B (provider). In the root directory of project A, create a directory called "checkouts". Inside "/checkouts" make a symbolic link to the root directory of project B.

除了“加载”源文件,您还可以使用leiningen“checkout dependencies”功能。如果你有两个leiningen项目,比如说项目A需要B(提供者)。在项目A的根目录中,创建一个名为“checkouts”的目录。 “/ checkouts”里面是项目B的根目录的符号链接。

- project A
  - project.clj
  - checkouts
    - symlink to project B
  - src
  - test

in project A's project.clj, declare project B as a dependency in the :dependencies section as if it were a project in clojars.org. E.g.

在项目A的project.clj中,将项目B声明为:dependencies部分中的依赖项,就像它是clojars.org中的项目一样。例如。

(defproject project-a
:dependencies [[org.clojure/clojure "1.5.1"]
               [project-b "0.1.0"]])

The thing though is, you have to go into project B and type:

但事情是,你必须进入项目B并输入:

lein install

That will compile project B's files into a jar file, which will appear in your ~/.m2 directory, which is kind of like your local clojars.org cache.

这会将项目B的文件编译成一个jar文件,该文件将出现在〜/ .m2目录中,这有点像你的本地clojars.org缓存。

Once you set all this up, in your *.clj src file(s) of project A, you can "require" project B files in the normal way, as if it were from clojars.org:

一旦你设置了所有这些,在你的项目A的* .clj src文件中,你可以正常方式“需要”项目B文件,就像它来自clojars.org:

(ns project-a.core
  (:require [project-b.core :as pb])

This way, you can refer to functions in your project-b.core the usual way like this:

这样,你可以参考你的项目中的函数-b.core通常的方式如下:

pb/myfunction1

In my opinion, this is a pretty good way to share libraries and data between Leiningen projects while trying keep each Leiningen project as independent, self-contained, and minimal as possible.

在我看来,这是在Leiningen项目之间共享库和数据的一种非常好的方式,同时尝试将每个Leiningen项目保持为独立,自包含且尽可能小。

#3


This one solved my problem and I have looked through countless other issues here so I would like to clarify.

这个问题解决了我的问题,我在这里看了无数其他问题,所以我想澄清一下。

The easiest way in emacs (on linux) is to do something like this:

emacs(在linux上)最简单的方法是做这样的事情:

java -cp "lib/*":. clojure.main -e "(do (require 'swank.swank) (swank.swank/start-repl))"

(note the "lib/*":. given to -cp

(注意“lib / *”:。给予-cp

Then you can use M-x slime-connect to connect with this instance.

然后,您可以使用M-x slime-connect连接此实例。

Don't know if it's required, but I have read that it's a good idea to use the same version of clojure, clojure-contrib and swank-clojure on both sides.

不知道是否需要它,但我已经读过,在两侧使用相同版本的clojure,clojure-contrib和swank-clojure是个好主意。

You can also setup the path inside emacs by consing the "." to swank-clojure-classpath.

您还可以通过查询“。”来设置emacs内的路径。 to swank-clojure-classpath。