how would I mutate a string in clojure, get the result, and then mutate the result in a loop?
i think this is doable with a reduce, but the syntax is a bit clumsy and think there may be a better way to do this
java.lang.Strings are immutable, so you aren't going to be mutating them
i mean mutate as in reassign
local bindings are also immutable
I would just use loop/recur to answer the question I think you asking
in an imperative language, I would use a for loop and reassign a variable:
var x = "hello world";
var arr = ["l", "o"];
for (y in arr) {
x = x.replace(y, "")
}
what is the best way to do this same operation in clojure?
Oh, reduce, definitely
That for loop is a good over arr
A fold
(reduce #(.replace %1 %2 "") "hello world" ["l" "o"])
so like this then?
is there anything like reduce that has the function as the last argument? it becomes a bit difficult to read when the arguments provided are place after the functionality
Thanks. Not sure to see the plus side of using cljs here though. Will have to tinker a bit. Enough reading, coding time.
Use a thread last macro
Eg (->> 10 (range) (filter odd?) (map #(* 2 %)) (reduce +))
Hi. I need to write spec (for reitit route coercion, but maybe it didn't matter). The problem is - my parameter contains square brackets, like ...&page[p]=10...
For simple type checking (symbol "page[p]") int?
works just fine, but when I try to write a more complex rule using s/def
I got errors about namespace. I need to check the type and make the parameter optional. For simple symbols (without those brackets) all work just as needed.
i.e.
That works
(s/def ::level int?)
(s/def ::page int?) ;no brackets
(s/def ::query-params (s/keys :req-un [::level] :opt-un [::page]))
That works but checks only the type
inside route data {:get {:parameters {:query :level int? (symbol "page[p]" int?)}...
That not
(s/def ::level int?)
(s/def (symbol "page[p]") int?) ; got error
(s/def ::query-params (s/keys :req-un [::level] :opt-un [(symbol "page[p]")]))
s/def
is a macro so it's not going to evaluate that form.
The error Assert failed: k must be namespaced keyword or resolvable symbol (c/and (ident? k) (namespace k))
occurs when I try to evaluate the spec in the repl, or when call the router
Yes, because s/def
is a macro and it expects a qualified keyword or symbol -- not a list. Macros do not evaluate their arguments.