在这种情况下如何用“ some”替换“ doseq”。我是clojure的新手。
(def handle (atom ()))
;; #'user/players
;; conjoin a keyword into that list
(swap! handlers conj [:report "handles"])
;;=> ([:report "handles"])
;; conjoin a second keyword into the list
(swap! handlers conj [:demo "handles2"])
;;=> ([:demo "handles2"] [:report "handle"])
(doseq [[a b] @handlers] (println a "--" b))
;;=> :demo -- handles2
;;=> :report -- handles
oseqq的Clojure文档以及其中的一些示例都可以帮助您弄清楚使用什么以及如何使用它。
关于您的情况,我有几件事不知道,但也许我可以为您提供帮助。
根据条件检测是否存在某种东西。如果谓词返回true,则返回谓词的结果。
带有谓词和集合
谓词示例:
#(= 2 %) ; Equals 2
(fn [val] (= val "user3438838")) ; equals your username
集合示例:
[1 2 3 4 5 6 7 8]
["user3438838" "programs" "in" "Clojure"]
让我们评估这些的组合:
(some #(= 2 %) [1 2 3 4 5 6 7 8]) ; => true
(some #(= 2 %) ["user3438838" "programs" "in" "Clojure"]) ; => nil
(some (fn [val] (= val "user3438838")) [1 2 3 4 5 6 7 8]) ; => nil
(some (fn [val] (= val "user3438838")) ["user3438838" "programs" "in" "Clojure"]) => true
为副作用的序列的所有元素实现一个表达式。这是我从JS获得的第一个功能,但这通常是不正确的(它没有利用延迟评估,降低性能的优势)。通常要应用递归表达式,例如loop
with recur
,但doseq
在这里可能有意义。
我们将采用与 some
doseq
接受一个或多个序列,以及表面上使用序列中每个元素的表达式。
序列示例:
[x ["user3438838" "programs" "in" "Clojure"]]
[x [1 2 3 4 5 6 7 8]]
; Note: Can use multiple [x (range 10) y (range 10 20)]
身体表情示例:
(println x)
(println (str "The number/word is: " x))
现在,我们将这些结合起来:
(doseq [x ["user3438838" "programs" "in" "Clojure"]] (println x)) ; Prints "user3438838\nprograms\nin\nClojure"
(doseq [x ["user3438838" "programs" "in" "Clojure"]] (println (str "The number/word is: " x))) ; Prints "The word is: user3438838 ..."
(doseq [x [1 2 3 4 5 6 7 8]] (println x)) ; Prints "1\n2\n3\n4\n5\n6\n7\n8
(doseq [x [1 2 3 4 5 6 7 8]] (println (str "The number/word is: " x))) ; Prints "The number/word is: 1 ..."
希望这可以帮助您理解两者。
如果您是新手,我认为学习Clojure的首选书是Daniel Higginbotham(2015)勇敢和真实的Clojure,他在其中描述了一些内容(不是doseq
b / c,您通常不希望使用延迟/递归求值的表达式) 。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句