在单独的Clojure命名空间中调用函数

6
我该如何从另一个命名空间bene-cmp.core调用bene-csv.core中的函数?我尝试了多种:require和:use,但都没有成功。
这是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))

以下是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)
  .
  .
  .

当前在(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]))

如果我修改bene-cmp.clj的头部
 (:require [bene-csv.core :as bcsv])

并且修改调用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]))

我遇到了这个错误

由于没有找到变量 bcsv/ret-csv-data,导致出现了 java.lang.RuntimeException 的错误。

那么,我该如何调用 fetch-csv-data 呢?谢谢。

1个回答

8
你需要调用函数而不是仅仅引用变量。
如果你在ns中有这个:
(:require [bene-csv.core :as bcsv])

那么你需要在命名空间/别名限定的变量周围加上括号才能调用它:
(let [benetrak-csv-data (bcsv/ret-csv-data arg)]
  ; stuff
  )

谢谢。我引用了变量而非调用函数。至少我的:require本能是正确的。 - octopusgrabbus

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接