Search code examples
fileimportclojure

How do I require and provide clojure files?


I have a set of functions saved in a clojure file.

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


Solution

  • 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:

     (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: