I have a vector of hash-maps (def my-data [{:id "1" :value "a"} {:id "2" :value "b"}])
. Now I want to lookup an :id
and change the respective :value
.
This does not work:
(map (fn[x] (if (= :id "1")
(update x :value "c")
x))
my-data)
That looks even better as in my real example I use :keywords for the lookup.
https://github.com/redplanetlabs/specter is really good at all these mutations
took some getting used to, but super powerful
Thanks @tws but my goal is to understand the basic functions on seqs better - especially for nested structures this is still brainteasing. π
I second the idiomatic
{"key1" {:name "jax"}
"key2" {:name "jin"}
"key3" {:name "jan"}}
style, because it's super easy to assoc new maps by giving them an unique key, and easy to update-in, assoc-in, and get-indissoc "key1" also removes the whole entry, quite handy.
hint: (= :id "1")
always returns the same result
got it. (if (= (:id x) "1")
works better π
I think that looks pretty good, you can look at destructuring map arguments and the for
macro to make it more concise
also maybe cond->
You can also write #( %)
instead of (fn [x] x)
(map #(if (= "1" (:id %)) (assoc % :value "c")) le-data)
an "anonymous function"
if i'm running something in a doseq
with a (Thread/sleep ...)
between loops, how do I ctrl-c or otherwise interrupt the loop?
this is using lein repl
, so it's an nrepl instance
I want to write out (and read) a nested data structure. Basically the EDN format would be perfect, only prn-str
writes everything in one line. Is there a way to get a more readable structure (eg. like Firefox does with JSON raw data)?
https://www.clojure-toolbox.com/ also shows: https://github.com/brandonbloom/fipp https://github.com/AvisoNovate/pretty If you need other options.
I tried pprint but it somehow did not work because the outermost structure was a vector. After changing this to a hashmap (what I wanted to do anyway) now it really looks pretty. π
@oliver.heck if you're printing large datastructures, you should be aware of clojure.core/*print-length*
and clojure.core/*print-level*
- if those dynamic vars are bound, you may be surprised when pprint prints something that you can't read back in π
and if you're doing this a lot, fipp
is nice and faster than pprint; in fact, it's useful to even set it as the default pretty printer in your CIDER repl, etc.
Editors like Emacs/Cider has direct support for Ctrl-C. Or maybe you can just wrap it with future and use future-cancel
Will check it out, thank you!
so lein repl
should not be used on its own?
i want to output nested html for a nested structure.
good time to use clojure.walk?
Sounds more like an application of Hiccup.
Here's an example input and desired output to get more concrete.
;jars-and-lids-to-html
(def datar [{:jar "hax" :lid "##"} {:jar "snax" :lid "##"} {:jar "slax" :lid "##"}])
desired output:
<html>
<span class='jar'>hax <span class='lid'>##</span> </span>
<span class='jar'>snax <span class='lid'>##</span> </span>
<span class='jar'>slax <span class='lid'>##</span> </span>
</html>
Make sure to use hiccup2.core
instead of hiccup.core
. The latter doesnβt HTML-escape strings by default.
That looks quite achievable with Hiccup, just map over the collection with hiccup vectors for each element. You can use eg. [:span.jar ...]
shorthand for classes
Brilliant. Thank you! π
kinda like (map (html [:span.jar (:jar %)]) datar)
?
You generally want just one invocation of the html
macro at the top level, so something like
(html [:html (for [{:keys [jar lid]} datar] ...)])
Hmm.. I don't exactly get it but I'll play around with it
(hiccup.core/html
[:html
(for [{:keys [jar lid]} datar]
[:span.jar jar
[:span.lid lid]])])
;; => "<html><span class=\"jar\">hax<span class=\"lid\">##</span></span><span class=\"jar\">snax<span class=\"lid\">##</span></span><span class=\"jar\">slax<span class=\"lid\">##</span></span></html>"