Fork me on GitHub

I want to map the values in a hash map {}. What's the best way to do this? Why isn't there a core library function (unless I'm mistaken)?


You can write your own mapvals:

(defn mapvals [f m]
  (into (empty m) (map (fn [[k v]] [k (f v)])) m))


reduce-kv maybe?

(reduce-kv #(assoc %1 %2 (f %3)) {} m) 


darn you beat me to it 🙂


(defn mapvals [f m] (reduce-kv (fn [m k v] (assoc m k (f v))) (empty m) m))
I made a gist that shows how to walk a map with nested maps and vectors:


there is also:, for deep levels of maps.


I came up with:

(defn hmapv [f map]
  (into {}
        (for [[k v] map]
          [k (f v)])))


When there's only one level of unwrapping in for, we might as well use map:

(defn mapvals [f m]
  (into {}
    (map (fn [[k v]] [k (f v)]) m)))
And we can get rid of the intermediate data (lazy sequence) created by map, by using arity-1 of map that returns a transducer, and arity-3 of into that accepts a transducer:
(defn mapvals [f m]
  (into {}
    (map (fn [[k v]] [k (f v)]))
Finally, if m is a sorted-map, or some other kind of map, the output will still be a hashmap, which is probably not what we want. To fix this, we can use empty:
(defn mapvals [f m]
  (into (empty m)
    (map (fn [[k v]] [k (f v)]))
This way, we're pretty fast and also preserve the type. This doesn't preserve metadata. That's another thing to think about. See also:


Thank you for the in-depth reply. Makes more sense


but I'm thinking it's just an odd missing piece of the core library


So I wondered if I missed something or is this the start of my own util library


Ahh thanks. I'll take a look at these util libraries I'd missed


hi guys, i'm a beginners and i would know how i can made a http request without download a library like cli-http


i not found a simple to do that like

(simple-request :POST "...")


Are you looking for a simpler interface? Or to see what it's doing under the hood?


yes for a simple interface without download a dependency

Alex Miller (Clojure team)13:07:09

well those goals are in conflict :)


Why are you avoiding a dependency?


If you do want to avoid a dep then I suppose you're limited to the core library. Try (slurp "")

Alex Miller (Clojure team)13:07:48

can't do a post that way

Alex Miller (Clojure team)13:07:54

at the bottom they're all using Java so you'd be doing a whole bunch of interop (something like


well, i'm question about that just because i was used to javascript and python that have simples interfaces like request and fetch


but maybe this is a different way that i need to get used to


imho. After doing some python with its requests module, I really wished Java/Clojure any lang had something like this. Then I realised python's requests is a module you have to install, just like http-kit 🙂


pip install requests


I'm not familiar with those, but have you seen these examples? Specifically (http/post "" {:body "hello!"}


It seems quite simple (to me)


There's an extra {:body ...} instead of just passing it. but that's where future options (which there likely will be) would go


yess! i m using this to my study and i like that, maybe using javascript and script languages got me unaccustomed kk


clj-http does a nice job yes.


i'm trying to use a single .cljc file to span 2 projects... the namespace is messing it up so i'd like to make a library of it... i'm not sure how i would make it a usable local library file i could include with :require ... presumably i can compile it and put it on the classpath? not certain


are you using deps.edn or lein?


while i wait i'll explain the differences. If you are using deps.edn this is trivial. Just add a dependency on this new project and give it local coordinates while you develop it. I've also seen examples where this stays local coordinates and always just refer to the sibling projects the whole time. If lein, you can look into lein checkouts to have your local copy while developing. Will have to switch back to an artifact when done developing.


I used this to create a private s3 bucket to host my local artifacts, so other projects could use it


haven't used deps.edn much but that sounds like a more convenient approach. there's one component file i need to stay synced between projects (it's rum/react stuff) so i think this is the way to go... how can i make sure a project is using deps.edn?


it's confusing, i thought it would be pretty smart to have the server and then a nested cljs project for the realtimey stuff, but now if i am trying to share a source file across 2 projects i make a library and every time i update the code i need to require a new library version?


@watchtheblur When I wanted to get fancy with time I looked at the source. Turns out there is not a lot there:

(defmacro time
  "Evaluates expr and prints the time it took.  Returns the value of
  {:added "1.0"}
  `(let [start# (. System (nanoTime))
         ret# ~expr]
     (prn (str "Elapsed time: " (/ (double (- (. System (nanoTime)) start#)) 1000000.0) " msecs"))
So I just wrote my own using that as a starting point.


maybe don't nest them. make them the same project with two build artifacts?


or a single build artifact. a jar that includes your cljs stuff


yeah maybe just de-nesting is the easiest way


Is (comp - compare) a good approach for reversing sort order? It seems to work perfect as far as I can tell, but it's not mentioned in any comment on ClojureDocs which makes me doubt myself.


#(compare %2 %1)


oh misread. that actually looks fine. was thinking it was using - as the compare function


ah, i forgot about swapping the arguments!


now I'm tempted to add both of them to clojuredocs since these tricks are easy to forget


thanks! I'll link that in the clojuredocs example.


Hi all! When I using the next.jdbc.sql/insert! I noticed that I get a return such as {:last_insert_rowid() 5}. I was trying to get the returned id by using something like (:last_insert_rowid() insert-return) but that doesn’t work. Also, I noticed that I cant define the map {:last_insert_rowid() 5} (it gives me a (err) Map literal must contain an even number of forms) Am I getting something wrong here? the return from insert! is not an actual hash map?


this is a case of something creating a keyword programmatically that is not readable by the reader


so you can't type it


you can do (get thing (keyword "last_insert_rowid()"))

👍 6
Aviv Kotek17:07:35

hi, I have a function foo that receives n params (predicates), and i'd like to re-use some of them, is there any way to "def" the params? (defn foo [x & preds]) (foo x :a :b :c) i'd want something like that (def base :a :b) => (foo x base :c)


does the order of predicates matter? This would work if the order doesn't matter

(def base [:a :b])
(apply foo x :c base)


if the order does matter, than you can use the slightly clumsier:

(def base [:a :b])
(apply foo x (conj base :c :d :e :f) )

Aviv Kotek17:07:58

yep, forgot about apply, thanks!


@aviv the normal idiom is something like this:

(let [base [:a :b]
      f (apply partial foo base)]
   ... ; assumed multiple uses of f
   (f :c :d :e :f))


you could skip the def of base and the apply and just have f (partial foo :a :b) if that makes sense in your context


oh never mind, you need a hole for x


client.cljs , server.clj , components.cljc ... but the components.cljc has a call to a javascript function in .cljs ... what do I do


because i tried naively including it via require refer and got circular dependency


@sova, there's not enough info to answer your question. Generally, some options are: • move the cljs function into the cljc file • use reader conditionals, depending on what the use case is, there are other options that might be better suited


Thanks @smith.adriane! details: sente project, using rum for components server and clientside; trying to call chsk-send! from components.cljc is not working. will try including it in the file + reader conditional. the whole file is reader conditionals but i'm a little confused on how to make the javascript components have access to my cljs functions


come to think of it, i'm not sure how to include the rum components either


because normal refers don't seem to do the trick


what’s in the components namespace that is used from clj?


ie. what’s the reason to not make components a cljs file


serverside rendering / all the components


figuring it would make more sense to put them in one file because then they can't be out of sync as they were getting...


(the serverside-pre-render and the clientside ui)


I'm not familiar with rum, but you might be able to export ( some functions like chsk-send! so that you can reference them in the server side rendered components


rum does mention server side rendering, but it doesn't give much guidance. maybe there's a #rum channel where you can get rum specific advice for server side rendering


it's not important to have the serverside have js functionality to me, it just has to be an identical component in order to "hydrate" ... which inflates the dry html components with a javascript/reactive thingy without redrawing the whole page. pretty neat. but things gotta line up perfectly


where is chsk-send! being called then? seems like you could just use reader condtionals to remove those calls


it's being called inside the cljs versions of the components, yes i could remove it but how do i make them do stuff?


thanks for your help, i feel like i'm missing something obvious


i'm imagining something like:

[:div {:on-change #?(:cljs (fn [] (chsk-send! my-args))
                     :clj nil)}]


I'm assuming that the rehydration will fill in the correct on-change handler assuming the html matches


oh it will, but that means i have to keep this component inside the client.cljs file and not the .cljc file it seems.


the reader conditional means it should be fine in the cljc file


hmm okay, maybe the error is elsewhere.


you may also need a reader conditional around the require statement that brings in chsk-send!


i'm stumped about how to refer these rum components into the client.cljs


the line in components.cljc is (rum/defc component-name) ... not a (defn ...)


according to, it seems like that it should be fine to have rum/defc in a clj file


that does make sense


i see some projects have {:paths ["src"]} and some have {:paths ["src/clj" "src/cljs" "src/cljc"]}. i know that clj is clojure, cljs is clojurescript, and cljc is common stuff. what are the pros/cons of using one directory structure for all the code, vs separating code out into different directories?


at least, i try and keep a minimal number of files, other projects have different philosophies... for me it's simpler to reason about as "client/server/etc" but some people like having many cljs files or many clj


so you prefer just having src, and not src/clj.?


personally, yes, i think if the number of files started getting large it would make sense to divide it up.


it usually looks like projectname/src/projectname/source.clj(s) for me. more interestingly you can use dots in the project name to create subfolders, you might see that in stuff like ring.util and ring.middleware


I was surprised to see how (), the empty list, behaves with when and if, as below.

(when () (prn "Am I really being printed?"))
;; prints "Am I really being printed?"

(when nil (prn "Am I really being printed?"))
;; does not print anything


Particularly because (true? ()) returns false.


I figure this has something to do when when and if being macros, but it feels like inconsistent behaviour. Could someone help me rationalize it? I think if I understand the reasoning, I'll be able to remember this behaviour better.


(doc true?) might shed some light


note its not truthy?


rather than true?, I think you might be looking for boolean


(doc if) and (doc when) should give some light as well


Thanks, this is exactly what I was looking for. Clearly, "the thing I don't understand" is true? vs. truthy? vs. boolean, and it sounds little research into those will clear this up.


The docstring of if gives the definitive answer here. The rest are some helper functions that might help you along the way


> Evaluates test. If not the singular values nil or false, evaluates and yields then, otherwise, evaluates and yields else


there are two values that "choose" the else branch, otherwise the then branch is chosen


Great! Someone also wrote a very helpful example on .


clojuredocs is great. i'd recommend always read the docstring first and then look for examples and clarification after reading. your repl and doc are very powerful tools

👍 6