clojure-java-interop

How to print EDN output in JSON format using Cheshire custom encoder

女生的网名这么多〃 提交于 2021-01-29 10:22:45
问题 I am newbie with Clojure and I am trying to print EDN output to valid JSON format using Cheshire custom encoder for classes defined in java. My EDN file: {:xyz #XyzBuilder "testString"} Clojure code: (defn getXyz [str] (.getXyz (XyzBuilder.) str) ) (defn custom-readers [] {'xyz/builder getXyz} ) (add-encoder com.java.sample.Xyz (fn [c jsonGenerator] (.writeString jsonGenerator (str c)))) (edn/read-string {:readers (custom-readers)} (slurp filename) ) This generates below output: {"xyz":"Xyz

Using imported Java libs in Clojure REPL

安稳与你 提交于 2020-03-22 09:04:39
问题 Twilio docs for Java lib MVN for this lib I'm trying to use Twilio from Clojure. Pretty new to Clojure dev, so I'm trying to get to grips with importing libs, in general. My project.clj looks like this: (defproject magical-new-project "0.1.0-SNAPSHOT" :description "FIXME: write description" :url "http://example.com/FIXME" :license {:name "EPL-2.0 OR GPL-2.0-or-later WITH Classpath-exception-2.0" :url "https://www.eclipse.org/legal/epl-2.0/"} :dependencies [[org.clojure/clojure "1.10.0"] [com

clojure access enum defined inside a java class

丶灬走出姿态 提交于 2020-01-30 10:57:06
问题 I am trying to use the argon-jvm library for hashing in my application. By default this library uses Argon2i , However, I would like to use Argon2id . To do so, I need to pass the enum value Argon2Factory.Argon2Types.Argon2id to the overloaded create method in the Argon2Factory class. Source code for Argon2Factory.java here. From the lein repl (affter adding [de.mkammerer/argon2-jvm "2.4"] as a dependency), I can do the following: user=> (import 'de.mkammerer.argon2.Argon2Factory) de

How do I implement this generic Java interface with a Clojure record?

白昼怎懂夜的黑 提交于 2020-01-25 16:40:06
问题 I'm trying to implement org.joda.time.ReadableInstant. It inherits from a generic interface, but apparently that shouldn't matter. The interface is: public interface ReadableInstant extends Comparable<ReadableInstant> { long getMillis(); Chronology getChronology(); DateTimeZone getZone(); int get(DateTimeFieldType type); boolean isSupported(DateTimeFieldType field); Instant toInstant(); boolean isEqual(ReadableInstant instant); boolean isAfter(ReadableInstant instant); boolean isBefore

Sandwiching Clojure between Java with Leiningen

二次信任 提交于 2020-01-02 04:12:08
问题 For a class, I need to write some JVM code, and I'm hoping to use Clojure. I got it to work with the bottom part of the software stack, but I can't manage to make it work in between the GUI layer sitting on top and the bottom part. My main problem is getting the Java GUI to recognize my Clojure files. I want to use Leiningen for this, but the Java compiling solution doesn't seem to account for this interoperation. The answer here seems to be exactly what I need. I don't understand where to

How does Clojure's laziness interact with calls to Java/impure code?

拥有回忆 提交于 2019-12-23 07:52:35
问题 We stumbled upon an issue in our code today, and couldn't answer this Clojure question: Does Clojure evaluate impure code (or calls to Java code) strictly or lazily? It seems that side-effects + lazy sequences can lead to strange behavior. Here's what we know that led to the question: Clojure has lazy sequences: user=> (take 5 (range)) ; (range) returns an infinite list (0 1 2 3 4) And Clojure has side-effects and impure functions: user=> (def value (println 5)) 5 ; 5 is printed out to screen

shutdown hook doesn't fire when running with “lein run”

。_饼干妹妹 提交于 2019-12-20 17:34:30
问题 I have the following code: (ns test-hook.core) (defn -main [] (.addShutdownHook (Runtime/getRuntime) (Thread. #(println "shutdown"))) (println "start") (doseq [i (range 1 6)] (Thread/sleep 1000) (println i))) and the following project.clj (defproject test-hook "1.0.0-SNAPSHOT" :aot :all :main test-hook.core :description "FIXME: write description" :dependencies [[org.clojure/clojure "1.2.0"]]) when I run it with "lein run" the shutdown hook only gets executed on normal program execution, not

How to handle java variable length arguments in clojure?

佐手、 提交于 2019-12-17 03:19:11
问题 I'am wrapping a java lib into clojure, but i have problems dealing with variable length arguments. Say, TestClass.aStaticFunction(Integer... intList){/*....*/} How could i call this function in clojure? 回答1: Since Java varargs are actually arrays, you can call vararg functions in Clojure by passing an array. You could convert a Clojure seq (maybe by using Clojure's variety of variable argument functions) into an array: (TestClass/aStaticFunction (into-array Integer [(int 1),(int 2)])) or

clojure gen-class returning own class

大城市里の小女人 提交于 2019-12-13 17:11:17
问题 I'm now making a class object with Clojure which has a method returning the object itself. Written with Java, the object that I'd like to make is like, class Point { public double x; public double y; public Point(double x, double y) { this.x = x; this.y = y; } public Point copy() { return new Point(this.x, this.y); } } The current clojure code that I wrote is like, (ns myclass.Point :gen-class :prefix "point-" :init init :state state :constructors {[double double] []} :methods [[copy []

To write a downloader, Clojure.java.io or Java's io api?

自古美人都是妖i 提交于 2019-12-11 19:26:36
问题 I am trying to write a general http/ftp file downloader in Clojure. I did a little research and found that I can either use java's api -- BufferedReader BufferedInputStream etc, or Clojure.java.io's api -- writer, reader, input-stream, output-stream. I found Clojure's api somewhat easier to use and read than java's api, but how about in terms of performance, speed, etc, will java's api be a better choice then? Is there any other reason to choose one instead of the other? As a jvm platform