Fork me on GitHub
#beginners
<
2021-07-25
>
Rob Haisfield00:07:31

Awesome, thank you @smith.adriane for the helpful explanations. That cleared up a lot for me

👍 3
Rob Haisfield00:07:31

Is there equivalent to contains? for finding if a value is present in a map?

contains? Returns true if key is present in the given collection, otherwise
returns false.

didibus07:07:57

You can also use Java interop for this:

(.containsValue {:a 1} 1)
;> true
But this won't be portable to other Clojure dialects, like it won't work in ClojureScript. If you want something portable, better stick to using some.

Rob Haisfield00:07:16

So

(value-contains? {:a 1} 1) => true

seancorfield00:07:22

I'd probably use something like (some (comp #{1} val) the-map)

9
🚀 3
seancorfield00:07:31

user=> (def the-map {:a 1 :b 2})
#'user/the-map
user=> (some (comp #{1} val) the-map)
1
user=> (some (comp #{2} val) the-map)
2
user=> (some (comp #{3} val) the-map)
nil

tschady01:07:19

if you need more than true/false, can use medley.core. see filter-vals, http://weavejester.github.io/medley/medley.core.html

roelof08:07:16

why is this test failing :

(def __ '(5 2 4))

(comment
  (->>
   [2 5 4 1 3 6]
   (drop 2)
   (take 3)
   (map inc))
  
  )

(tests
 (__ (map inc (take 3 (drop 2 [2 5 4 1 3 6])))) := (->> [2 5 4 1 3 6] (drop 2) (take 3) (map inc) (__)))

Geoffrey Gaillard08:07:10

It seems you are calling as a function with ()

roelof08:07:54

you mean the answer is only 5 2 4 without the parentheses ?

Geoffrey Gaillard08:07:35

is a value. You could try (map inc (take 3 (drop 2 [2 5 4 1 3 6])))) :=

Geoffrey Gaillard08:07:36

'(5 2 4) is a list. So calling it like () is equivalent to ('(5 2 4)), meaning "call this list as a function", which fails.

roelof08:07:02

so the answer needs to be onky 5 2 4

popeye11:07:13

({:xAxisValue "MAR 2017", :yAxisValue "NA", :clientYAxisValue 2, :client10p 0, :client90p 0 :shareYAxisValue 6, :share10p 1, :share90p 1}
           {:xAxisValue "APR 2017", :yAxisValue 1, :clientYAxisValue 4, :client10p 0, :client90p 0 :shareYAxisValue 7, :share10p 1, :share90p 1}
           {:xAxisValue "MAY 2017", :yAxisValue 2, :clientYAxisValue 5, :client10p 0, :client90p 0 :shareYAxisValue 8, :share10p 1, :share90p 1}
           {:xAxisValue "JUNE 2017", :yAxisValue 5, :clientYAxisValue "NA", :client10p "NA", :client90p "NA" :shareYAxisValue "NA", :share10p "NA", :share90p "NA"})

tschady12:07:52

user=> (def a '({:x 1 :y 1} {:x 2 :y 2}))
#'user/a
user=> (def b '({:x 1 :z 1} {:x 2 :z 2}))
#'user/b
user=> (map #(apply merge %) (vals (group-by :x (concat a b))))
({:x 1, :y 1, :z 1} {:x 2, :y 2, :z 2})

Lukas14:07:03

Hey, could someone help me to configure emacs+cider pprint? I'm currently facing the problem that my emacs freezes when I try to pprint a long string. I tried (setq cider-print-options '(("length" 50) ("right-margin" 70))) but this does not resolve the issue (or I'm missing something)

zackteo15:07:21

Hello, may I know if a Ring server automatically handles multiple requests? o:

indy15:07:21

Yup, ring’s default jetty web server and most others will have a thread pool to handle concurrent requests. Ring itself though is more like a pattern for working requests at the application level.

Joe15:07:24

Hello, I've set up an http-kit server on localhost:3000 which I'm trying to call from a cljs app on 9090, but I keep getting a Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at . (Reason: CORS header 'Access-Control-Allow-Origin' missing). I have ring-cors set up, but it doesn't look to me like the "Allow" header is being populated. My app looks like this:

(def app
  (-> #'routes
      (wrap-defaults site-defaults)
      (wrap-cors :access-control-allow-credentials "true"
                 :access-control-allow-origin [#""])))
Full file https://github.com/RedPenguin101/Notes_Clojure/blob/master/sente_example/ws_server/src/main.clj curl --head localhost:3000 gives the following, also missing the origin allow
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: ring-session=64c4da9f-5dd2-4b62-9ae6-d1eff297d835;Path=/;HttpOnly;SameSite=Strict
X-Xss-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Content-Length: 0
Server: http-kit
Date: Sun, 25 Jul 2021 15:16:18 GMT
am I not using the wrap-cors correctly? Thanks!

Joe15:07:45

Thanks Cora, I made the change (silly typo), but no dice unfortunately. To be clear, the curl works fine, and returns as I expect it to, just missing the allow header. It's only when I hit it from the browser that it doesn't work.

Joe15:07:13

▶ curl -X OPTIONS localhost:3000 -i    
HTTP/1.1 404 Not Found
Content-Length: 0
Server: http-kit
Date: Sun, 25 Jul 2021 15:48:28 GMT
And
▶ curl  localhost:3000 -i          
HTTP/1.1 200 OK
Content-Type: application/json
Set-Cookie: ring-session=6b7042cf-4a86-484c-bac2-13a0217f349e;Path=/;HttpOnly;SameSite=Strict
X-Xss-Protection: 1; mode=block
X-Frame-Options: SAMEORIGIN
X-Content-Type-Options: nosniff
Content-Length: 17
Server: http-kit
Date: Sun, 25 Jul 2021 15:48:58 GMT

{"Hello":"World"}% 

Joe16:07:47

I see, thanks - I guess I need to read more about the preflight request and how they work

Karol Wójcik16:07:02

Curl will probably always work for preflight

ChillPillzKillzBillz06:07:31

Maybe a late answer... but I had this problem... and after much searching ... I figured out that in the service settings I needed to have ::http/secure-headers {:content-security-policy-settings {:object-src "none"}} as one of the entries... This is for my pedestal server setup tho... so keep that in mind!! Happy Clojuring!!

Joe08:07:53

Thanks ChillPillzKillzBillz, I'll give that a go.

Samuel Couto19:07:22

Hello, how a LocalDateTime is declared using spec-tools?

deleted19:07:10

@samuel.couto92 that seems to work for me? ^

Samuel Couto20:07:31

It works, thanks 😄

leif20:07:07

If I have an s-expression with metadata that includes line+col information, is there any built in function that can either print out that s-expression putting in line breaks as appropriate? (Alternatively converting the s-expression to a string works.)

alpox21:07:19

Im not sure if there is something taking into account the metadata but for printing human readable s-expressions there is https://clojuredocs.org/clojure.pprint/pprint

leif22:07:28

@U6JS7B99S Does pprint print things out in a way that can be read back in with, say read-string?

alpox22:07:33

I believe that there are cases where it doesn't - for example printing a java class instance. It also cannot read back in record instances (It would read them as plain maps)

alpox22:07:38

As far as EDN-ready values go it should, I believe, be possible

leif22:07:18

Which is about what I would expect from pr and prn.

leif22:07:37

Still sad that it will reflow everything, but I guess it can't be helped. Thanks.

phronmophobic22:07:38

you may be interested in something like https://github.com/clj-commons/rewrite-clj which can read edn and preserve whitespace.

leif20:07:12

Basically, looking for something machine readable, but with human-readable spacing.

ghosttoaster21:07:45

How would you summon the find-versions tool talked about https://clojure.org/reference/deps_and_cli#find-versions? I'm trying clj -X:deps find-versions :lib io.github.clojure/tools.deps.graph and all I get is the error message Function not found: clojure.tools.cli.api/find-versions

dpsutton21:07:03

What version of the clojure cli are you using ?

dpsutton21:07:07

Is that the prerelease version? The new goodies haven’t made it into a stable release yet

ghosttoaster21:07:34

ohhhhh probably not.

ghosttoaster21:07:03

weird that it would already be in the official docs...

seancorfield21:07:54

@cdimara I think Alex was hoping to get to a stable release faster -- but there have been a number of bug fixes on the prerelease side and the Windows scripts also needed to be substantially updated.

3
seancorfield21:07:29

(I suspect he also hoped folks read the news article about the changes -- https://clojure.org/news/2021/07/09/source-libs-builds -- where it mentions the prerelease... but it certainly has caused some confusion for quite a few folks based on the Qs I've seen posted here in various channels and on ClojureVerse)

seancorfield21:07:12

The latest prerelease is 1.10.3.929 I believe (we're on 1.10.3.920 at work). You can see all the release numbers here https://github.com/clojure/brew-install/releases but only stable releases are listed here https://clojure.org/releases/tools (and I don't think anyone would expect folks generally to tie Clojure CLI to brew-install when trying to figure that out, which is why I'm linking to it).

ghosttoaster21:07:34

Oh man I totally read that article. And I assumed it was about features in a current release. Glazed right over the installation part. sorry.

seancorfield21:07:01

Easy to miss: it's only mentioned on one line over halfway through the article I think...

seancorfield21:07:31

I'll make the same offer here that I've made in a few other threads: my DMs are always open for any Qs about the Clojure CLI, tools.deps, tools.build, and stuff like clj-new, depstar, etc 🙂 (which will make sure I see the Q, whereas I might miss it here)

thanks3 6
👍 2
seancorfield21:07:03

A lot of deps.edn tooling out there still assumes -A as the way to invoke things (despite how long there has been a warning to use -M instead) and a lot of people are still using old CLI installs that don't support -X so that also adds to the confusion (in the opposite direction).

seancorfield21:07:11

(and the behavior of -M changed so old CLI versions don't get the warning for using -A but can't actually use -M because it hadn't used to pick up dependencies!)