Fork me on GitHub
#beginners
<
2021-07-15
>
sova-soars-the-sora00:07:47

@josh604 could you explain the (juxt key f) part?

hiredman00:07:52

(into {} (map (juxt key (comp f val))) some-map) is the correct snippet

👍 4
sova-soars-the-sora00:07:01

It basically applies (key) and (f) to each Val because “mapping over a map” actually goes KV pair-by-pair?

sova-soars-the-sora00:07:39

oh Okay… melding into brain … 😄

sova-soars-the-sora00:07:33

(juxt key (comp f val)) will return segments like :a 48 :n 96 so we add (into {} …) to make it a map ?

sova-soars-the-sora00:07:12

Juxt looks super useful I wish I worked it into my flow more

jkxyz00:07:13

@hiredman’s snippet is the actually correct expression and yes, juxt returns a function which will return a vector of the application of each function, so in this case it returns a map entry as a [k v] pair

sova-soars-the-sora00:07:40

okay thank you that is super helpful! 😄

👍 4
sova-soars-the-sora00:07:09

The “will return a vector of…” is not in the docstring

sova-soars-the-sora00:07:16

that cleared up a lot

Fredrik09:07:05

Maybe you are looking at some other docstring, but is says on https://clojuredocs.org/clojure.core/juxt that juxt returns a function that "returns a vector containing the result of applying each fn to the args"

teodorlu11:07:55

Which one do you prefer to read? Or is there a better third option? (1️⃣ / 2️⃣ / comment)

;; 1
  (->> @eventlog
       (filter (fn [e]
                 (contains? #{:create :modify}
                            (-> e :e :kind))))
       count)

  ;; 2
  (->> @eventlog
       (filter #(#{:create :modify} (-> % :e :kind)) )
       count)

1️⃣ 6
2️⃣ 4
dgb2312:07:34

I like using the set as a function instead of contains of the second version. But i dont like the function shorthand there, it’s slightly confusing.

☝️ 4
teodorlu12:07:21

Like this?

;; 3
(->> @eventlog
     (filter (fn [e]
               (#{:create :modify} (-> e :e :kind))))
     count)

👍 4
delaguardo12:07:10

(count (filter #(-> % :e :kind (#{:create :modify})) @eventlog))
another one

lispyclouds12:07:52

maybe even?

(->> @eventlog
     (map #(-> % :e :kind))
     (filter #{:create :modify})
     count)

Stuart12:07:07

Does this result in one more intermediate collections being created ? COmpared to the original ?

Stuart12:07:22

Or does map being lazy solve that? I'm still a bit shakey on laziness

lispyclouds12:07:04

yeah the map will produce a lazy seq, theres an extra seq but numbers of things processed should be same

delaguardo12:07:30

yes, it will create intermediate collection. To avoid it - replace ->> with transducer equivalent

(count (sequence (comp (map ...) (filter ...)) @eventlog)

2
👍 2
delaguardo12:07:06

laziness has nothing to do with intermediate collections

👍 2
Santiago15:07:50

is it possible to conj two maps making sure the first map will always be first in order in the final map? Example:

(conj {:uuid 123} {:a 234 :b 35 :z 345})
;=> {:uuid 123 :a 234 :b 35 :z 345}
This small example actually works, but some larger maps conjoin with strange ordering

alexmiller15:07:10

maps are unordered

alexmiller15:07:14

so, no

👍 2
sova-soars-the-sora15:07:36

@slack.jcpsantiago there is something by amalloy you might find interesting … https://github.com/clj-commons/ordered

👍 2
🙏 2
ChillPillzKillzBillz17:07:45

I am using figwheel for developing frontend. I've got a few nested reagent components. How do you access the states of the components at runtime using repl...? I tried add-watch function... it breaks my component!!

sova-soars-the-sora17:07:30

You want to see component state in the REPL ? I am pretty sure you can see it in the browser with the extra dev tools … REPL I’m not sure about

ChillPillzKillzBillz20:07:33

Hi, Sorry to bother you. Is there an example of using devtools for debugging active run... ?

Geoffrey Gaillard21:07:44

No problem 🙂 cljs-devtools just pretty prints cljs data structures to the javascript console. Without cljs-devtools :

(js/console.log {:a 1}) ;; => prints gibberish 
With cljs-devtools
(js/console.log {:a 1}) ;; => nice explorable object you can interact with from your console
To inspect your component state at runtime, just js/console.log it and cljs-devtools will present it well.

👍 2
roelof18:07:55

With ruby I had to make a tic tac toe game which works at a prompt. Is there a tutorial how to make it work in clojure ?

dpsutton18:07:05

i think brave and true has a chapter where you make a command line game right?

dgb2318:07:40

I’m trying to use clojure.xml/parse but I keep getting a file not found as I’m passing a string through an InputStream. it points at a path that is in the doctype of the xml like so:

dgb2318:07:12

(-> xml-string .trim .getBytes .ByteArrayInputStream. xml/parse)

dgb2318:07:49

it works w/o the doctype information. what am I missing?

Russell Mull18:07:11

It seems likely that the underlying xml reader is pulling in the dtd automatically, perhaps so it can resolve entity references or something.

Russell Mull18:07:30

This can probably be turned off using one of the options listed in https://clojure.github.io/data.xml/#clojure.data.xml/parse

dgb2318:07:56

ty so much!

Russell Mull18:07:57

The entity-related ones and :support-dtd are the first ones I'd try.

dgb2318:07:06

I guess so

Russell Mull18:07:08

np, xml is a beast. good luck!

dgb2318:07:14

:support-dtd was the one ty!

seancorfield19:07:36

(and just to be clear, you mean clojure.data.xml/parse, not clojure.xml/parse?)

dgb2321:07:52

initially i tried clojure.xml/parse but based on @U7ZL911B3 and @U050ECB92’s advice I switched to clojure.data.xml/parse and turning off dtd support immediatly fixed my problem

2
dgb2321:07:18

I guess was more of a java and xml question but I’m very happy I got help here and got pointed to the other lib!

ghadi18:07:06

clojure.xml is deprecated, FYI - will be more officially marked in next release AFAIK use clojure.data.xml instead

🙏 2
2
roelof18:07:43

@dpsutton thanks, it is another game then I have in mind but I could use some ideas I think

dpsutton18:07:24

sure. but the loop it sets up and input it gets should be very similar to your use case

roelof18:07:43

that is what I also mean

roelof18:07:57

the layout is different

roelof18:07:30

and maybe when I learn react or friends I could make a gui front-end