Fork me on GitHub
#beginners
<
2019-11-09
>
Marcio Arakaki00:11:07

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 ?

bfabry00:11:32

joda DateTime implements Comparable

bfabry00:11:46

so just (compare date1 date2) will work

bfabry00:11:53

clj-time also has equal? after? and before? if you want to be descriptive and avoid the whole -1 0 1 thing

Marcio Arakaki00:11:29

I need to know the difference between the 2 dates

andy.fingerhut00:11:08

Use compare to sort them first, then use clj-time/minus?

andy.fingerhut00:11:03

I mean clj-time/interval, perhaps. Sorry, whichever one you said works if the two date/times are given in time order.

Marcio Arakaki00:11:06

Thats my initial idea, with interval instead of minus.

bfabry00:11:24

looks like the best option to me

bfabry00:11:33

probably using t/after?

bfabry00:11:53

as it's more readable than compare

Marcio Arakaki00:11:35

yeah probbably. I was hoping for cleaner option. Thanks ๐Ÿ™‚

seancorfield00:11:33

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)

seancorfield00:11:23

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=>

โค๏ธ 4
๐Ÿ‘ 4
Marcio Arakaki00:11:49

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 ?

bfabry00:11:53

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

Marcio Arakaki01:11:39

thanks, I'll will try it out ๐Ÿ™‚

pez09:11:07

Calva does auto-formatting. And a lot more. I recommend. (But, yeah I created it, so of course ๐Ÿ˜€) I can also recommend #calva-dev

byrongibby00:11:03

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?

andy.fingerhut00:11:05

If the nested class is declared private in Java, I have not discovered how to access it from Clojure.

andy.fingerhut00:11:35

What steps have you tried that lead you to believe the method is missing?

byrongibby00:11:01

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"]  

andy.fingerhut00:11:40

Is the inner class declared private? Or any other Java modifiers?

byrongibby00:11:26

Its declared static.

andy.fingerhut00:11:05

Does the method limits have any modifiers?

byrongibby00:11:06

Some of the methods work and others don't ๐Ÿ˜•

andy.fingerhut00:11:23

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.

andy.fingerhut00:11:05

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.

andy.fingerhut00:11:37

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.

andy.fingerhut00:11:51

That isn't a typical Clojure beginner question ๐Ÿ™‚

seancorfield00:11:22

@byrongibby The problem is those are variadic methods.

seancorfield00:11:39

To call them from Clojure you must pass an array of values in the ... position.

seancorfield01:11:08

I assume that's the problem you're having with "some" methods rather than others?

seancorfield01:11:48

Oh, no, you have a type compatibility issue... sorry.

seancorfield01:11:07

(hopefully, you'd already figured out the array/variadic thing?)

byrongibby01:11:35

@seancorfield Yep. ๐Ÿ™‚ Although I struggled with that as well...

seancorfield01:11:51

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...

byrongibby01:11:16

@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)  

byrongibby01:11:55

@seancorfield No problem, help is always appreciated.

andy.fingerhut01:11:22

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.

byrongibby01:11:04

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.

byrongibby01:11:17

as the call*

seancorfield01:11:06

This is an interesting problem @byrongibby ๐Ÿ™‚

byrongibby01:11:11

Haha. I'm glad it's interesting to someone, it's been hell of a frustrating to me. ๐Ÿ˜›

ghadi01:11:02

@byrongibby what jvm version ?

byrongibby01:11:13

I'm off to bed, it's 4 AM in South Africa. Thanks for the help, I'll check back later today.

4
byrongibby01:11:06

@ghadi OpenJDK 64-Bit Server VM 1.8.0_232-b09

seancorfield01:11:00

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?

seancorfield01:11:47

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!).

seancorfield02:11:06

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?

stardiviner11:11:41

I found I use clj-http.client/get download image file is not complete. Here it is.

coder4633411:11:55

Is it possible to re-use s/fdefs if I have several functions with the same signature?

coder4633417:11:18

Thanks! How would I do that? The fdef includes the function name, which is used during insturmentatin. That's why I'm stuck

Alex Miller (Clojure team)20:11:46

s/fdef is just the same as s/def on an s/fspec

Alex Miller (Clojure team)20:11:55

with a symbol as the key

Alex Miller (Clojure team)20:11:00

in fact, that is literally exactly what it is:

Alex Miller (Clojure team)20:11:24

user=> (source s/fdef)
(defmacro fdef
  "...docs"
  [fn-sym & specs]
  `(clojure.spec.alpha/def ~fn-sym (clojure.spec.alpha/fspec ~@specs)))

Alex Miller (Clojure team)20:11:36

so you can do the same thing:

Alex Miller (Clojure team)20:11:28

(s/def ::reusable-fn (s/fspec :args ... :ret ...))
(s/def fn-1 ::reusable-fn)
(s/def fn-2 ::reusable-fn)

coder4633421:11:36

super, thank you!

stardiviner11:11:21

Solved, I added (with-open ...) wrapper fixed this problem.

Arto Kalishian13:11:01

Is there any reference to understand CLJC extension? I know CLJS and CLJ but I am not sure of the purpose of CLJC.

Alex Miller (Clojure team)13:11:07

The c stands for โ€œcommonโ€ and these files are intended to work on any Clojure platform (Clojure, ClojureScript, ClojureCLR)

Alex Miller (Clojure team)13:11:08

They support reader conditional expressions that are platform conditional

๐Ÿ‘ 4
Arto Kalishian13:11:15

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?

Alex Miller (Clojure team)13:11:27

Because not all code is portable

Arto Kalishian13:11:07

In other words, is the use case to write front-end code and back-end code in the same source file?

sysarcher15:11:35

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...

ScArcher15:11:35

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 ( (.getInputStream socket))] (doseq [line (line-seq rdr)] (println line)))

Chris16:11:50

@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

๐Ÿ‘ 4
eval-on-point18:11:44

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

thheller18:11:41

don't need to worry. shadow-cljs will only include on react version, the one from npm in fact.

eval-on-point18:11:48

awesome, does that mean I should manually install react, react-dom, etc. through npm?

eval-on-point18:11:49

thank you very much @thheller! this is an awesome tool! I really appreciate it

๐Ÿ‘ 4
sova-soars-the-sora23:11:47

hello, anybody have a question?