Fork me on GitHub
#beginners
<
2018-04-03
>
Denis G09:04:19

Does anybody know how to handle errors/exceptions in Clojure? especially if I have a function composition like

(->> (f x)
        (g)
        (h))
and every function can throw an error/exception, but I only want to call the next function if no error is thrown. Any ideas?

orestis09:04:22

With exceptions, the next function will not be called by default.

Denis G09:04:33

But is it a clojury way of doing things?

orestis09:04:44

If g throws, execution stops there... and resumes at the closest try/catch block. Or you get a stacktrace in your console.

orestis09:04:21

I’m not the best person to answer that, but since Clojure doesn’t have any other error handling constructs, I’d say yes.

Denis G09:04:47

I’ve seen some other libs like https://github.com/scgilardi/slingshot or https://github.com/MichaelDrogalis/dire but don’t know if it’s a good idea

orestis09:04:49

Can’t help with that much... but usually you need something better than exceptions in cases where you want to clean up or wrap the error in some other way to pass to your caller. In the simple case that you posted, exceptions usually suffice.

jumar10:04:40

@denisgrebennicov There are some monadic libraries in clojure like https://github.com/funcool/cats but the usual answer that I got is that exceptions are the fact of life on the JVM so we need to live them => just use try-catch(-finally) at the appropriate level. you can also look at with-exception-default in tupelo lib: https://github.com/cloojure/tupelo#default-value-in-case-of-exception

OctarineSorcerer10:04:13

Anyone know what on earth the IllegalArgumentException Don't know how to create ISeq from: clojure.core$some_QMARK_ exception means when doing something like (filter ['sym] some?)

Denis G10:04:04

@daniel.gomme but shouldn’t the predicate be written after the filter not after the coll

Denis G10:04:13

(filter pred coll)

OctarineSorcerer10:04:22

oh damn is it really that

OctarineSorcerer10:04:47

I should wake up properly

sundarj10:04:57

@daniel.gomme ISeq is the interface for a sequence; so the error message is saying 'can't create a sequence from blah'

OctarineSorcerer10:04:54

Yeah, I see how that'd arise from them being the wrong way around, cheers 🙂

4
tdantas10:04:53

the “monad way” to handle exceptions, but once you start using all your functions must respect and use the failjure functions

Denis G10:04:29

Thank you guys, and BTW does anybody know some good how-to-start-guide for emacs/spacemacs? Just trying to move from IDE to spacemacs and it’s hard 😄

jumar10:04:30

But yes, you have to invest non-trivial time in the transition. Btw. there's also #spacemacs

Denis G10:04:45

Yeap 😄 I’ve installed spacemacs and try to work in it. But … it’s hard 😄 All those key-bindings… Are you using Spacemacs yourself?

jumar10:04:06

The great thing about spacemacs is that it makes learning easier - when you press SPC you will get a mini-buffer with categories of commands which you can explore further.

👍 4
jumar10:04:34

Moreover, if you use GUI version, then Menubar is quite useful for finding useful commands.

frenata13:04:05

Agreed @U06BE1L6T, I found spacemacs to be a relatively non-intrusive transition that allowed me to learn new stuff at my own pace. But I was coming to it from vim land, so I was already comfortable in evil mode.

frenata13:04:46

https://practicalli.github.io/spacemacs/ is a nice resource that's specifically about spacemacs with clojure. @denisgrebennicov

Denis G13:04:24

Perfect! Thanks 🙂

justinlee14:04:39

@denisgrebennicov no shame in using an ide. I used emails for ten years and now I use cursive and it’s great

Drew Verlee14:04:57

Anyone able to spot any problems with this implementation of depth first search?

(defn dfs
  [g n v]
  (cons n ;; add the node to the search path
        (mapcat #(dfs g % (conj v n)) ;; and do the same
                (remove v (n g))))) ;; for unvisited neighboring nodes

(dfs {:a #{:b}
      :b #{:c}
      :c #{}}
     :a #{:a})

;; => (:a :b :c)

petr.mensik15:04:13

What is tbe best way to read a static file from JAR file? Because (io/file (io/resource "templates/file.pdf)) returns nil when called from uberjar 😕 Note that is really there (tested with unzip) and it works in REPL

seancorfield15:04:10

@petr.mensik You can just use (io/resource "templates/file.pdf") I believe. Pretty sure that's how we do it.

seancorfield15:04:30

It returns a java.net.URL which you can slurp I believe, or you can call io/reader on it to get a BufferedReader.

seancorfield15:04:06

(well, you wouldn't want to slurp a PDF I suspect 🙂 )

petr.mensik15:04:54

Thanks guys, that really works, I thought that I can call io/file on java.net.URL since java.io.File accepts it in constructor 🙂

petr.mensik15:04:25

Uh, it's URI, not URL, that's why I guess 🙂

deg16:04:16

Is there a standard equivalent of zipmap that creates a sequence rather than a map? Something like

(defn zipseq [& seqs]
  (partition (count seqs)
             (apply interleave seqs)))
Or, is that the briefest way to say it?

seancorfield17:04:51

@deg That looks like (map vector seq1 seq2 seq3)

👍 4
Alex Miller (Clojure team)17:04:08

(apply map vector seqs)

👍 4
seancorfield17:04:34

(I meant to use map vector directly instead of zipseq -- but, yeah, (defn zipseq [& seqs] (apply map vector seqs)) if you want zipseq)

Will19:04:26

Is there a good eureka client for clojure? I found this one but it hasn’t been updated for 3 years. Is there one that’s more current and compatible with the latest version of clojure?

andy.fingerhut19:04:15

@Will That project lists Clojure 1.6.0 in its project.clj file dependencies. That should be quite compatible with latest version of Clojure, too. I have no idea whether the code works to do what you want, but if it does not, I would be surprised if it was because of Clojure version issues.

Will19:04:59

I should clarify my question. At work we have a bunch of java microservices using Spring Boot to connect to a eureka discovery server I’m wondering if it would be possible to add a clojure microservice that could function the same the java microservices do and register itself with the discover server

andy.fingerhut19:04:30

Successive versions of Clojure since 1.3.0 have been mostly "add some new features, fix some bugs", with little or no "break things that worked with the previous version of Clojure"

Will20:04:57

I assumed it was a problem with it using an older version of clojure because of these errors I am getting. My import looks like this [eureka-client.core :as eureka] When running the server I’m getting this error

Exception in thread "main" clojure.lang.ExceptionInfo: Call to clojure.core/import did not conform to spec:
In: [0] val: clj_http.headers.clj-http.headers/HeaderMap fails spec: :clojure.core.specs.alpha/quotable-import-list at: [:args :class :spec] predicate: simple-symbol?
In: [0] val: clj_http.headers.clj-http.headers/HeaderMap fails spec: :clojure.core.specs.alpha/quotable-import-list at: [:args :class :quoted-spec] predicate: (cat :quote #{(quote quote)} :spec simple-symbol?)
In: [0] val: clj_http.headers.clj-http.headers/HeaderMap fails spec: :clojure.core.specs.alpha/package-list at: [:args :package-list :spec] predicate: (cat :package simple-symbol? :classes (* simple-symbol?))
In: [0] val: clj_http.headers.clj-http.headers/HeaderMap fails spec: :clojure.core.specs.alpha/quotable-import-list at: [:args :package-list :quoted-spec] predicate: (cat :quote #{(quote quote)} :spec :clojure.core.specs.alpha/package-list)

Will20:04:46

I am successfully import other libraries this same way, so I’m not sure quite what is wrong?

andy.fingerhut20:04:47

So this looks like it falls under the "little" in the "little or no" I mentioned earlier. You caught me 🙂

andy.fingerhut20:04:43

Clojure 1.9.0 tightened up the syntax permitted in :require and :import clauses of ns forms. Only a few projects contained forms that do not conform to the new restrictions. Looks like this is one of those few.

andy.fingerhut20:04:51

Or perhaps it depends on another project that is one of those few.

dpsutton20:04:20

(ns eureka-client.core
  (:require [clj-http.client :as http]
            [clojure.set :refer [rename-keys]]
            [clojure.core.cache :as cache]))

andy.fingerhut20:04:54

The error might be in the version of clj-http that eureka depends upon, perhaps.

Will20:04:58

I did find this one that is supposed to work with clojure v1.8 but i am getting the same error, so would it be a problem with the other libraries I’m importing?

andy.fingerhut20:04:02

If you want to quickly try out eureka without these errors, you could try using Clojure 1.8 instead of Clojure 1.9. The main differences between those versions of Clojure are the addition of Clojure spec. If you are not intending to use Clojure spec right away, Clojure 1.8 will not give those errors and let you proceed to experiment with eureka.

andy.fingerhut20:04:51

I would expect that updating eureka and/or its dependencies to work with Clojure 1.9 is probably not a big amount of work, but is probably a distraction from your current goals.

Will20:04:01

I am just learning clojure and trying to get some of the developers at work to try it.

Will20:04:26

Their first question was if it would integrate with our current java services and the discovery server we are using

Will20:04:49

I was hoping to find an easy solution. If i was to debug and find the problem where would I start?

andy.fingerhut20:04:00

Makes sense. There are others who could answer that question backed by much more experience than I can.

Alex Miller (Clojure team)20:04:57

if you lein deps :tree that will show you all your deps and you might be able to spot the offender

Alex Miller (Clojure team)20:04:53

I don’t actually see the error you mention above in that list so maybe it’s newly found

Will20:04:03

Am I just looking for duplicate dependencies?

andy.fingerhut20:04:58

I noticed that the eureka project you mentioned depends on version 1.0.0 of the clj-http library, which is several years old, and eventually depend upon clj-tuple 0.1.5, which is an older version than one on the list of projects that gives an error with Clojure 1.9.0.

andy.fingerhut20:04:43

I do not know whether the clj-http library API has changed in a way since version 1.0.0 that the existing eureka project code would work with a more recent version of clj-http or not.