Fork me on GitHub
#beginners
<
2020-11-30
>
william11:11:06

how could I find all the keywords in an arbitrarily nested data structure (which has sets, arrays, etc inside)?

william14:11:45

I have problems with this in clojurescript when the map contains strings, because: (seq "a") evaluates to ("a")

Schmoho19:11:45

something like this

(let [a (atom [])]  
  (clojure.walk/prewalk #(if (keyword? %) (do (swap! a conj %) %) %) [#{{:a '(1 :b)}}])
  @a)
;; => [:a :b] 
seems like a straightforward way to what I understand your first question to be, however I don't understand your addition to it. there is probably some nicer way to do it though

Schmoho19:11:32

(->> [#{{:a '(1 :b)} :c} :d]
     (tree-seq seqable? identity)
     (filter keyword?))
;; => (:a :b :c :d)
is another basically you'll want something like walk or tree-seq

octahedrion13:11:50

(filter keyword? (tree-seq seqable? seq my-map))

šŸ‘ 1
1
william14:11:05

I have problems with this in clojurescript when the map contains strings, because: (seq "a") evaluates to ("a")

Michaƫl Salihi13:11:40

Hi, is there a good one-liner to get this result? Someone see another better solution? More idiomatic and/or concise?

(->> [1 2 3 4 5] (filter even?) (mapv #(* % 10)))
;; => [20 40]

dgb2314:11:56

your code is fine! transducer version: (into [] (comp (filter even?) (map #(* 10 %))) [1 2 3 4 5])

dgb2314:11:27

list comprehension version: (into [] (for [n [1 2 3 4 5] :when (even? n)] (* 10 n)))

Michaƫl Salihi14:11:29

Perfect, thanks for these solutions!@U01EFUL1A8M

Michaƫl Salihi14:11:01

I was just thinking of a version with comp, I like it.

dgb2314:11:26

The comp version produces a transducer, which evaluates eagerly. See: https://clojure.org/reference/transducers

scythx14:11:34

Hello, i need to create simple API web server for my vue.js App. The server only need auth & crud stuff. I don't really understand about web security. So is it recommended to use only reitit and add some security stuff (via library if there is any, or implement myself (hopefully there's some resources i could read)) or use a framework such as luminus/pedestal?

Michaƫl Salihi14:11:06

Hi @raefaldhiamartya First of all, you can watch this video https://www.youtube.com/watch?v=CBL59w7fXw4 It's not recent so the libraries (friend, buddy, etc) have evolued but the main security concerns are still relevant. I recommend reading also this posts https://purelyfunctional.tv/article/clojure-web-security/

scythx14:11:07

thank you! this is what i need exactly!

šŸ‘ 1
Michaƫl Salihi14:11:08

You"re welcome! If you using Reitit Ring so I recommanded to adding this Ring middleware: https://github.com/ring-clojure/ring-defaults > This library attempts to automate the process, by providing sensible and secure default configurations of Ring middleware for both websites and HTTP APIs.

scythx14:11:54

okay, i'll take a look at that

Michaƫl Salihi17:11:21

@raefaldhiamartya Last but no least, @UGJ8075QQ just post the third part of video tutorials series about how to using Buddy auth library. https://youtu.be/FcxO5VCPLi4

ā¤ļø 2
mm15:11:34

hey when you are reducing something with update-in conj. like (reduce #(update-in %1 [:a] conj %2) {} ["a", "b", "c"]) how can I have control over final output type, conj by default doesnt put into vector

dpsutton15:11:46

common to see (fnil conj []) as the updating function. you could also use {:a []} as your initial value

mm15:11:32

thanks for fnil, seems bit awkward, i provided static key, but its actually dynamically mapped based on initial-acc

dpsutton15:11:54

i'm not sure what seems awkward. its quite common from my experience. and its purpose is what value to provide in the event of a nil argument, which is pretty much exactly your situation. it seems quite tailored to your current requirement

mm15:11:04

fair enough, thanks šŸ™‚