在跷跷板中返回Clojure动作时出错

时间:2012-08-25 21:43:40

标签: clojure action seesaw

此程序打开一个文件,将其读入列表 然后要求用户一次一个地从列表中输入单词 但是在发言后我发现了一个错误

(ns EKS.Core)
(use '[speech-synthesis.say :as say])
(use '[clojure.java.shell :only [sh]])
(use 'seesaw.core)
(use 'seesaw.dev)
(use 'seesaw.chooser)
(use 'clojure.java.io)
(import 'java.io.File)
(native!)
(defn festival [x](sh "sh" "-c" (str "echo " x " | festival --tts")))
(defn espeak [x] (sh "espeak" x))
(defn mac-say[x] (sh "say" x))
(defn check-if-installed[x] (:exit(sh "sh" "-c" (str "command -v " x " >/dev/null 2>&1 || { echo >&2 \"\"; exit 1; }"))))


(defn env
  "Returns the system property for user.<key>"
  [key]
  (System/getProperty (str "user." key)))

(defn get-lines [fname]
  (with-open [r (reader fname)]
    (doall (line-seq r))))

(defn engine-check[]
(def engines (conj["Google" ]
(if (= (check-if-installed "festival") 0)  "Festival" )
(if (= (check-if-installed "espeak") 0) "ESpeak"   )
(if (= (check-if-installed "say") 0)  "Say"  )))(remove nil? engines))
(engine-check)

(def ListDir  (str (env "home") ))
(def speak say)

(defn setup-list[ file](
 (def ListEntry (remove #{""}  (get-lines (.getAbsolutePath file))))
 (speak (str "Enter the word " (first ListEntry)))
 (.getAbsolutePath file)
 (def current 0)))


(def Open-Action (action :handler (fn [e] (choose-file :type :open :selection-mode :files-only :dir ListDir :success-fn (fn [fc file](setup-list file))))
:name "Open"
:key "menu O"
:tip "Open  list"))


(def entry-text(text :id :entry :text "" ))

(defn check-word[] ((if (= (text entry-text) (nth ListEntry current))(speak "correct")(speak "incorrect"))(def current (+ current 1))(speak (str "Enter the word " (nth ListEntry current)))))

(def Main-Panel(grid-panel :vgap 7 :hgap 20 :border "Main" :items[ (top-bottom-split  entry-text
        (left-right-split (grid-panel :vgap 7 :items[(label :text "Correct: 0") (label :text "Incorrect: 0")]) (grid-panel :vgap 7 :items[(button :text "Next"
            :mnemonic \N
            :listen [:action (fn [e] (check-word))]) 
           (button :text "Repeat!"
            :mnemonic \\
            :listen [:action (fn[e] (speak(nth ListEntry current)))])
                                           ])   :divider-location 1/3 )
          :divider-location 1/3 )]))

(def Main-Window(frame :title "??????", :on-close :exit, :size[425 :by 172]:menubar(menubar :items[(menu :text "File" :items [Open-Action ])]) :content Main-Panel ))

(show! Main-Window)

这是我打开文件时收到的错误信息

 Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: clojure.lang.LazySeq cannot be cast to clojure.lang.IFn
    at clojure.lang.Var.fn(Var.java:378)
    at clojure.lang.Var.invoke(Var.java:409)
    at EKS.Core$setup_list.invoke(NO_SOURCE_FILE:38)
    at EKS.Core$fn__5794$fn__5795.invoke(NO_SOURCE_FILE:42)
    at seesaw.chooser$choose_file.doInvoke(chooser.clj:173)
    at clojure.lang.RestFn.invoke(RestFn.java:619)
    at EKS.Core$fn__5794.invoke(NO_SOURCE_FILE:42)
    at seesaw.action$action$fn__1218.invoke(action.clj:90)
    at seesaw.action.proxy$javax.swing.AbstractAction$0.actionPerformed(Unknown Source)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2018)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2341)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.AbstractButton.doClick(AbstractButton.java:376)
    at javax.swing.plaf.basic.BasicMenuItemUI.doClick(BasicMenuItemUI.java:833)
    at javax.swing.plaf.basic.BasicMenuItemUI$Handler.mouseReleased(BasicMenuItemUI.java:877)
    at java.awt.AWTEventMulticaster.mouseReleased(AWTEventMulticaster.java:289)
    at java.awt.Component.processMouseEvent(Component.java:6505)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3321)
    at java.awt.Component.processEvent(Component.java:6270)
    at java.awt.Container.processEvent(Container.java:2229)
    at java.awt.Component.dispatchEventImpl(Component.java:4861)
    at java.awt.Container.dispatchEventImpl(Container.java:2287)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4832)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4492)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4422)
    at java.awt.Container.dispatchEventImpl(Container.java:2273)
    at java.awt.Window.dispatchEventImpl(Window.java:2719)
    at java.awt.Component.dispatchEvent(Component.java:4687)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:713)
    at java.awt.EventQueue.access$000(EventQueue.java:104)
    at java.awt.EventQueue$3.run(EventQueue.java:672)
    at java.awt.EventQueue$3.run(EventQueue.java:670)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
    at java.awt.EventQueue$4.run(EventQueue.java:686)
    at java.awt.EventQueue$4.run(EventQueue.java:684)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:683)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:244)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:163)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:151)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:147)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:139)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:97)

这是我正在使用的测试文件

this
here
word

1 个答案:

答案 0 :(得分:3)

我认为导致追溯的是:

(defn setup-list[ file](
  (def ListEntry (remove #{""}  (get-lines (.getAbsolutePath file))))
  (speak (str "Enter the word " (first ListEntry)))
  (.getAbsolutePath file)
  (def current 0)))

注意[ file]之后的paren。你正在调用def(这可能不是你的意思),但def的结果是一个懒惰的序列(因为remove),并且因为那个paren而被调用为函数。不幸的是,我不清楚你要用setup-list做什么,否则我会为你写一些更惯用的东西。

您还应该考虑其他一些事情。首先,不要在一个非常糟糕的函数中使用(def ...)。在这种情况下,我认为你真正追求的是(let ...)

(defn setup-list [file]
  (let [entries (get-lines (.getAbsolutePath file))]
    ...))

如果您可以为代码保留更多结构,它还可以帮助您了解正在发生的事情。我认为格式化是让paren隐藏的东西,如果你有更好的风格,它可能会更明显:

(defn setup-list [file]
  ((def ListEntry ...)
    ...))

在这里,更清楚的是def将被用作函数调用,这可能不是你想要的。

顺便说一下,Clojure有一些Library Coding Standards。他们在那里有一些好东西。而且,Ring是一个很好的例子,代码库也是如此。