tags:

views:

596

answers:

2

I have a set of functions saved in .clj.

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

+6  A: 

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 w/o the extension:

 (load "fun")

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

 (load "files/fun")

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

Or you could use load-file and pass it the location of your 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.

Here are the docs for the functions I've described.

Hope this helps.

seth
I am sorry but I still have some problem. I have a file called stopwords.clj and Autotag.clj. I want to import stopwords into Autotag. When I put (load "stopwords"), it says "Could not locate stopwords__init.class or stopwords.clj on classpath:" even though I have stopwords and Autotag in the same folder. I am sorry but I am a complete newbie. Please help me.
kunjaan
The current directory is probably not on your classpath so clojure can't 'see' it. Put stopwords.clj somewhere on your classpath (or change your classpath to include ".").
seth
This is embarrassing but how do I configure the class path in eclipse?
kunjaan
Sorry, don't know. emacs user over here.
seth
Hi seth, I moved to emacs from eclipse. How do I add directories in my class path so that the current working file can access third-party libraries? Thanks a lot.
kunjaan
Thank you, Seth. I've been banging my head for an hour trying to figure this out (no thanks to the PragProg book, which is anything but pragmatic)
Joshua Cheek
A: 

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:

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

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

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

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.

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

grm