Fork me on GitHub
#beginners
<
2020-03-21
>
theequalizer7302:03:52

Hello, how can I solve this?

> (def url "")

> (def file (net.cgrand.enlive-html/html-resource (.URL. url))) 

Syntax error (SunCertPathBuilderException) compiling at (form-init5400902524976976728.clj:499:35).
unable to find valid certification path to requested target

potetm03:03:42

If I recall correctly, lein ships with a helpful routine to download the HTML for a URL and print it in the REPL.

seancorfield04:03:07

@potetm Hmm, not that I'm aware of... Happy to be proved wrong...

potetm04:03:32

oh wait, it doesn’t matter for his problem anyways

potetm04:03:51

he’s calling the request explicitly, just doesn’t have the certs he needs

potetm04:03:06

but yeah, it was def there at one point. I’d have to track it down.

potetm04:03:10

iirc @noisesmith might have been the one to point it out to me

dpsutton04:03:39

Cider has an (anti) feature that it will slurp uris automatically. Not sure nrepl in general does though

potetm04:03:24

that’s it, mb

seancorfield04:03:40

That kinda makes sense. I mean, CIDER has every feature, right? 🙂

seancorfield04:03:36

Still doesn't solve the OP's problem. I can only assume the cert on that .ni site isn't in the default chain of trust for the JVM?

seancorfield04:03:07

(I tried that code in a bare clj REPL and got the same exception)

theequalizer7305:03:27

Is there a {:insecure? true} option for html-resource like in clj-http?

dpsutton05:03:35

it looks like enlive just calls (.getContent) on a URL so no way to get that kind of info there.

jumar05:03:41

@orlandomr27 The certificate of that site seems to be invalid according to openssl too:

openssl s_client -showcerts -connect 
...
    Verify return code: 21 (unable to verify the first certificate)
I'd say it's best to import the certificate into java keystore manually (also safer then using some kind of "insecure" option forever)

👍 4
dpsutton05:03:31

(let [body (:body (http/get url {:insecure? true}))
      input (.ByteArrayInputStream. (.getBytes body))]
  (soup/parser input))

dpsutton05:03:05

where soup/parser is net.cgrand.tagsoup

dpsutton05:03:44

the html-resource ns has a bunch of methods for retrieving an inputstream. but if you want to get it by other methods this is where you end up anyways

👍 4
Gulli08:03:05

Anyone here use Aero? I have a property like this :port #or [#env MONGO_PORT 27018], But when I run in my terminal: MONGO_PORT=27777 And then run in the same terminal: lein with-profile test-profile test The test that checks if the port is 27018 passes. Anyone know why the environment var is not being picked up?

Tzafrir Ben Ami08:03:07

Are you using io/resource to read config file?

Chris O’Donnell12:03:54

If you want an environment variable to be seen by subprocesses you need to set it with export or prepend it to your command. See https://unix.stackexchange.com/questions/130985/if-processes-inherit-the-parents-environment-why-do-we-need-export for more info.

💯 4
Gulli12:03:20

@U0DUNNKT2 Beautiful, works as intended now

👍 4
Luis C. Arbildo17:03:39

There a way to write this, without [x times], I mean I don't need the variable x

Chase17:03:02

Is there an idiomatic way to remove elements of a vector using destructuring? Say I have a list of vectors that have 6 elements but I want the data with the 4th and 5th elements removed, how would you approach that?

Chase17:03:20

Well, not necessarily using destructuring, I just approached it that way at first because it would have been very nice and easy. haha. Something like (map (fn [[a b c _ _ f]] [a b c f]) coll)

didibus17:03:55

(let [v [1 2 3 4 5 6]]
  (into (subvec v 0 3) (subvec v 5)))

hindol18:03:11

If the indices are arbitrary, you can also use keep-indexed.

Chase18:03:08

awesome! Thanks folks

hindol18:03:53

Something like,

(keep-indexed #(when (#{0 2 4} %1) %2) [1 2 3 4 5])
;; => (1 3 5)
But your solution using destructuring is elegant. If you really need a vector, pass through (into [] ...).

didibus18:03:51

That said, vectors are not designed for this use case. They aren't efficient for it. Subvec with into is the fastest way

didibus18:03:04

But if you plan on removing a lot of elements, consider using a map instead

didibus18:03:53

Also, vectors can't really have their elements removed. You need to construct a new vector which contains all the other elements.

didibus18:03:24

subvec is fastest because it won't actually copy it all, it'll share the same vector as long as it can under the hood

👍 4
didibus18:03:51

Whereas a solution where you iterate over the whole vector to do so won't

hindol18:03:00

Nice! One more magic.

hindol18:03:34

I did not know that about subvec.

didibus18:03:24

------------------------- cljs.core/subvec ([v start] [v start end]) Returns a persistent vector of the items in vector from start (inclusive) to end (exclusive). If end is not supplied, defaults to (count vector). This operation is O(1) and very fast, as the resulting vector shares structure with the original and no trimming is done.

hindol18:03:35

But when you concat two or more subvecs, it does get copied, right? Or are there more magic?

andy.fingerhut18:03:59

You can iterate through elements of a vector v2, and conj each of them successively onto a vector v1, unit eventually all of them are, to concatenate, but that takes linear time (times some logarithmic factor) in the size of v2 to concatenate, and is essentially what (into v1 v2) does (except it uses transients for better efficiency in the constant factors involved, although it stiill takes linear time in the size of v2)

andy.fingerhut18:03:38

The library core.rrb-vector can concatenate two vectors in logarithmic time in the size of the larger one, using a different kind of tree data structure internally than Clojure's built-in vectors use.

andy.fingerhut18:03:01

It is still a little bit buggy, though, so I wouldn't 100% recommend it unless those remaining bugs are fixed.

didibus19:03:01

Ya, I should have mentioned, while subvec is O(1), the following into is what will slow the operation down, and why using a map or like @U0CMVHBL2 mentioned a more specialized structure built for this split/join use case like rrb-vector would be faster. You can also go down to Java and just use Linked list, but now you are entering mutation land.

didibus19:03:45

Anyways, all these are probably some pre-optimization concerns that don't matter unless you have hot loops or are implementing algorithms.

hindol17:03:34

<@UUGUA0MGQ> If you don't need x, just name it underscore_,_ like `[_ times]`.

👍 8
Luis C. Arbildo17:03:17

yep but is the same, I asked for way for just use times instead [something times]

hindol17:03:14

I don't know of a way, except, (run! println (repeat times "Hello World")) but this will create a sequence for no reason at all and is more wasteful.

Luis C. Arbildo18:03:25

@UJRDALZA5 I guess that the first method is the most common, well I just had curiosity if there a way to do it, thank you

didibus19:03:27

There isn't

didibus19:03:57

The thing is, any given implementation of this will keep track of a counter

didibus19:03:17

since you need to know when to break out of the loop

didibus19:03:30

So why not expose the counter?

didibus19:03:38

In theory dotimes could be made to accept a binding vec that is only a number, but seems a bit silly, saves you one character to type

didibus19:03:02

(defmacro dotimes
 [bindings & body]
 (if (= 1 (count bindings))
  `(clojure.core/dotimes [_# ~(first bindings)]
    ~@body)
  `(clojure.core/dotimes ~bindings ~@body)))      

👍 4
zonkhead19:03:06

When I run lein repl I get an exception saying it can’t find a class of mine. The class it’s complaining about is one where I use :gen-class to subclass InputStream. If I compile the class explicitly in the repl, it works but I can’t figure out why it’s missing to start with.

lispyclouds19:03:09

this tells lein to pre-compile the namespace

zonkhead19:03:29

Very nice. That’s what I was looking for. Thanks!

lispyclouds19:03:40

The caveat being, you need to run compile again or restart the REPL if the file changes

👍 4
lispyclouds19:03:19

you can try something like https://github.com/yogthos/lein-watch if this becomes a hassle