Fork me on GitHub
#clojure
<
2018-03-11
>
wds_05:03:28

I have a nested structure and I want to filter out items based on a nested value in a collection

{:tag :a,
  :attrs {:href "",
          :name "adchoices_logo",
          :style "color:#000001;text-decoration:underline; height:13px; display:inline-block;",
          :target "_blank"},
  :content ({:tag :img,
             :attrs {:alt "AdChoices",
                     :border "0",
                     :src "",
                     :style "display:block;",
                     :title "AdChoices",
                     :width "69",
                     :height "15"},
             :content nil})}
In this case I want to filter out any items that have a :content value of nil

wds_05:03:22

I've been attempting to use clojure.walk for this solution as it seems it would exhaust arbitrary depth, however, I'm at a loss for reconstructing the form

wds_05:03:01

Talking to myself here but I think I figured it out, while also realizing I missed a key detail. nil is returned when a value is not preset in a hash for a given key. So, my "solution" works

(clojure.walk/prewalk #(filter (comp nil? :content) %) links)

wds_05:03:35

but just returns a linked-list of empty linked-lists, as will all cases in this instance 😑 Sorry for the rudimentary question

the2bears06:03:55

'remove' is the analogous opposite to 'filter' if that helps.

rakyi08:03:28

what book would you recommend after reading Clojure for the Brave and True and Programming Clojure?

hmaurer12:03:58

@rakyi I think The Joy of Clojure is very good

hmaurer12:03:12

but it goes over some of the same material as the Brave and the True

rakyi12:03:42

that is the one I was thinking about as well

manuel13:03:10

@rakyi Clojure Applied could be a nice read too.

rakyi13:03:33

will check it, thanks

nathanmarz14:03:31

@wds_: that's easy to do with specter

(def NODES (recursive-path [] p (continue-then-stay :content ALL p)))
(setval [NODES (selected? :content nil?)] NONE data)

wds_15:03:01

I just stumbled on your talk about Specter this morning, beautiful library, thank you for assistance!

nathanmarz14:03:39

that will also be much higher performance than clojure.walk, since it's only traversing parts of your data structure that are relevant

nathanmarz14:03:16

clojure.walk will traverse into every key/value pair and map key

nathanmarz14:03:35

this behavior can also cause surprising bugs when parts of the data structure you weren't expecting are inadvertently affected

qqq14:03:31

I'm trying to run:

(let [img (BufferedImage. 400 300 BufferedImage/TYPE_BYTE_GRAY)
      g2d (.getGraphics img)]
  (.drawString g2d "Hello World" 0 20)
  (ImageIO/write img "png" ( "out.png")))
on a headless jvm, and I get the error of: Could not initialize class sun.awt.X11GraphicsEnvironment

Alex Miller (Clojure team)16:03:57

use -Djava.awt.headless=true

bbktsk16:03:54

Hi, I got surprised by this behavior of clojure.spec: ` (require ’[clojure.spec.alpha :as s]) (s/def :x/a integer?) (s/def :x/x integer?) (s/def :x/m (s/keys :req [:x/a])) (s/explain :x/m {:x/a 12, :x/x “invalid”}) => In: [:x/x] val: “invalid” fails spec: :x/x at: [:x/x] predicate: integer? ` I.e. when a map is validated, spec validates not only keys listed in the map’s spec (`:x/a`), but also keys that are not included but have their own spec defined (`:x/x`). What’s the rationale for that? And is there a way around it? How to say “Hey spec, validate only the keys I have mentioned explicitly in the map’s definition and ignore others”?

sundarj16:03:44

https://vimeo.com/195711510 go to 46:00 to see Rich explaining this very thing

Alex Miller (Clojure team)16:03:31

and no, there is no way not to validate all attributes in keys

qqq18:03:35

I'm using CLJ, not cljs. I need to read a json data in *.js file as a clojure map. Is https://github.com/clojure/data.json the ideal library to use ?

qqq18:03:48

data is small enough that it can all store in memory

noisesmith18:03:51

data.json is OK, cheshire is good too

pyr18:03:50

@qqq cheshire brings in more dependencies, but is substantially faster in most scenarios

qqq19:03:20

I used data.json, turned out ot be fine, my data was only 84k

lwhorton19:03:30

i wanted to throw this out there: one of the worst things about clojure is how well the language is designed. working in pretty much any other language really hammers-home the importance of syntax consistency, least-surprise, operator precedence, true/false consistency, polymorphic api design, etc.. it’s so painful to use anything else after clojure.

lwhorton19:03:21

“clojure: making your job, where you cannot use it, more painful since 2009”

zentrope23:03:31

My (admittedly meager) experience is that fellow developers are far more conservative and resistant than managers.