beginners

Getting started with Clojure/ClojureScript? Welcome! Also try: https://ask.clojure.org. Check out resources at https://gist.github.com/yogthos/be323be0361c589570a6da4ccc85f58f.
Oliver 2021-04-18T04:34:38.199300Z

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)

Oliver 2021-04-18T17:26:30.204900Z

That looks even better as in my real example I use :keywords for the lookup.

tws 2021-04-19T13:53:29.218700Z

https://github.com/redplanetlabs/specter is really good at all these mutations

tws 2021-04-19T13:53:41.219Z

took some getting used to, but super powerful

Oliver 2021-04-21T16:38:41.291100Z

Thanks @tws but my goal is to understand the basic functions on seqs better - especially for nested structures this is still brainteasing. πŸ˜‰

sova-soars-the-sora 2021-04-25T03:28:29.066200Z

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-in

sova-soars-the-sora 2021-04-25T03:28:51.066400Z

dissoc "key1" also removes the whole entry, quite handy.

yuhan 2021-04-18T04:48:59.199400Z

hint: (= :id "1") always returns the same result

πŸ‘ 1
Oliver 2021-04-18T04:49:47.199600Z

got it. (if (= (:id x) "1") works better πŸ˜€

βž• 1
yuhan 2021-04-18T05:09:36.200400Z

I think that looks pretty good, you can look at destructuring map arguments and the for macro to make it more concise

πŸ‘ 1
yuhan 2021-04-18T05:10:51.200600Z

also maybe cond->

πŸ‘ 1
sova-soars-the-sora 2021-04-18T05:59:05.201200Z

You can also write #( %) instead of (fn [x] x) (map #(if (= "1" (:id %)) (assoc % :value "c")) le-data)

sova-soars-the-sora 2021-04-18T05:59:13.201400Z

an "anonymous function"

cpdean 2021-04-18T11:46:22.202800Z

if i'm running something in a doseq with a (Thread/sleep ...) between loops, how do I ctrl-c or otherwise interrupt the loop?

cpdean 2021-04-18T11:46:38.203200Z

this is using lein repl, so it's an nrepl instance

Oliver 2021-04-18T17:32:30.207100Z

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)?

phronmophobic 2021-04-18T17:33:36.207200Z

https://clojuredocs.org/clojure.pprint/pprint

πŸ‘ 1
phronmophobic 2021-04-18T17:36:27.207400Z

https://www.clojure-toolbox.com/ also shows: https://github.com/brandonbloom/fipp https://github.com/AvisoNovate/pretty If you need other options.

πŸ‘ 1
Oliver 2021-04-18T17:45:53.208100Z

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. πŸ˜‰

1
pithyless 2021-04-18T18:22:52.208800Z

@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 πŸ™‚

πŸ‘ 1
pithyless 2021-04-18T18:23:40.209Z

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.

πŸ‘ 1
jumar 2021-04-18T18:25:04.211100Z

Editors like Emacs/Cider has direct support for Ctrl-C. Or maybe you can just wrap it with future and use future-cancel

Oliver 2021-04-18T18:28:16.211500Z

Will check it out, thank you!

cpdean 2021-04-18T19:41:09.211700Z

so lein repl should not be used on its own?

sova-soars-the-sora 2021-04-18T20:58:58.212100Z

i want to output nested html for a nested structure.

sova-soars-the-sora 2021-04-18T20:59:05.212300Z

good time to use clojure.walk?

seancorfield 2021-04-18T21:17:40.212600Z

Sounds more like an application of Hiccup.

sova-soars-the-sora 2021-04-18T21:26:45.213600Z

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>

2021-04-19T11:09:58.215700Z

Make sure to use hiccup2.core instead of hiccup.core. The latter doesn’t HTML-escape strings by default.

πŸ‘οΈ 1
yuhan 2021-04-18T21:35:05.213800Z

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

sova-soars-the-sora 2021-04-18T21:38:36.214Z

Brilliant. Thank you! πŸ˜ƒ

sova-soars-the-sora 2021-04-18T21:40:03.214200Z

kinda like (map (html [:span.jar (:jar %)]) datar) ?

yuhan 2021-04-18T21:44:43.214500Z

You generally want just one invocation of the html macro at the top level, so something like (html [:html (for [{:keys [jar lid]} datar] ...)])

sova-soars-the-sora 2021-04-18T22:12:05.214700Z

Hmm.. I don't exactly get it but I'll play around with it

yuhan 2021-04-18T23:16:04.214900Z

(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>"