【问题标题】:Calling Function In Separate Clojure Namespace在单独的 Clojure 命名空间中调用函数
【发布时间】:2023-03-05 18:12:01
【问题描述】:

如何从另一个命名空间 bene-cmp.core 调用一个 Clojure 命名空间 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]))

我收到此错误

原因:java.lang.RuntimeException:没有这样的变量:bcsv/ret-csv-data

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

【问题讨论】:

    标签: clojure


    【解决方案1】:

    您需要调用函数,而不仅仅是引用 var。

    如果你的 ns 中有这个:

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

    然后你需要在命名空间/别名限定的变量周围加上括号来调用它:

    (let [benetrak-csv-data (bcsv/ret-csv-data arg)]
      ; stuff
      )
    

    【讨论】:

    • 谢谢。我是在引用 var 而不是调用函数。至少我的 :require 直觉很好。
    猜你喜欢
    • 1970-01-01
    • 2013-03-12
    • 1970-01-01
    • 2015-05-25
    • 2014-07-15
    • 2011-08-31
    • 2012-10-13
    • 1970-01-01
    • 2011-11-07
    相关资源
    最近更新 更多