简体   繁体   中英

Clojure: I am trying to use 'some' instead of 'doseq' but I am not really sure how to use it

How to replace the "doseq" with "some" in this scenario. I am new to 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

The Clojure docs for doseq and some are loaded with examples that can help you figure out what to use and how to use it.

There are several things I don't know about your situation, but maybe I can help with these examples.


some

Detects if something exists based on a condition. Returns the result of the predicate, if the predicate returns truthy.

Takes a predicate and a collection

Predicate examples:

#(= 2 %) ; Equals 2
(fn [val] (= val "user3438838")) ; equals your username

Collection examples:

[1 2 3 4 5 6 7 8]
["user3438838" "programs" "in" "Clojure"]

Let's evaluate the combinations of these:

(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


doseq

Implement an expression for all elements of a sequence, for side effects. This is the first function I looked for when coming from JS, but it's usually not the right thing (it doesn't take advantage of lazily evaluating, decreasing performance). Generally want to apply a recursive expression, like loop with recur , but doseq may make sense here.

We'll take the same approach as with some

doseq takes (a) sequence (s) and and expression that ostensibly uses each element of the sequence.

Sequence examples:

[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)]

Body expression examples:

(println x)
(println (str "The number/word is: " x))

And now we'll combine these:

(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 ..."

Hope this helps you understand the two.

And if you're new, I think the go-to book for learning Clojure is Daniel Higginbotham's (2015) Clojure for the Brave and True where he describes some (and not doseq b/c you generally want to use lazily/recursively evaluated expressions).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM