Calling Function In Separate Clojure Namespace

Go To StackoverFlow.com

6

How do I call a function in one Clojure namespace, bene-csv.core from another namespace, bene-cmp.core? I've tried various flavors of :require and :use with no success.

Here is the function in bene-csv:

(defn ret-csv-data
    "Returns a lazy sequence generated by parse-csv.
     Uses open-csv-file which will return a nil, if
     there is an exception in opening fnam.

     parse-csv called on non-nil file, and that
     data is returned."

    [fnam]
    (let [  csv-file (open-csv-file fnam)
            csv-data (if-not (nil? csv-file)
                         (parse-csv csv-file)
                         nil)]
            csv-data))

Here is the header of bene-cmp.core:

(ns bene-cmp.core
   .
   .
   .
  (:gen-class)
  (:use [clojure.tools.cli])
  (:require [clojure.string :as cstr])
  (:use bene-csv.core)
  (:use clojure-csv.core)
  .
  .
  .

The calling function -- currently a stub -- in (bene-cmp.core)

defn fetch-csv-data
    "This function merely loads the two csv file arguments."

    [benetrak-csv-file gic-billing-file]
        (let [benetrak-csv-data ret-csv-data]))

If I modify the header of bene-cmp.clj

 (:require [bene-csv.core :as bcsv])

and change the call to ret-csv-data

(defn fetch-csv-data
    "This function merely loads the two csv file arguments."

    [benetrak-csv-file gic-billing-file]
        (let [benetrak-csv-data bcsv/ret-csv-data]))

I get this error

Caused by: java.lang.RuntimeException: No such var: bcsv/ret-csv-data

So, how do I call fetch-csv-data? Thank You.

2012-04-04 16:43
by octopusgrabbus


8

You need to invoke the function, not just reference the var.

If you have this in your ns:

(:require [bene-csv.core :as bcsv])

Then you need to put parentheses around the namespace/alias qualified var to invoke it:

(let [benetrak-csv-data (bcsv/ret-csv-data arg)]
  ; stuff
  )
2012-04-04 16:48
by Jeremy
Thanks. I was referencing the var instead of calling the function. At least my :require instincts were good - octopusgrabbus 2012-04-04 16:50
Ads