This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-11-09
Channels
- # announcements (7)
- # beginners (84)
- # calva (3)
- # cljs-dev (19)
- # cljsrn (1)
- # clojure (64)
- # clojure-dev (31)
- # clojure-italy (13)
- # clojure-nl (6)
- # clojure-spec (4)
- # clojure-uk (35)
- # clojurescript (4)
- # clojurex (86)
- # datomic (4)
- # fulcro (11)
- # graalvm (93)
- # joker (5)
- # nrepl (32)
- # off-topic (45)
- # pedestal (4)
- # reagent (3)
- # rewrite-clj (12)
- # sql (17)
- # tools-deps (4)
- # vim (28)
Hi! Trying to compare two dates, clj-time/interval only works when the first date comes before the second. (clj-time/minus date1 date2) throws org.joda.time.DateTime cannot be cast to org.joda.time.ReadablePeriod. Any ideas of could I do ?
clj-time also has equal? after? and before? if you want to be descriptive and avoid the whole -1 0 1 thing
I need to know the difference between the 2 dates
Use compare to sort them first, then use clj-time/minus?
I mean clj-time/interval, perhaps. Sorry, whichever one you said works if the two date/times are given in time order.
Thats my initial idea, with interval instead of minus.
yeah probbably. I was hoping for cleaner option. Thanks ๐
Just a note that Joda Time (and clj-time
) is essentially deprecated since Java 8, because Java Time is built-in. (I'm one of the maintainers of clj-time
BTW and I encourage people not to use it)
Using Java Time to calculate the difference between two date/times https://www.javabrahman.com/quick-tips/java-8-how-to-calculate-difference-between-two-java-time-localdate-instances/
FWIW @UQ88JCRGU I like clojure.java-time
as a wrapper around Java Time (it's one of the things we recommend folks use instead of clj-time
):
seanc@DESKTOP-QU2UJ1N:/mnt/c/Users/seanc/clojure$ clj -Sdeps '{:deps {clojure.java-time {:mvn/version "RELEASE"}}}'
Clojure 1.10.1
user=> (require '[java-time :as jt])
nil
user=> (-> (jt/instant) (jt/minus (jt/days 10)))
#object[java.time.Instant 0x26bb92e2 "2019-10-30T00:55:34.202273Z"]
user=> (def a *1)
#'user/a
user=> (def b (jt/instant))
#'user/b
user=> (def a-b (jt/duration a b))
#'user/a-b
user=> a-b
#object[java.time.Duration 0x577bfadb "PT240H13.605787S"]
user=> (jt/as a-b :days)
10
user=> (jt/as a-b :hours)
240
user=> (jt/as a-b :seconds)
864013
user=>
BTW I'm using VSCode, and it seems that is has no auto formater (auto-ident) for clojure. Does anyone knows any options to format a clojure code ?
zprint but disclaimer I have no idea how good it is. I know it doesn't deal with quoted forms well. I'd love a solid opinionated clojure autoformatter
thanks, I'll will try it out ๐
Calva does auto-formatting. And a lot more. I recommend. (But, yeah I created it, so of course ๐) I can also recommend #calva-dev
Hi everyone. I am looking for some help Java interop. Does anyone know why it might be that an object that is an instantiation of a nested class might have some of its methods missing in Clojure, but not Java?
If the nested class is declared private in Java, I have not discovered how to access it from Clojure.
What steps have you tried that lead you to believe the method is missing?
If I call the method directly, I get
ojalgo-clj.matrix.primitive-matrix=> (.limits mat (int 2) (int 3))
Execution error (IllegalArgumentException) at ojalgo-clj.matrix.primitive-matrix/eval6966 (form-init8309903648537351375.clj:1).
No matching method limits found taking 2 args for class org.ojalgo.matrix.PrimitiveMatrix$LogicalBuilder
But if I wrap it in this Java function
public class PrimitiveMatrixLogicalBuilder {
public static PrimitiveMatrix.LogicalBuilder limits(PrimitiveMatrix.LogicalBuilder m, int rowLimit, int columnLimit) {
return(m.limits(rowLimit, columnLimit));
}
}
Then I get
ojalgo-clj.matrix.primitive-matrix=> (PrimitiveMatrixLogicalBuilder/limits mat (int 2) (int 3))
#object[org.ojalgo.matrix.PrimitiveMatrix$LogicalBuilder 0x6537bfea "org.ojalgo.matrix.PrimitiveMatrix$LogicalBuilder@6537bfea"]
Is the inner class declared private? Or any other Java modifiers?
Its declared static.
Does the method limits
have any modifiers?
Nope.
Some of the methods work and others don't ๐
You could try use the Java reflection API, e.g. getDeclaredMethod, to get a reference to the method you want to call, then call (.setAccessible the-method true)
, then the .invoke
method, to see if that helps.
That can help if they are declared private, and perhaps even if they have no modifiers at all (not sure), but I have had difficulty trying that when the inner class was declared private.
You may want to try the #clojure channel for this question, as there may be a bigger audience of knowledgeable folks following there than in this channel.
That isn't a typical Clojure beginner question ๐
@byrongibby The problem is those are variadic methods.
To call them from Clojure you must pass an array of values in the ...
position.
I assume that's the problem you're having with "some" methods rather than others?
Oh, no, you have a type compatibility issue... sorry.
(hopefully, you'd already figured out the array/variadic thing?)
@seancorfield Yep. ๐ Although I struggled with that as well...
OK. Let me read those JavaDocs for a bit and see if I have any ideas. Sorry, should have read all the way back before jumping in...
@andy.fingerhut Thanks for the advice, I'll try out your suggestions. I think this package is a little weird, I also got this obscure error previously
ojalgo-clj.matrix.primitive-matrix=> (.makeEye PrimitiveMatrix/FACTORY 2 2)
Execution error (IllegalArgumentException) at ojalgo-clj.matrix.primitive-matrix/eval6968 (form-init8309903648537351375.clj:1).
Can't call public method of non-public class: public org.ojalgo.matrix.BasicMatrix org.ojalgo.matrix.MatrixFactory.makeEye(int,int)
@seancorfield No problem, help is always appreciated.
That "Can't call public method of non-public class" message looks vaguely familiar to me ... perhaps the same one I was seeing a few days ago when trying to call a method in a private inner class. In my case I had access to the Java source and could easily create a fork for my own personal use -- it was primarily testing/debugging for my application, so not an issue for me.
Yeah, I created a Java wrapper as call is permitted in Java as was suggested to me elsewhere. I am wondering if this new error is some continuation of the same issue.
as the call*
This is an interesting problem @byrongibby ๐
Haha. I'm glad it's interesting to someone, it's been hell of a frustrating to me. ๐
@byrongibby what jvm version ?
I'm off to bed, it's 4 AM in South Africa. Thanks for the help, I'll check back later today.
@ghadi OpenJDK 64-Bit Server VM 1.8.0_232-b09
The JavaDocs (which are pretty poor) are for 47.2.0 but the current version on Maven is 47.3.1. I'd tried that first and wondered if that was part of it, so I backed off to 47.2.0 and I can repro Byron's puzzling behavior. If you call methods that take no arguments, they work. So I'm wondering if it is something specific in the way Clojure is trying to find matching methods via reflection?
I'm trying it on Java 10.0.2 (for no good reason -- I guess I just haven't updated my WSL install for a while!).
I (set! *warn-on-reflection* true)
and I do not get reflection warnings from (.makeZero PrimitiveMatrix/FACTORY 2 2)
-- which has only one declaration (no overloads) -- but I get a reflection warning (and a failure to resolve) from (.makeEye PrimitiveMatrix/FACTORY 2 2)
-- which has two declarations (one with two int
args, one with a structure object). @ghadi Wondering if we should take this to #clojure-dev as it's feeling like a bug (or a least an interesting shortcoming) in Clojure interop reflection?
I found I use clj-http.client/get
download image file is not complete. Here it is.
Is it possible to re-use s/fdefs if I have several functions with the same signature?
Thanks! How would I do that? The fdef includes the function name, which is used during insturmentatin. That's why I'm stuck
s/fdef is just the same as s/def on an s/fspec
with a symbol as the key
in fact, that is literally exactly what it is:
user=> (source s/fdef)
(defmacro fdef
"...docs"
[fn-sym & specs]
`(clojure.spec.alpha/def ~fn-sym (clojure.spec.alpha/fspec ~@specs)))
so you can do the same thing:
(s/def ::reusable-fn (s/fspec :args ... :ret ...))
(s/def fn-1 ::reusable-fn)
(s/def fn-2 ::reusable-fn)
super, thank you!
Solved, I added (with-open ...)
wrapper fixed this problem.
Is there any reference to understand CLJC extension? I know CLJS and CLJ but I am not sure of the purpose of CLJC.
The c stands for โcommonโ and these files are intended to work on any Clojure platform (Clojure, ClojureScript, ClojureCLR)
They support reader conditional expressions that are platform conditional
So on a tutorial someone was explaining the #?(:cljs conditional expression to use in a CLJC file, but I wonder why is it needed if the produced compiled code will only either run on JVM or JS runtime?
Because not all code is portable
In other words, is the use case to write front-end code and back-end code in the same source file?
Newb here, but does this announcement from MS mean that Clojurescript can target Windows via React Native too? (https://github.com/microsoft/react-native-windows) Does something like this exist for Mac/Linux? I saw a couple of presentations on Youtube where people had authored React-native cross-platform mobile apps but I thought it was only limited to mobile OSs...
What is the Clojure equivalent of this Java code?
BufferedReader br = new BufferedReader(socket.getInputStream());
while ((thisLine = br.readLine()) != null) {
System.out.println(thisLine);
}
Would this be considered the Clojure equivalent?
(with-open [rdr (
(doseq [line (line-seq rdr)]
(println line)))
Look at my solution https://github.com/ArturAralin/clojure-tasks/blob/3cf40d005cab5c1c86e5332ead98be655b8003c2/http-request/main.clj#L25
@scott.archer in my opinion that is pretty much the equivalent, the clojure code is actually doing more, since it is closing the reader afterwards
I am learning shadow-cljs and my project includes both reagent and a library depending on React through npm. I'm afraid that there is a danger that my project will be running two instances of react, but am unsure if this worry is well founded. Does anyone have experience with this? Thanks in advance
don't need to worry. shadow-cljs will only include on react version, the one from npm in fact.
awesome, does that mean I should manually install react, react-dom, etc. through npm?
thank you very much @thheller! this is an awesome tool! I really appreciate it
hello, anybody have a question?