clojure - 在 clojure 命令行应用程序中获取 'Attempting to call unbound fn'

标签 clojure functional-programming jvm leiningen cider

我有以下代码,当我在 Cider 中逐步执行它时它可以工作,但是当我在命令行上直接运行它时,我收到以下错误:

Exception in thread "main" java.lang.IllegalStateException: Attempting to call unbound fn: #'clojure-todo.todo/execute-todo-cmd, compiling:(/private/var/folders/d_/b8gmsvl16sgdg70m15gx20l40000gn/T/form-init6080372317525706515.clj:1:125)
        at clojure.lang.Compiler.load(Compiler.java:7391)
        at clojure.lang.Compiler.loadFile(Compiler.java:7317)
        at clojure.main$load_script.invokeStatic(main.clj:275)
        at clojure.main$init_opt.invokeStatic(main.clj:277)
        at clojure.main$init_opt.invoke(main.clj:277)
        at clojure.main$initialize.invokeStatic(main.clj:308)
        at clojure.main$null_opt.invokeStatic(main.clj:342)
        at clojure.main$null_opt.invoke(main.clj:339)
        at clojure.main$main.invokeStatic(main.clj:421)
        at clojure.main$main.doInvoke(main.clj:384)
        at clojure.lang.RestFn.invoke(RestFn.java:421)
        at clojure.lang.Var.invoke(Var.java:383)
        at clojure.lang.AFn.applyToHelper(AFn.java:156)
        at clojure.lang.Var.applyTo(Var.java:700)
        at clojure.main.main(main.java:37)
Caused by: java.lang.IllegalStateException: Attempting to call unbound fn: #'clojure-todo.todo/execute-todo-cmd

这是代码。我猜这是一个命名空间错误,但我很困惑,如果它在苹果酒中工作,它怎么可能是一个命名空间问题。

 (ns clojure-todo.todo
  (:gen-class))

    ///// Other Code here
(defn print-msg-list [list]
  (doseq [item list] (print-msg item)))

(defn create-todo [todo-string] (hash-map :todo-content todo-string :checked false))

(defn list-todos [todos]
  (doseq [single-todo todos]
    (do
      (print (get single-todo :todo-content))
      (let [is-checked (get single-todo :checked)]
        (if is-checked
          (print-msg :is-checked)
          (print-msg :is-unchecked))))))

(defn add-todo []
  (do
    (print-msg :add-todo-message)
    (let [todo-string (read-line) the-todo-created (create-todo todo-string)]
        (def the-todos (conj the-todos the-todo-created))
        (list-todos the-todos)))

(def todo-fns {"1" add-todo "2" delete-todo "3" list-todos "4" check-todo})

(defn execute-todo-cmd [command]
  (get todo-fns command nil)))

(defn take-todo-command [backup-fn]
  (let [command (read-line)]
      (if (= command "q")
        (print-msg :goodbye)
        (let [todo-fn (execute-todo-cmd command)]
          (if todo-fn
            (todo-fn)
            (do
              (print-msg :sorry)
              (print-msg :border)
              (backup-fn)))))))

(defn run-app []
  (do
    (print-msg :welcome)
    (print-msg-list '(:add-todo :list-todos :delete-todo :check-todo :quit))
    ;; The take-todo-command fn takes a backup in case the user types in
    ;; a command that's not supported by the application. In this case we just
    ;; want the app to restart
    (take-todo-command run-app)))

最佳答案

这只是在add-todo中缺少一个右括号,并且在execute-todo-cmd中有一个额外的右括号。因此,在运行 add-todo 之前,execute-todo-cmd 并不存在,这就是它产生 unbound-fn 错误的原因。

关于clojure - 在 clojure 命令行应用程序中获取 'Attempting to call unbound fn',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48757493/

相关文章:

c - C 函数式编程有哪些工具?

unit-testing - Elixir /ExUnit : how to test functions with system calls most elegantly?

java - JVM 中 .class 对象的同步范围

java - 在 Java 中什么时候选择 SerialGC、ParallelGC 而不是 CMS、G1?

macros - 宏中的 clojure 引号和代字号

haskell - 这个 Haskell 移动平均函数的 Clojure 等价物?

clojure - lein - 如何使用下载的库

javascript - Javascript 中嵌套 for 循环与数组函数的性能

android - 你如何调整 gradle 中 dex 内存的 jvm args?

clojure - 围绕 lein 的困惑 :dependencies and :plugins