This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2024-01-10
Channels
- # announcements (14)
- # beginners (55)
- # calva (4)
- # cider (9)
- # clojure (56)
- # clojure-austin (25)
- # clojure-brasil (1)
- # clojure-dev (29)
- # clojure-europe (44)
- # clojure-mexico (1)
- # clojure-nl (2)
- # clojure-norway (1)
- # clojure-uk (5)
- # clojurescript (15)
- # cursive (9)
- # datomic (5)
- # emacs (30)
- # events (1)
- # graalvm (30)
- # honeysql (17)
- # hyperfiddle (54)
- # introduce-yourself (1)
- # jobs-discuss (6)
- # kaocha (2)
- # leiningen (5)
- # lsp (6)
- # malli (3)
- # missionary (16)
- # off-topic (42)
- # overtone (40)
- # pedestal (2)
- # re-frame (21)
- # shadow-cljs (16)
- # squint (2)
- # tools-deps (14)
I almost never use Java interop besides calling a few functions, so I'm a bit surprised that Clojure's set!
doesn't have an arity that's usable with doto
, like ClojureScript does.
So in ClojureScript, this construct is valid:
(def obj (doto (create-obj)
(set! -field1 1)
(set! -field2 2)))
Would it make sense to extend set!
in Clojure to have that arity?https://cljs.github.io/api/cljs.core/#setBANG interesting... api reference for cljs doesn't mention this arity
Hm, right. It's in the impl though, and it's maintained. Perhaps it should be mentioned in the docs as well.
yeah, it should be) that would safe me many keystrokes 😄
I am using https://github.com/plumatic/schema/blob/master/src/clj/schema/experimental/generators.clj Is it possible to set a limit of how many items it will generate?
(s/optional-key :descriptions) (s/constrained
[{:name (non-blank-str-max-length 120)
(s/optional-key :photo-urls) [(non-blank-str-max-length 250)]}]
#(<= (count %) 99))
This is my schema ^. There is a problem with such-that
when more than 99 descriptions gets generated.
Hi. Can't help with your question, but for future reference, please start topics in a single message. Slack allows using a thread to add extra details or edit the original post where Shift+Enter can add a new line without sending the message.
https://github.com/plumatic/schema-generators/blob/master/src/schema_generators/generators.cljc#L217-L219
sample
function takes argument k
to limit number of returned samples.
It seems that sample
works on the top level, when you yourself call it. But I think OP needs to generate, say, a map where a value for a particular key is a collection with fewer than 100 items.
I want to generate ~300 (or more) samples. Sometimes it creates more that 99 descriptions and throws runtime exception.
At least with spec
, you can attach a custom generator to a spec. Perhaps the same can be done here.
how to write this in clojure?
public class HelloVerticle extends AbstractVerticle {
@Override
public void start() {
Vertx vertx= super.getVertx();
System.out.println(this.toString());
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new HelloVerticle());
}
}
i tried :gen-class with -start method and i got java.lang.ClassFormatError: Duplicate method name "start" with signature "()V" in class file vertxx/HelloVerticlehttps://github.com/vertx-clojure/vertx/blob/master/src/vertx/core.clj is old and marked as alpha, but may be useful to see how they interact with vertx without doing all the weird stuff java examples do in the name of brevity
this core.clj, looks complicated, for now i want to extend AbstractVerticle and override its start() method
start takes an argument, and an exception is being thrown, but on a different thread and is communicated back via the promise like object that deploy returns
% clj -Sdeps '{:deps {io.vertx/vertx-core {:mvn/version "4.5.1"}}}'
Clojure 1.11.1
(def vertx (io.vertx.core.Vertx/vertx))
(.deployVerticle vertx
(proxy [io.vertx.core.AbstractVerticle] []
(start [_]
(println "hello world"))))
#'user/vertx
user=> user=> hello world
#object[io.vertx.core.impl.future.Mapping 0xef1695a "Future{unresolved}"]
user=>
on AbstractVerticle class not the Verticle interface, there is a start method without argument, the java code that i sended is working ok
so overriding start via proxy will override and start methods regardless of arg count
i was using it like start [] without argument, because it has 2 start methods one without and one with a Promise
the implementation of AbstractVerticle is likely the 1 arg start method is calling the 0 arg start method
but with proxy when you provide a start method, that method overrides all the start methods on the class
so Vertx was calling it with 1 arg and it was throwing an error because the start on the proxy can only take 0 args
but that was happening on another thread and being communicated back to the main thread via the promise like thing that deploy returns, which I doubt you were looking at very hard when proxy didn't work
so you need something like (start ([] ...) ([p] (proxy-super this start p))
, but I forget the exact usage details of proxy-super, and something to keep in mind is proxy-super is not threadsafe, all of which is why I suggest (and I bet why the nascent clojure library there) biting the bullet and using the interface instead
% clj -Sdeps '{:deps {io.vertx/vertx-core {:mvn/version "4.5.1"}}}'
Clojure 1.11.1
user=> (def vertx (io.vertx.core.Vertx/vertx))
#'user/vertx
user=>
(deftype V [^:volatile-mutable vertx
^:volatile-mutable context
start
stop]
io.vertx.core.Verticle
(getVertx [_] vertx)
(init [_ v c]
(set! vertx v)
(set! context c))
(start [_ p]
(start vertx context p))
(stop [_ p]
(stop vertx context p)))
user.V
user=>
(.deployVerticle vertx
(->V
nil
nil
(fn [vertx context p]
(println "start")
(.complete p))
(fn [vertx context p]
(println "stop")
(.complete p))))
start
#object[io.vertx.core.impl.future.Mapping 0x6ab4ba9f "Future{unresolved}"]
user=> (.close vertx)
stop
#object[io.vertx.core.impl.future.PromiseImpl 0x30e6a763 "Future{unresolved}"]
user=>
thank you for helping me, this with the 2 start methods, with 1 arg it worked, but still i cant make the java code to work
AbstractVerticle i want to extend, just to follow the java code , while reading the book, deftype i never used it in past
i think i just need to find books that explain those java things from clojure very good, because they are complicated
package ch2;
import io.vertx.core.AbstractVerticle;
import io.vertx.core.Vertx;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class HelloVerticle extends AbstractVerticle {
private final Logger logger = LoggerFactory.getLogger(HelloVerticle.class);
private long counter = 1;
@Override
public void start() {
Vertx vertx= super.getVertx();
System.out.println(this.toString());
vertx.setPeriodic(5000, id -> {("tick");});
vertx.createHttpServer()
.requestHandler(req ->
{
("Request #{} from {}",
counter++,
req.remoteAddress().host());
req.response().end("Hello!");
})
.listen(8080);
("Open ");
}
public static void main(String[] args) {
Vertx vertx = Vertx.vertx();
vertx.deployVerticle(new HelloVerticle());
}
}
its from book, learning vert.x , when reading a book , it helps to just write the same code in clojure, because i dont know vert.x yet, to think of alternative ways
you can see https://github.com/eclipse-vertx/vert.x/blob/master/src/main/java/io/vertx/core/AbstractVerticle.java does close to nothing
I think, in order to use the abstract base class correctly you'll need to use gen-class and aot compile, and then maybe use :exposes to avoid the two start methods having the same name, but not sure
or you can just not use the base class and do all that stuff right now in a live repl