Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将Clojurescript中的core.match与goog.events.KeyCodes一起使用?_Clojure_Clojurescript_Core.match - Fatal编程技术网

如何将Clojurescript中的core.match与goog.events.KeyCodes一起使用?

如何将Clojurescript中的core.match与goog.events.KeyCodes一起使用?,clojure,clojurescript,core.match,Clojure,Clojurescript,Core.match,上述代码有效。然而,如果我尽量少一点罗嗦,直接使用goog键码,就像这样 (defn editing-mode? [] "a hardcoded (for the moment) value, will look up in db later" false) (def UP 38) ;; goog.events.KeyCodes.UP (def DOWN 40) ;; goog.events.KeyCodes.DOWN (def LEFT 37) ;; goog.events.KeyC

上述代码有效。然而,如果我尽量少一点罗嗦,直接使用goog键码,就像这样

(defn editing-mode? []
  "a hardcoded (for the moment) value, will look up in db later"
  false)

(def UP 38) ;; goog.events.KeyCodes.UP
(def DOWN 40) ;; goog.events.KeyCodes.DOWN
(def LEFT 37) ;; goog.events.KeyCodes.LEFT
(def RIGHT 39) ;; goog.events.KeyCodes.RIGHT
(def W 87) ;; goog.events.KeyCodes.W
(def S 83) ;; goog.events.KeyCodes.S
(def A 65) ;; goog.events.KeyCodes.A
(def D 68) ;; goog.events.KeyCodes.D
(def E 69) ;; goog.events.KeyCodes.E
(def ESC 27) ;; goog.events.KeyCodes.ESC

(defn delta [e]
  ;; e is a google closure Event
  (js/console.log (.-keyCode e))
  (js/console.log (editing-mode?))
  (match [(editing-mode?) (.-keyCode e)]
   [false 38] [:slide :up]
   [false 40] [:slide :down]
   [false 37] [:slide :left]
   [false 39] [:slide :right]
   [false 87] [:slide :up]
   [false 83] [:slide :down]
   [false 65] [:slide :left]
   [false 68] [:slide :right]
   [false 69] [:start-editing]
   [true 27]  [:done-editing]
   :else nil))
我得到以下cljsbuild错误:

(match [(editing-mode?) (.-keyCode e)]
  [false goog.events.KeyCodes.UP] [:slide :up]
  [false goog.events.keyCodes.DOWN] [:slide :down]
  ...
好的,所以我不能使用
goog.events.KeyCodes.
本身,但也许我可以使用引用它们的
def
?所以我试着

...
Caused by: clojure.lang.ExceptionInfo: Invalid local name: goog.events.KeyCodes.UP ...
...
这确实可以编译,但现在match不起作用。每个关键事件都与
[false UP]
匹配子句匹配(core.match始终发出
[:slide:UP]


无论如何,第一个代码示例确实有效。但是为什么我不能在我的core.match matcher中使用
goog.events.KeyCodes.*
或引用
goog.events.KeyCodes.*
?有什么我遗漏的吗?

核心部分的一个关键部分。匹配是符号将绑定到值,而不是匹配。也就是说,
UP
的当前值未被查找并在匹配中使用;相反,当
false
匹配时,符号
UP
将绑定到值
(.-keyCode e)

不幸的是,据我所知,使用
core.match
,您对此无能为力。它在很大程度上依赖于文字值。但是,由于您的模式相当简单,您可以使用
(conp=…)

您只能匹配:


因此,您可以首先通过
(let[…])
将keycodes绑定到局部变量,从而实现您的结果。我想通过简单地反转keycodes对象来构造“keyname”到“keycode”的映射:

      (= (let [x 2
               y 2]
           (match [x]
             [0] :a0
             [1] :a1
             [y] :a2
             :else :a3))
        :a2))
然后,我可以用字符串来识别键,而不是笨重的互操作(字符串是值,因此可以很好地匹配):


请注意,您可以匹配局部变量,因此一种解决方法是首先
(让[…]
您的键码,然后匹配该局部绑定:
      (= (let [x 2
               y 2]
           (match [x]
             [0] :a0
             [1] :a1
             [y] :a2
             :else :a3))
        :a2))
(ns awesome.sauce
  (:require [clojure.set :as set])
  (:import [goog.events KeyCodes]))

(def codename
  (set/map-invert (js->clj KeyCodes)))
(match [(editing-mode?) (codename (.-keyCode e))]
   [false "UP"] [:slide :up]
   [false "DOWN"] [:slide :down])