Fork me on GitHub

not sure how to ask.Can i get stuff out of dotimes? simplistic example…

(defn something
    (dotimes [n count]
      ; do something with a side effect
      (+ total-ran 1)
    ("Total: " total-ran)
so that
(something 5)
-> "Total: 5"

Matthew Davidson (kingmob)01:01:52

@simon223 If you look at the dotimes macro (, you can see the body of the loop is a when that always ends in a recur call, so it’ll always return nil when the iterations are done. dotimes is primarily for side effects. If you want to just return the number of iterations, you can do that from the count var, but that’s not very useful, assuming not every iteration has a side effect. What you might want is something like this:

(defn something
  (loop [n count
         total-ran 0]
    (if (< n 0)
      (str "Total: " total-ran) ; return final result
      (if (even? n) ; some test to determine whether to do something
          (println "Did something with" n)
          (recur (dec n) (inc total-ran)))
        (recur (dec n) total-ran)))))


@kingmob thankyou!

👍 5
Matthew Davidson (kingmob)03:01:10

Happy to help. Feel free to reach out if you have any other questions


is a binding basically a pointer/reference?


A binding gives a name to a value. Each time you recur you give the same names to new values.


A top-level name, bound with def (or defn) is more like a pointer/reference since a Var is created -- the name is bound to the Var and the Var "contains" the value. And you can alter what the Var contains (`alter-var-root`, for example, or just calling def again).


thanks @U04V70XH6 and @kingmob i got what i needed with something like:

(defn do-stuff [total]
    (loop [results []]
      (if (= (count results) total)
          (let [new-thing (do-something)]
            (recur (conj results new-thing))))...


(take total (repeatedly #(do-something))) should be about the same.

👍 5

is there a type of prettier for clojurescript? where on save the file reformats automatically like this:


emacs with clojure-mode and aggressive-indent?


Thank you!

Karol Wójcik13:01:03

How can I destructure extra_info keys from the map?

  :categories ["A"]
  :attributes {
      :tags ["tag1"] 
      :unit_price "1"
      :extra_info {
         :neutrient_elements ["elem1"]

I tried something like
[{:keys [categories] :as product
    {:keys [tags unit_price] :as attributes} :attributes
    {:keys [nutrient_elements] :as extra_info} [:attributes :extra_info]}]
and it seems to work for attributes but does not destructure the extra_info 😞


you'll have to destructure it inside your attributes map

❤️ 5

[{:keys [categories] :as product
  {:keys [unit-price tags] :as attributes
   {:keys [nutrient-elements] :as extra-info} :extra-info} :attributes}]


but at this level of nesting it's probably more readable to use get-in than use destructuring


if i have [{:type "cat" :age 5}, {:type "dog" :b 6}] how can i get a list like ("cat", "dog")?


a little uncertain about my square/round brackets there, but my squigglies are definite 😅


Bit of feedback on the above, just for context: You can express it either as a [vector] or a '(list) (note the quote so that it doesn’t try to interpret (list) as a function). Either works equally well in this case; it seems to be more common to use vectors for literals. They have different performance characteristics, but those aren’t going to matter unless you’re dealing with a large number of elements. One way to think about it is that they’re both sequences (ie fulfill the sequence interface), and a ton of functions in Clojure operate on sequences, without caring what the concrete type is.


@U077BEWNQ the “sequence interface” makes sense, I generally keep forgetting what’s what after i google it 😛


(map :type <your list>)


ah i was close with map.. thanks!


map is clearly where I need to get my skates on


maybe i mean transducers

Oliver Marshall16:01:28

@simon223 If you're just starting out you'll probably have a better time focusing on the classics like map, filter and friends before moving on to transducers

👍 5

Definitely starting out… sooo. i feel bad i couldn’t work this out, because it feels simple… but i’ve got beginners amnesty? Trying to turn:

{:First "foo", :Second "bar"}
  [:eq "foo"]
  [:eq "bar"]}
My latest non-working example.
output ((fn [[k v]] [:eq v]) (into [] input))]
im just not quite getting how to process/transform items in a map 😕


ok i think i’m … close

Oliver Marshall17:01:39

I PM'd you if you still need help btw 🙂


you can use map or for with into {} or reduce-kv


(into {} (for [[k v] {:First "foo", :Second "bar"}] [k [:eq v]]))


there are also utility libraries which implement map-vals for mapping over values in a map, which clojure doesn’t have built in


do you understand why your example doesn’t work?


ah i got similar.. yeah i did mostly understand why my examples weren’t working from the errors.


I got

output (into {} (for [[k v] input] [k [:eq v]]))]


have a look at update also (edit: oops, misread the question)

👍 5

looks same?


that ended up as simple as i hoped 😄


you can also use reduce and be lazy: (reduce #(assoc %1 (first %2) [:eq (last %2)]) {} {:First "foo", :Second "bar"})

Lennart Buit19:01:31

(you can do code formatting with backticks (` ), how to escape backticks is beyond me for now)


k, thnx.. can u edit messages?

Lennart Buit19:01:04

yes, to the right is a menu with three dots

Lennart Buit19:01:29

OR, when it is the last message you wrote, you can simply press arrow up


ok, thnx