beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
clj8394 2021-07-06T01:16:41.077500Z

how would I mutate a string in clojure, get the result, and then mutate the result in a loop?

clj8394 2021-07-06T01:17:38.079500Z

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

2021-07-06T01:17:46.079800Z

java.lang.Strings are immutable, so you aren't going to be mutating them

clj8394 2021-07-06T01:19:20.080200Z

i mean mutate as in reassign

2021-07-06T01:19:52.081200Z

local bindings are also immutable

2021-07-06T01:20:48.082800Z

I would just use loop/recur to answer the question I think you asking

clj8394 2021-07-06T01:21:48.083500Z

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, "")
}

clj8394 2021-07-06T01:21:58.083800Z

what is the best way to do this same operation in clojure?

2021-07-06T01:22:23.084200Z

Oh, reduce, definitely

2021-07-06T01:22:58.085Z

That for loop is a good over arr

2021-07-06T01:23:03.085300Z

A fold

clj8394 2021-07-06T01:23:56.086Z

(reduce #(.replace %1 %2 "") "hello world" ["l" "o"])

clj8394 2021-07-06T01:24:00.086200Z

so like this then?

clj8394 2021-07-06T01:36:42.086900Z

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

Maxime D 2021-07-06T02:36:05.088Z

Thanks. Not sure to see the plus side of using cljs here though. Will have to tinker a bit. Enough reading, coding time.

2021-07-06T03:09:14.089100Z

Use a thread last macro

2021-07-06T03:10:33.090100Z

Eg (->> 10 (range) (filter odd?) (map #(* 2 %)) (reduce +))

Alexander Moskvichev 2021-07-06T04:55:25.102600Z

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

seancorfield 2021-07-06T04:57:39.103100Z

s/def is a macro so it's not going to evaluate that form.

Alexander Moskvichev 2021-07-06T05:14:53.105600Z

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

seancorfield 2021-07-06T05:31:22.106500Z

Yes, because s/def is a macro and it expects a qualified keyword or symbol -- not a list. Macros do not evaluate their arguments.