This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-10-17
Channels
- # announcements (2)
- # aws (44)
- # beginners (96)
- # calva (10)
- # cider (7)
- # cljdoc (5)
- # cljsrn (2)
- # clojure (38)
- # clojure-dev (19)
- # clojure-europe (6)
- # clojure-italy (16)
- # clojure-nl (10)
- # clojure-norway (44)
- # clojure-spec (7)
- # clojure-uk (74)
- # clojurescript (133)
- # cloverage (1)
- # cursive (54)
- # datomic (78)
- # duct (11)
- # graalvm (5)
- # instaparse (4)
- # joker (3)
- # kaocha (5)
- # nrepl (2)
- # off-topic (10)
- # pathom (56)
- # pedestal (1)
- # reagent (7)
- # reitit (17)
- # shadow-cljs (144)
- # slack-help (2)
- # sql (35)
- # testing (5)
- # tools-deps (22)
- # vim (22)
- # xtdb (11)
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 http://deps.co/
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?
This is the repo: https://github.com/romanows/WordSequenceAligner
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
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: https://github.com/borkdude/sci/tree/master/src-java/borkdude/sci
this might help if you're using tools.deps: https://github.com/EwenG/badigeon
hi everyone, how could i write in clojure the following nested if statement?
int x = something
int y = something
if ( x * y < z ) {
x++;
if (x * y < z) {
y++
}
}
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++
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
I just did a test with cond
: https://repl.it/repls/SecondhandSparseLesson
yeah i was thinking perhaps something like this:
(let [x #(if (< (* something something) z)
(+ 1 something)
something)
y #(if (< (* x something) z)
(+ 1 something)
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
(if (< (* x y) z)
(let [new-x (inc x)]
(if (< (* new-x y) z)
[new-x (inc y)]
[new-x y]))
[x y])
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?
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.
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".
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.
(enable-console-print!)
I think will do it
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.
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?
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
are you loading (or causing to load via require, use, etc) the file with the implementation?
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)
@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
call load
or load-file
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?
require and use ultimately call load so these are all the same thing eventually
re abc, no it's not necessary - b will ensure a gets loaded if you load b in c
I'm assuming based on prior discussion you care primarily about the load side effects
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 defmethod
s 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?
I may have misunderstood what was in a/b/c before :)
consumers only care about method definition (defmulti) implementers (defmethod) must depend on the defmulti the system should ensure implementations are loaded before consumers invoke
sometimes system == consumer, but it depends
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 clojure.java.sh, use processbuilder or exec on runtime and call destroy on the process object
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.