Clojure- 在函数内部定义变量?
Clojure- Defining a variable inside of a function?
我有这个变量,手,当我自己定义它时它工作得很好。
(def yourHand
(map
;;fn
#(let [x %]
(cond
;;hearts
(< x 10) (* x -1)
(< x 13) -10
;;diamonds
(< x 23) (* (mod x 13) -1)
(< x 26) -10
;;clubs
(< x 36) (mod x 13)
(< x 39) 10
;;spades
(< x 49) (mod x 13)
(< x 52) 10
))
;;list
(take (rand-int 12) (repeatedly #(+ 1 (rand-int 52))))))
我想在这里的这个函数中使用这个变量。当我先定义变量然后在函数中使用它的名称时,这很好用。
(reduce + (vec (map #(let [x %]
(cond
(= x 1) 1
:else 0
))
yourHand)))
当我尝试在函数中定义变量时出现问题,就像这样。
(reduce + (vec (map #(let [x %]
(cond
(= x 1) 1
:else 0
))
(def hand
(map
;;fn
#(let [x %]
(cond
;;hearts
(< x 10) (* x -1)
(< x 13) -10
;;diamonds
(< x 23) (* (mod x 13) -1)
(< x 26) -10
;;clubs
(< x 36) (mod x 13)
(< x 39) 10
;;spades
(< x 49) (mod x 13)
(< x 52) 10
))
;;list
(take (rand-int 12) (repeatedly #(+ 1 (rand-int 52)))))))))
如果不是因为两件事,这就没有必要了。首先,如果可能的话,我想把这个程序压缩成一个函数(我认为它/是/可能的!)。其次,我需要在我的程序中的另一个位置使用这个变量,所以我需要能够以某种方式引用它。
无论如何,当我尝试计算上述函数时,它抱怨说它不知道 "how to create ISeq from: clojure.lang.Var"。 (这是错误:IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Var clojure.lang.RT.seqFrom (RT.java:542))我假设这意味着它不知道如何将我的变量用作向量...但是当我在函数外部定义变量时,它似乎将它用作向量就好了!
有什么建议吗?
您不应该尝试 def
在函数内部。通常 def
用于 top-level 命名空间值;加载命名空间后(通常)不会更改的内容。
让我们重构一下代码以获得一些不依赖于 top-level/static 命名空间值的可重用函数。我们可以用一个函数来生成 hands:
而不是 (def yourHand ...)
(defn deal-hand []
(map
;;fn
#(cond ;; you have a subtle bug here that sometimes returns nil
;;hearts
(< % 10) (* % -1)
(< % 13) -10
;;diamonds
(< % 23) (* (mod % 13) -1)
(< % 26) -10
;;clubs
(< % 36) (mod % 13)
(< % 39) 10
;;spades
(< % 49) (mod % 13)
(< % 52) 10)
;;list
(take (rand-int 12) (repeatedly #(inc (rand-int 52))))))
然后如果你还想要一个命名空间def
,你可以这样得到它:
(def your-hand (deal-hand))
我们可以将您的 reduce
包装在另一个 需要帮助的函数中 :
(defn score-hand [hand] ;; I made a few simplifications here, but logic is the same
(reduce + (mapv #(if (= 1 %) 1 0) hand)))
现在您有两个可重复使用的函数,可以生成手牌并得分:
(deal-hand)
=> (-10 -9 -9 -10 -3 7)
(deal-hand)
=> (8 -2 10 -9 1 2 -10 3 nil 5)
(score-hand (deal-hand))
=> 1
如果您需要在程序的其他部分使用手,请考虑如何构建函数以将手作为输入,以及手如何在程序的函数中流动。
我有这个变量,手,当我自己定义它时它工作得很好。
(def yourHand
(map
;;fn
#(let [x %]
(cond
;;hearts
(< x 10) (* x -1)
(< x 13) -10
;;diamonds
(< x 23) (* (mod x 13) -1)
(< x 26) -10
;;clubs
(< x 36) (mod x 13)
(< x 39) 10
;;spades
(< x 49) (mod x 13)
(< x 52) 10
))
;;list
(take (rand-int 12) (repeatedly #(+ 1 (rand-int 52))))))
我想在这里的这个函数中使用这个变量。当我先定义变量然后在函数中使用它的名称时,这很好用。
(reduce + (vec (map #(let [x %]
(cond
(= x 1) 1
:else 0
))
yourHand)))
当我尝试在函数中定义变量时出现问题,就像这样。
(reduce + (vec (map #(let [x %]
(cond
(= x 1) 1
:else 0
))
(def hand
(map
;;fn
#(let [x %]
(cond
;;hearts
(< x 10) (* x -1)
(< x 13) -10
;;diamonds
(< x 23) (* (mod x 13) -1)
(< x 26) -10
;;clubs
(< x 36) (mod x 13)
(< x 39) 10
;;spades
(< x 49) (mod x 13)
(< x 52) 10
))
;;list
(take (rand-int 12) (repeatedly #(+ 1 (rand-int 52)))))))))
如果不是因为两件事,这就没有必要了。首先,如果可能的话,我想把这个程序压缩成一个函数(我认为它/是/可能的!)。其次,我需要在我的程序中的另一个位置使用这个变量,所以我需要能够以某种方式引用它。
无论如何,当我尝试计算上述函数时,它抱怨说它不知道 "how to create ISeq from: clojure.lang.Var"。 (这是错误:IllegalArgumentException Don't know how to create ISeq from: clojure.lang.Var clojure.lang.RT.seqFrom (RT.java:542))我假设这意味着它不知道如何将我的变量用作向量...但是当我在函数外部定义变量时,它似乎将它用作向量就好了!
有什么建议吗?
您不应该尝试 def
在函数内部。通常 def
用于 top-level 命名空间值;加载命名空间后(通常)不会更改的内容。
让我们重构一下代码以获得一些不依赖于 top-level/static 命名空间值的可重用函数。我们可以用一个函数来生成 hands:
而不是(def yourHand ...)
(defn deal-hand []
(map
;;fn
#(cond ;; you have a subtle bug here that sometimes returns nil
;;hearts
(< % 10) (* % -1)
(< % 13) -10
;;diamonds
(< % 23) (* (mod % 13) -1)
(< % 26) -10
;;clubs
(< % 36) (mod % 13)
(< % 39) 10
;;spades
(< % 49) (mod % 13)
(< % 52) 10)
;;list
(take (rand-int 12) (repeatedly #(inc (rand-int 52))))))
然后如果你还想要一个命名空间def
,你可以这样得到它:
(def your-hand (deal-hand))
我们可以将您的 reduce
包装在另一个 需要帮助的函数中 :
(defn score-hand [hand] ;; I made a few simplifications here, but logic is the same
(reduce + (mapv #(if (= 1 %) 1 0) hand)))
现在您有两个可重复使用的函数,可以生成手牌并得分:
(deal-hand)
=> (-10 -9 -9 -10 -3 7)
(deal-hand)
=> (8 -2 10 -9 1 2 -10 3 nil 5)
(score-hand (deal-hand))
=> 1
如果您需要在程序的其他部分使用手,请考虑如何构建函数以将手作为输入,以及手如何在程序的函数中流动。