2010/8/17 jandot <jan.ae...@gmail.com>:
> Hi all,
>
> I'm trying to write a library to perform some statistical and data
> mining analyses. Clojure has proven a great help here, especially with
> the incanter library. Writing the code has been kind of an "organic"
> process (read: no planning), and I ended up with different conceptual
> groups of functions all within one file. So it makes sense to split
> this up and start organizing it.
>
> Unfortunately, I am having trouble making the different files (and
> namespaces) talk to each other, and need some help.
>
> Let's say I have two parts in my analysis, each of which require quite
> a few underlying functions. I would ideally start up a repl and focus
> on just one analysis: playing with the data, making some graphs, ...
>
> I've created a leiningen project with "lein new my-important-project",
> to which I added two files in src: analysis-1.clj and analysis-2.clj.
> Directory structure:
>
>  +- project.clj
>  +- test
>  |     +- my-important-project
>  |               +- core.clj
>  +- src
>        +- my-important-project
>                  +- core.clj
>                  +- analysis-1.clj
>                  +- analysis-2.clj
>
> Let's say this is the contents for those 3 files in src/ (core.clj
> contains functions and constants that are necessary for both
> analyses):
>
>    ==== core.clj ====
>    (ns my-important-project.core
>      (:use [incanter core io stats charts]
>              [somnium congomongo]))
>    (mongo! :db "the-database")
>    (def some-constant 3.141592)
>
>    ====analysis-1.clj====
>    (ns my-important-project.analysis-1)
>    (defn say-from-one [text] (println (str "from 1: " text)))
>
>    ====analysis-2.clj====
>    (ns my-important-project.analysis-2)
>    (defn say-from-two [text] (println (str "from 2: " text)))

Here, the ns declaration looks right.

> If I want to work on analysis 2, I'd start up the repl in the main
> directory created by lein (so one *up* from src) and type:
>
>    user=> (ns '(my-important-project core analysis-2))
>    user=> (say-from-two "some text")

ns is a macro normally used at the top of the source files. To force
the code of a namespace/file to be loaded, call require (which, unlike
ns, is a function and thus requires the args to be quoted):

(require '(my-important-project core analysis-2))

At this point, you should be able to call say-from-to with its full
name: my-important-project.analysis-2/say-from-two. You can use the
:as option to make a shorter prefix if you like (see link below). If
you want to play around inside the namespace, you can go into it by
calling:

(in-ns 'my-important-project.analysis-2)

or simply use the ns macro:

(ns my-important-project.analysis-2)

You will see the prompt change when you do this and from here you
should be able to call say-from-two directly.

> But as you'd have guessed: this doesn't work. The (ns) returns the
> following error:
>    java.lang.ClassCastException: java.lang.String cannot be cast to
> clojure.lang.IObj (core.clj:1)
>
> I've tried different versions of the (ns) function, including a vector
> as its argument, periods instead of spaces, ...
>
> In addition: in the end I'd like to write a script that does the
> analysis automatically. So instead of going into the repl, I'd "cljr
> run" a clj file. What should the (ns) bit of that file look like?
>
> I've been searching the web for what I'm doing wrong, but haven't
> found the solution yet. It's quite frustrating to see so many
> discussions about namespaces, but not being able to solve this issue.

The ns macro and library conventions are described here: http://clojure.org/libs

> Any help very much appreciated,
> jan.

The ns macro is pretty complex and unfortunately I don't have time to
write longer about it right now. I hope this could at least get you
started.

// Rasmus

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to