Hi,

using namespaces also requires to layout
the files in a certain way. For the following
consider the directory cp to be in the
classpath.

Create a file cp/permutations.clj with your
functions:

(ns permutations)

(defn fun1 ...)
(defn fun2 ...)

Then create your second namespace in
the corresponding file. Note that I introduced
a dot to get directories into discussion,
because dots in namespaces translate to
directories in filenames. So put the test
code into the file cp/test/perms.clj.

(ns test.perms
  (:require permutations))

(defn some-test
  []
  (permutations/fun1))

Require should be preferred over use, since
it doesn't pollute the namespace. And you
see where all the functions come from. For
a function which is used only once or twice
this is certainly sufficient. If you need the
functions more often and your prefix is too
long, you can easily assign an alias.

(ns test.perms
  (:require [some.long.prefix.permutations :as p]))

(defn some-test
  []
  (p/fun1))

If you want to use use, you should consider
using the :only option. This imports only the
functions named. For example, when we
use only the function fun1, then there is no
reason to also suck in fun2 just polluting
our namespace. Another advantage: we
see (as with require) where the function
comes from.

(ns test.perms
  (:use [permutations :only (fun1)]))

(defn some-test
  []
  (fun1))

Then you can use test.perms similarly from
other files or from the Repl in the same way
discussed above.

(ns testperms
 (:use permutations))

This doesn't work at all, so I must have the syntax wrong.

No, the syntax is correct. Since you didn't
provide the error ("It doesn't work!" is not
the error), I can only guess. But my suspicion
is that clojure told you something like the
following:

1:3 user=> (ns testperms (:use permutations))
java.io.FileNotFoundException: Could not locate permutations__init.class or permutations.clj on classpath: (repl-1:3)

It is important to remember:
- The files must be in the classpath!
- The namespace a.b.c has to
  be defined in the file a/b/c.clj.

This is a VVFAQ. It was explained on the list
by Chouser, me and others in every detail several
times during the last days alone, not to mention
the months before. Stressing the Google "Find"
button and reading the failure message are not
a bad things.

Sincerely
Meikel

Attachment: smime.p7s
Description: S/MIME cryptographic signature

Reply via email to