This question already has an answer here:
When should you use swap or reset
(1 answer)
Closed 2 years ago.
I want to change the atom, which only holds one boolean to true.
I came up with this workaround, is there something I'm missing?
(defn always-true [x]
"Used to change atom value"
true
)
(def x (atom false))
(swap! x always-true)
Thank you!
there's the reset! function for atoms, for this exact usecase:
(reset! x false)
Related
This question already has an answer here:
MongoDB: Query has implicit limit(256)?
(1 answer)
Closed 4 years ago.
I've a function photos-with-keyword-starting that gets lists of photos for a given keyword from a MongoDB instance using monger, and another that finds subsets of these photos using set/intersection.
(defn photos-with-keywords-starting [stems]
(apply set/intersection
(map set
(map photos-with-keyword-starting stems))))
Previously I thought this worked fine, but since adding more records the intersection doesn't work as expected -- it misses lots of records that have both keywords.
I notice that calls to the function photos-with-keyword-starting always return a maximum of 256 results:
=> (count (photos-with-keyword-starting "lisa"))
256
Here's the code of that function:
(defn photos-with-keyword-starting [stem]
(with-db (q/find {:keywords {$regex (str "^" stem)}})
(q/sort {:datetime 1})))
So because calls to find records in MongoDB don't return all records if there are more than 256, I don't get the right subsets when specifying more than one keyword.
How do I increase this limit?
You could simply convert the datetime in your function photos-with-keyword-starting to for instance a string, if you can live with that.
Alternatively you could remove logical duplicates from your output, for instance like this:
(->>
-your-result-
(group-by #(update % :datetime str))
(map (comp first val)))
This question already has answers here:
Clojure: What does [_] do in a Functions Argument List?
(3 answers)
Closed 4 years ago.
for example we have function:
(defn my-fun [param]
(let [[x _] param]
x))
Why we use "_" in this example?
(let [[x _] [1 2]] x) destructures the first element of a sequence and binds it to the name x. It also binds the second value to the name _, but by convention this name means the value can be ignored.
Note that (let [[x _] [1 2]] [x _]) would technically be valid Clojure, but the semantics of _ is that we just don't care about that value. There is no special treatment of that symbol from the perspective of the compiler, just human convention.
This question already has answers here:
Constructing a map on anonymous function in Clojure
(3 answers)
Closed 5 years ago.
Is there a way to return a map (or vector) like this, with the shorthand notation?
(#({:ok %}) "lol") => {:ok "lol"} ;; this doesn't work
Functionally equivalent longhand example:
((fn [x] {:ok x}) "lol") => {:ok "lol"}
There are couple of alternatives
(#(hash-map :ok %) :yoo)
(#(do {:ok %}) :yoo)
This question already has answers here:
In clojure how to map over overlapping pairs?
(4 answers)
Closed 7 years ago.
say, I have 5 javaScript Objects stored in a vector:
(def v [o1 o2 o3 o4 o5])
Each o them has a method "connect", which gets another object as a parameter.
Manually I would now:
o1.connect(o2);
o2.connect(o3);
o3.connect(o4);
o4.connect(o5);
What would be a good approach to automate this?
only some weird solutions come to my mind: as:
(doseq [[a b] (zipmap (butlast v) (rest v))]
(.connect a b))
is there a better way?
You can use partition:
(doseq [[a b] (partition 2 1 v)]
(.connect a b))
This question already has answers here:
"reduce" or "apply" using logical functions in Clojure
(2 answers)
Closed 8 years ago.
"and" seems to be a macro, so I can't use it in something like (reduce and list-of-booleans)
What should I do instead?
You can wrap it into some lambda like this.
user=> (reduce (fn [a b] (and a b)) '(true true true))
true
user=> (reduce (fn [a b] (and a b)) '(true true false))
false
Of course, you don't need reduce at all, you must use every?:
user=> (every? true? '(true false true))
false