Fork me on GitHub

Hi all, I have found some Java code that I would like to use from Clojure. It does not appear to be in the Maven repo, nor is it clear to me how I could compile it into a jar. I came to Clojure without any prior knowledge about Java, what would be the best way to use this code from Clojure?


It depends. many java projects uses maven. In that case you can just mvn install to build and install into your local maven repo. Then you can use it locally by adding a dependency on appropriate version (e.g. in leiningen's project.clj). If you want to share between devs/machines then you need to install into a shared remote maven repository like Nexus or


Hi Jumar, thanks. This project does not include a pom file, so mvn install complains it cannot build it. Maybe I could add a pom file myself?


Most probably yes; hard to yes without seeing it.


maybe it's using build.xml (ant)? Or perhaps nothing, just build from IDE?


I think it’s supposed to build from Eclipse


I'd just create a new maven project in my IDE and copy-paste that class. It doesn't seem to have any external dependencies


perhaps just for tests (junit)


Ok, don’t have a Java IDE installed right now, but shouldn’t be that hard. Thanks for the pointers!


since it's just one class file and the lib seems unmaintained, I would just copy it over to my project and vendor it


That's what @U56R03VNW is trying to do I guess 😉


ok, fwiw, here's an example Clojure project using lein to compile the Java:


and fwiw, I wrote that Java completely in Emacs, no IDE 😛

😱 4

that must have been painful 🙂 (ok it's really quite simple thing in this case)


yeah, it was just edit, compile, fix.


if I would vendor the Java code, I would maybe rewrite it in Clojure altogether

clj 4

this might help if you're using tools.deps:


hi everyone, how could i write in clojure the following nested if statement?

int x = something
int y = something
if ( x * y < z ) {
   if (x * y < z) {


I just started with clojure 2 days ago but I think this mutation isn't good You might want to do (+ x 1) and (+ y 1) instead of x++ and y++


for if, you can probably use case


I haven't tried it (AND, I'm also a beginner) Just tried to translate this logic as-is... there probably exists a more functional way to solve this


yeah i was thinking perhaps something like this:

(let [x #(if (< (* something something) z)
           (+ 1 something)
      y #(if (< (* x something) z)
           (+ 1 something)


i had an error in code above the initial values obviously are not 0 but anyway i was looking for something to avoid nesting let statements


need to think about this a bit more


nesting if statements are a smell in C anyway 🙂


you have to click Run at the top to get the function in your REPL


(if (< (* x y) z)
  (let [new-x (inc x)]
    (if (< (* new-x y) z)
      [new-x (inc y)]
      [new-x y]))
  [x y])

👏 4

So everyone is clamoring to re-implement that example Java logic in Clojure. Shouldn't we be asking, first and foremost: what on earth is that code trying to do?

👍 4

Is there a real-world problem being solved? If there is, the great failing of that Java code is: I cannot muster enough effort to figure out what it might be, not because I'm too dumb (although I might be), but because that code is ANNOYING.


It gives no indication as to the meaning of anything.


If we knew, in plain english, what we were looking for (and/or WHY we were looking for it), I wonder how we might express that idea more idiomatically in Clojure.


no I think (as is the case with myself) we were both trying to get familiarity with the syntax of Clojure more than anything else


Fair enough. The spirit of my rant I think still does apply here though. The above Java sample is very typical of how one thinks to solve problems in Java: it's all about the "mechanics" of computing, rather than the "meaning".

👍 4

The lambdas/streams they added in Java 8 do make it possible to get closer to meaning and farther from mechanics, which is nice (my day job is still Java unfortunately, so I take what I can get).


So as a Java hostage, with jealous eyes, I would just implore you (and anyone coming to Clojure) to think about the meaning of things, about expressing code that actually illustrates the meaningful solution to real problems. Clojure is really really good at this.


How to enable println function in re-frame


(enable-console-print!) I think will do it

Raziyeh Mohajer11:10:24

Hi everyone How can I take the current system time in nanoseconds and store it in postgresql? (I heard that system.nanoTime is not accurate and it doesn't have fixed reference point)


Just like as->, are cond-> and some-> designed to be used within ->?


Not saying they can't be used by themselves.

Cas Shun13:10:01

I writing a macro where I wish to check for a value, if the value exists output (foo bar ~baz)`, if the value does not then '(foo bar). Is it there a value for baz that would not affect the arity of the function foo?

Cas Shun13:10:17

essentially a value that when unquoted would disappear from the list

Cas Shun13:10:30

otherwise I assume I have to branch and 'write out' both forms fully

Cas Shun13:10:04

I guess I can assemble (foo bar) and splice baz if it exists?


yup. splicing-in nil or an empty seq should preserve the arity.

👎 4
Cas Shun13:10:31

empty seq doesn't work, let me try nil


You could splice it in like this

`(foo bar ~@(when baz (list baz))) 

💯 4
Cas Shun14:10:20

that does work, awesome. Thank you

👍 4

also I'd always use [baz] in place of (list baz) - there's no context where the list version is better


hi guys , I have a defined a multimethod

(defmulti product-list-view (fn [kw lang-id]
                              (identity kw)))
and then implemented it with
(defmethod product-list-view :local [_ lang-id]
  (product/product-list-view lang-id)) 
but when calling it
(product-list-view :local 1) 
I get this error
No method in multimethod 'product-list-view' for dispatch value: :local 


If I run in the REPL , and reload the file, it works fine, but when running from lein run , same issue


is the implementation in another file?


are you loading (or causing to load via require, use, etc) the file with the implementation?

👏 4

thank you! that was the problem


How did you do it??? I've been writing everything in a single file up until now 🙂


defmulti in file a.clj, defmethod in b.clj, call to the defined multimethod in c.clj. something in c.clj’s list of required files should cause both a.clj and b.clj to be loaded (for instance, just requiring both of them works)


the problem most people then run in to is they decide they want the namespace with the defmulti to require all the implementation namespaces (with the defmethods)


which creates a cyclic dependency which is verboten


@U050MP39D must b.clj load a.clj for the defmethod to be recognized as an implementation of the multimethod?


@UPEGYT4G4 well I have a interface.clj where i put the defmulti, then implementation1.clj and implementatio2.clj where I define the defmethod, and then I have a service.cljs which requires the interface.clj and the implementantion1.clj and implementation2.clj


yes. a must be required in order to use the multimethod at all. b must be loaded in order to get to that implementation but it doesn’t necessarily have to be required


requiring is an easy way to ensure load though 🙂


yep, thats what I did , what is the other way to load?


Heh. I didn't even know about load/`load-file`. I meant "load" as a general term for require/use. What I was getting at is this: if b requires a, then surely it's not necessary require a in c, since c requires b and b requires a, right?

Alex Miller (Clojure team)18:10:28

require and use ultimately call load so these are all the same thing eventually

Alex Miller (Clojure team)18:10:37

re abc, no it's not necessary - b will ensure a gets loaded if you load b in c

👍 8

Thanks for confirming 🙂

Alex Miller (Clojure team)18:10:28

I'm assuming based on prior discussion you care primarily about the load side effects

Alex Miller (Clojure team)18:10:46

if you're actually calling functions in a directly from c, then I'd say you should explicitly require it in c


Hmmm... you just made me realize that I was going about it the wrong way: since client code of a multimethod is agnostic about the defmethods of said multimethod, it means that c should be agnostic of b, and only require a (where the multimethod is defined). Therefore it's the larger system's responsibility to make sure that b was loaded before c calls the multimethod. Does that make sense?


Thanks! :levelup: 😛

Alex Miller (Clojure team)18:10:07

I may have misunderstood what was in a/b/c before :)

Alex Miller (Clojure team)18:10:06

consumers only care about method definition (defmulti) implementers (defmethod) must depend on the defmulti the system should ensure implementations are loaded before consumers invoke

Alex Miller (Clojure team)18:10:47

sometimes system == consumer, but it depends


Sure, that make sense 👍


I hugely regret my choice of names 😆

Alex Miller (Clojure team)18:10:24

welcome to software :)

✔️ 4

It is, after all, the second of Dijkstra's Phil Karlton's two "hard problems in computer science" 😝


(mount/defstate sass-server
  :start (future (sh "bash" "-c" "nohup npx sass --watch sass/main.scss:public/css/main.css & echo $! > pid.txt"))
  :stop (sh "bash" "-c" (str "kill " (clojure.string/trim (slurp "pid.txt")))))
how to solve this nicer in clojure, if the future-cancel not works and the npx script need to run in the background? (that is in the user / dev environment mount part / ns)


don't use, use processbuilder or exec on runtime and call destroy on the process object

👍 4

Ok, I check it, thanks!


this may be much to ask but would someone be willing to pair program with me for 1 hour this weekend? im having some issues with ring cors and i could use some mentorship if anyone is feeling charitable 🙂


Yeah, I could probably do that @tjb -- My wife's away in China so some interaction would make a nice change 🙂 I'm West Coast and not a morning person tho'...


that would be awesome! i am CST (Chicago) but honestly whatever time works for you i can bend my schedule to


Just DM me when you're around -- I've no idea what kind of hours I'll be keeping, with only the cats for company. My schedule kind of goes to hell when my wife's away (I WFH 100% so I'm used to having her around my "office" nearly all the time).


no worries Ill get online mid day to see if you are online and i will DM you then! @U04V70XH6


We have Ring-based CORS running in production.