Fork me on GitHub

I want (dotimes [i 3] (something i)) to accumulate the result where the iterator is relevant - is (map something (range 3)) the best bet?


(for [i (range 3)] (something i)) is also a possibility

thanks3 1

There's also map-indexed which can useful in situations like this.


user=> (defn something [n x] (* n x))
user=> (map-indexed something [2 4 8 16])
(0 4 16 48)


Is there a way to call a function from a macro in clojurescript? I'm looking for something like

(defn do-foo [x] x)
(defmacro foo [x] `(do-foo ~x))
Right now I put the above in a clj file and in the clojurescript file I repeat the function
(ns foo (:require-macros [foo]))
(defn do-foo [x] x)
Is there a way to avoid the duplication?

Georgi Stoyanov11:09:36

Hi guys 🙂 I'm learning Clojure at the moment with Clojure for the brave and true. So far so good, I'm writing one of the exercises and it's pretty strange to me why it's not working. I need to write a function that is mapset -> works like map, but returns a set. Ex: mapset inc [1 1 2 2] => #{2 3} I've managed to make it to this state, but it throws an exception "Error printing return value (IllegalArgumentException) at clojure.lang.RT/seqFrom ( Don't know how to create ISeq from: java.lang.Long" Then I've changed the seq to be in brackets ( "[seq]") and this returns #{2}. seems to me like the map is applied only to the first value, not to the whole sequence. What could be the reason for that?

(defn mapset
  [expr [seq]]
  (into #{} (map #(expr %) seq))
(mapset inc [1 1 2 2])


don't use destructuring for the collection you want to use as is later. just remove square brackets from arguments form of your function

Georgi Stoyanov11:09:01

yep, that one worked. Silly me


and one thing, #(expr %) is equal to just expr. So just map the function expr and not an anonymous function that calls expr on an arg

Sam Ritchie20:09:35

If you move one paren you can use a transducer version and avoid an intermediate sequence!

(defn mapset
  [expr seq]
  (into #{} (map expr) seq))

(mapset inc [1 1 2 2])

🙌 1

Howdy all - I have an :env/dev alias that I use to load some basic repl features - how can I apply this to all namespaces in my project?

(ns user
   [ahungry.overdexer :as od]))

(defn go []
  (apply require clojure.main/repl-requires))



I want to be able to do (doc something) regardless of what ns I am in


Hmm, maybe if it helps future readers, I just worked around it with my editor:

(defun my-cider-repl-set-ns ()
  "Like cider-repl-set-ns, but better."
  (call-interactively #'cider-repl-set-ns)
  (call-interactively #'cider-switch-to-repl-buffer)
  (cider-interactive-eval "(clojure.core/refer-clojure)")
  (cider-interactive-eval "(clojure.core/apply clojure.core/require clojure.main/repl-requires)"))
The historic answer I found wasn't satisfactory ( for the goal


What editor are you using?


Cider or inf clojurr?


cider - it's having some funky behavior though - even though at first glance it seemed to work, it swaps back to 1 prior NS on the stack of ones I've went through - I tried subbing the cider-interactive-eval calls for cider-insert-in-repl - still being strange...almost a good solution, just gotta polish it a bit more 😄


like, the repl shows my.other.ns> but when I eval a form, it goes back to my.prior.ns>


Weird. The reason I asked is that cider provides actions for those so you don't need to have doc in the current namespace


this guy seems to work, but sure is ugly - it leaves the forms unsubmitted:

(defun my-cider-repl-set-ns ()
  "Like cider-repl-set-ns, but better."
  (call-interactively #'cider-repl-set-ns)
  (call-interactively #'cider-switch-to-repl-buffer)
  (cider-insert-in-repl "(clojure.core/refer-clojure)" nil)
  (cider-insert-in-repl "(clojure.core/apply clojure.core/require clojure.main/repl-requires)" nil))


hah yea, C-c C-d C-a or C-c C-d C-d and friends? That works well if I've entered a thing at point, but sometimes I like to do exploratory stuff locally and not just google it..


like, (doc pmap) or (source pmap), regardless of where I'm hacking at in the repl


(if anyone cares, the first solution works if the interactive eval calls are staggered by 1 second with a run-with-timer - I think cider has some race condition / async going on, and it was effectively evaluating those forms before the NS switch was totally done)


What's the advantage of (doc pmap) over cider show doc?


Like type pmap and press C-c C-d C-d ?


to me, it feels more natural to just type (doc whatever) when I'm already in the repl (rather than go back over to the source and do there)


if there was a solution outside of the editor approach, it would have been nice for the purposes of being able to do in inf-clojure or even straight from cli with clj


What you're doing is exactly what I do for inf clojure. Just a keybinding to require the repl requires


Oh, I guess since I never type in the REPL buffer, I assumed you could also show doc on a symbol in it just like other clojure buffer. Weird that it doesn't. What you can do is define a doc function in your user.clj, and then you can do (user/doc ...) from anywhere. In theory you can also just do (clojure.repl/doc ...) but I admit that's a lot of typing. The last thing you can do, but it's more involved, is write a nRepl middleware that auto-requires it on ns switch.