Fork me on GitHub

Hello, all! I have a couple of queries that I have been pondering on for a while: * Is there an idiomatic way to design error handling for a project? Say, specifically, for a web project? Currently, I am using failjure, and while it is nice for small snippets, I find that I am now tied to using it everywhere, and readability is also taking a hit due to the main logic being hidden within the failjure code itself. What cleaner ways are there to approach error-handling in general in Clojure? * What would be some nice open source projects to read to get a good sense of best practices in Clojure (libraries, backend code, etc). * Any good (and updated) resources on learning ClojureScript? I have the book from Packtpub, and it seems promising enough, but the very first chapter jumps into using a bunch of different (and wildly varying) ways of just setting up a ClojureScript project. That appears to be kind of overwhelming. Also, figwheel seems to have a newer avatar (figwheel-main), but most people still seem to be using the old figwheel. Use figwheel or go with figwheel main?


My two cents here @UDV3M0U10, as far as error handling goes, is that monads aren't usually considered idiomatic in clojure. My recommendation, if you're writing a web service would be to validate incoming data (using spec or schema) and handle external errors (using exceptions) at the edges of your system. This would push both out of your business logic, and give you more confidence that the meat of your code is just on the happy path. If errors are more a part of your domain, I'd go with a monadic approach. For learning clojurescript, is probably the best blog on clojurescript in particular, though it's a bit sparse. Learning cljs is a weird process, since you need to know javascript, including idiosyncrasies of your host (node/browser), as well as clojure. For actually using clojurescript, I've settled on shadow-cljs, which has some rough edges, but is pretty full-featured and is a bit less magical than lein-cljsbuild for example.

💯 1

Also is a good cljs reference.


@jstaab Thank you so much for your response. It is very helpful indeed! 🙂


Also, for ClojureScript, yes, I agree - finding resources that are cohesive seems to be rather difficult! I will check out the resource that you mention. Cheers, and thank you for your help again.

peter hull09:01:33

Is there a straightforward way to get a lazy sequence of lines from a sequence of filenames? I tried line-seq inside with-open but didn't work because I needed to realize the line sequence before with-open closed the reader. I want to do processing on a set of files as if they were all catted together in one big file.


@U7KD4HQ1W Are you doing the processing outside the with-open block?

peter hull10:01:47

Yes. With one file it's easy to do it inside; with multiple files I ideally want to get a line sequence without having to store all the lines/intermediates


problem is the with-open will close it before anything happens. you need to keep all those files open simultaneously if youre using line-seq. how big are your files? i'd suggest reading them all into memory if small enough

peter hull10:01:14

In practice they are not too big, so assembling them in memory works. I was just wondering if there was a neater solution to the problem. (usually there is with clojure!) Thanks for your help.


@U7KD4HQ1W Something ugly i hacked together, does the stuff lazily but isnt that clean. might help:

(defn lazy-cat'
    (if (seq colls)
      (concat (first colls) (lazy-cat' (next colls))))))

(let [readers (->> ["file1"
                   (map ))
      lines   (->> readers
                   (map line-seq)
  (do-your-stuff-here lines)
  (run! #(.close %) readers))


as you can see the closing has to be done manually 😕

peter hull11:01:57

👍 Thanks! Makes sense (I think, will have to look at it a bit longer)


hey, I can't figure out easy way how to clean up all my namespaces. After some refactoring, I can't run tests, coz some functions which where moved to different namespaces are now colliding with where they where, so I would like to have "clean sheet" after refactoring, so unload and reload all the namespaces


Is this running test on the command line, or in a repl, or...?


My solution to a large scale refactor like that would generally be to kill/restart the test runner or exit/restart the repl


It's just not worth the time to try and get everything cleaned up properly vs the time it takes to exit/restart


yes looks like restart repl is the way I go


another question I feel I am missing some idiomatic approach I see pattern emerging in my code... I have top level functions called by REST layer, which all take in two ids, find the objects by those ids and call some function This "some function" is the only distinctive specifics, but getting ids, and translating them from ids to objects (maps) is common for all those kind of function... how to abstract or shorten it coz all of my functions have the same shape (same args, which are transformed)


Hello all, how do I convert this new double[][] { { 30 }, { 20 }, { 50 } } to clojure?

Lennart Buit11:01:57

Do you mean like this: [[30] [20] [50]]


[[30.] [20.] [50.]] to keep desired double type


I´m getting this error clojure.lang.PersistentVector cannot be cast to [[D


could you show the line that throws this error?


(let [dataset (DefaultXYZDataset.)]
    (.addSeries dataset "USA" [[30.] [20.] [50.]])
    (.addSeries dataset "CHINA" [[30.] [20.] [50.]]))


it´s expecting 2d double array


ah, you probably have to use make-array then


I'm guessing (make-array (make-array 30.) ...)

Lennart Buit11:01:38

(to-array-2d [[30.] [20.] [50.]])

👍 1

good to know

Lennart Buit11:01:46

yeah, I am checking whether it actually works tho :’)

Lennart Buit11:01:07

because it appears to be an array of Objects, whereas we want an array of primitives


to-array-2d is not working too [[Ljava.lang.Object; cannot be cast to [[D

Lennart Buit11:01:58

yeah, this is an array of boxed values, whereas you want an array of primitives


I found this on google :v (into-array (map double-array data))


it seems that (into-array (map double-array [[30.] [30.] [30.]])) worked


Is (shutdown-agents) the only way for a clojure app to exit when using futures? Or is there some way to shutdown futures individually?


shutdown-agents is the best way. there is future-cancel but that won’t necessarily do anything useful (depends what the future task is)

👍 1

future-cancel is my favorite Clojure docstring btw: “Cancels the future, if possible.”


apocalyptic …. or, not?


My futures don't need to be canceled. They reach the ready state as expected. Was just wondering if there was a non-global way to clean up the future in a way that would allow the main thread to exit. Thanks for the confirmation.


would rich accept a ticket to add an ominous elipsis to that doc string?


Like others, I’m about to try clojurescript again for the new year; intimidated by the infrastructure advances since I last tried and since my books-on-hand were written. I’ve got a [card] game I’ve written in JS with Vue.js. I’m feeling like shadow-cljs + reagent is considered a good way to go?


That's pretty much where I've landed for my various projects. Definitely still some rough edges with shadow, but it's got all the features I care about, including npm integration and lots of build targets.


nice. If I get it cloned (it currently is one page, in browser, no server) - I might try to add some firebase integration.


Looks like those should be possible.


Do I need figwheel too? Or is that just a lein thing?


I believe figwheel is leiningen-focused


As far as I know, figwheel is browser oriented, and may be more solid than shadow-cljs, but I needed to compile the same code to node, which I don't think figwheel does.


Figwheel can work with node, and it can also work outside of Lein (there is figwheel-main)


@U04VDQDDY how does it compare with shadow-cljs? I never really got started with figwheel except for a small browser app a few years ago because of the above assumptions.


I know shadow-cljs has a hot-reloading mechanism, but I've never used it, so I can't compare


It's pretty good, if a little buggy on the skinny branches (for example, when running tests using a custom main function it loses track of which tests exist). Does figwheel handle stuff like production builds, dependencies, bundle optimization, and integration with npm? Or is it still just the hot-reloading part?


I am trying to understand the connections between namespaces and file names and directory structure. (Any good articles on the topic would be appreciated.) I’ve been looking at examples in the clojure namespaces and have a question about one in particular. The namespace clojure.spec.alpha is contained in the directory/file path “spec.alpha/src/main/clojure/clojure/spec/alpha.clj” and there is also “spec/gen/alpha.clj” and “spec/test/alpha.clj”. My question is what will this structure look like when alpha (or alpha2) is done and the namespace becomes just “clojure.spec”?


it is complicated because what matters to the clojure runtime is classpath roots


and which paths become classpath roots is up to whatever tooling is building your classpath

hiredman23:01:42 is setting up the classpath root for building spec.alpha


(a classpath root)


so src/main/clojure/ becomes a classpath root


and when the clojure goes to load the namespace clojure.spec.alpha it looks for clojure/spec/alpha.clj relative to the classpath roots