This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-04-23
Channels
- # announcements (3)
- # babashka (68)
- # beginners (297)
- # calva (13)
- # cider (4)
- # clj-kondo (8)
- # cljs-dev (10)
- # cljsrn (26)
- # clojure (100)
- # clojure-europe (4)
- # clojure-germany (1)
- # clojure-italy (9)
- # clojure-nl (5)
- # clojure-spec (9)
- # clojure-uk (41)
- # clojurescript (69)
- # conjure (70)
- # cursive (44)
- # data-science (20)
- # datascript (2)
- # datomic (55)
- # emacs (1)
- # exercism (3)
- # graalvm (2)
- # kaocha (11)
- # leiningen (6)
- # meander (9)
- # mental-health (1)
- # off-topic (73)
- # pathom (6)
- # pedestal (1)
- # re-frame (3)
- # reagent (52)
- # reitit (8)
- # rum (39)
- # shadow-cljs (152)
- # spacemacs (10)
- # tools-deps (28)
- # xtdb (5)
Ah, yes. Thank you!
Hi, is there any way to keep metadata
after using clojure.walk/post-walk
?
I've tried the code below, but it returns nil
(meta (walk/postwalk
(fn [v]
(if (meta v)
(with-meta v
(meta v))
v))
(with-meta '(1 2 3)
{:ar 2})))
Well, it appears that a small change in post-walk
gives me what I want
(defn postwalk
[f form]
(let [form' (clojure.walk/walk (partial postwalk f) f form)]
(if (meta form)
(with-meta form' (meta form))
form')))
Thank you o/ @U11BV7MTK
Gracias, @U2J4FRT2T o/ It's a very nice library
I'm trying to build a native image to take a screenshot in clojure. The code is not that hard, but compiling to native images seems to fail. Or rather, I can build an image, but when running it, I get this exception:
Exception in thread "main" java.lang.IllegalArgumentException: AWT is currently not supported on Substrate VM
at java.awt.Toolkit.getDefaultToolkit(Toolkit.java:37)
at hosa$_main.invokeStatic(hosa.clj:14)
at hosa$_main.doInvoke(hosa.clj:8)
at clojure.lang.RestFn.invoke(RestFn.java:397)
at clojure.lang.AFn.applyToHelper(AFn.java:152)
at clojure.lang.RestFn.applyTo(RestFn.java:132)
at hosa.main(Unknown Source)
This is my deps.edn
:
{:paths ["src/main/clojure"]
:aliases {:native-image
{:main-opts ["-m clj.native-image hosa"
"--initialize-at-build-time"
"--initialize-at-run-time=java.awt.*"
;; optional native image name override
"-H:Name=hosa"]
:jvm-opts ["-Dclojure.compiler.direct-linking=true"]
:extra-deps
{clj.native-image
{:git/url ""
:sha "7708e7fd4572459c81f6a6b8e44c96f41cdd92d4"}}}}
}
in my code, I added type hints (without them, I couldn't build the native image):
(ns hosa
(:gen-class)
(:import [java.awt GraphicsDevice GraphicsEnvironment Rectangle Robot Toolkit]
.File
java.awt.image.BufferedImage
javax.imageio.ImageIO))
(defn -main [& _]
(let [^Rectangle screen-rectangle (Rectangle. (.getScreenSize
^Toolkit (Toolkit/getDefaultToolkit)))
^BufferedImage capture (.createScreenCapture ^Robot (Robot.) screen-rectangle)]
(ImageIO/write ^BufferedImage capture "bmp" ^File (File. "printscreen.bmp"))
))
So, the question actually would be: how to add AWT
to the substrate VM? (I get the same result with the --no-server
option)
@kurt.sys the exception starts with AWT is currently not supported on Substrate VM
so I would say you just can't
(I'm trying JavaFX, but that also doesn't seem to be supported - well, probably not a clj question anymore)
I have found this: https://github.com/gluonhq/substrate Seems to support JavaFX on Graal.
Yeah... I'm trying to make it work. I seem to be able to add javafx, but for some reason, to get a saveable image from a javafx image, one still needs java.awt. So far, no native image to take screenshots, I suppose.
Right. That might almost be ok, except:
1. having a javafx WriteableImage
can apparently only be written when it's converted to a java.awt.BufferedImage
, and the latter is depending on java.awt
, which is not supported for native images
2. the main target would be win-images, which is not available.
Hi How can I, with clojure.data.zip.xml/xml1->
navigate to text of first children inside tag? Imagine case where I have something like:
<names>
<name>
otwieracz
<comment>
...
</comment>
</name>
...
</names>
I'd like to access otwieracz
, so something more or less reassembling: (xml1-> xml :names :name first text)
I don't think your first
means what you think it means. I think one way to do it is to replace it with clojure.zip/down
can I create a multidimensional array of doubles with double-array
?
There's only the two following signatures:
[size-or-seq]
[size init-val-or-seq]
But if I try using make-array
, I cannot use double
as the type.
I need this to interop with a library that is expecting a double[][]
as one of its parametersI ended up with this:
(defn make-2d-double-array [dim1 dim2]
(into-array (repeatedly dim1 #(double-array (repeat dim2 0)))))
I'd still prefer a solution not involving the creation of a lazy seq just for the sake of having something to feed into-array
, but this is enough for my use-case
Should next.jdbc be considered the successor to clojure.java.jdbc or will both be maintained indefinitely? Say if I were adding one of these to babashka, is next.jdbc preferred?
next.jdbc is the active code base
(read-string "#:db{:ident :class-desc/10-major-varies-by-season}")
the above throws an error.
Why is this valid :a/b-10
and this invalid :a/10-b
?
It’s a long story, but the symbols can’t start with numbers
Keywords are “like symbols” in this respect
This is really the Clojure reader, not edn
So https://clojure.org/reference/reader is more relevant
But there are some long standing now grandfathered in bugs that does allow it in keywords
But you’re seeing one of the cases where it will be marked invalid
Yes, code derived from there
you should use it for true compile-time constants that should be compiled into code that uses it
this just came up on reddit - https://www.reddit.com/r/Clojure/comments/g48c53/why_is_there_no_constants_in_clojure_like_final/fnwfvh1/
Hello, getting the below error..
Applying task run to [-m tap-mssql.core --config /builds/config/config.json --discover] Applying task javac to nil Running javac with [@/tmp/.leiningen-cmdline13379584158302418975.tmp] Applying task compile to nil All namespaces already AOT compiled.
Any help would be highly appreciated
does clojure have a go-to markdown parsing library? I'm looking to parse markdown into hiccup if I can but HTML if not.
there's https://github.com/yogthos/markdown-clj . some rough edges like it parses a line at a time so the markdown must be a bit tidier than the spec allows
I looked into this at length a while back. I would recommend https://github.com/atlassian/commonmark-java
@nihilazo I have suffered so you don’t have to
(let [^Parser mdparser (-> (Parser/builder)
(.extensions [(StrikethroughExtension/create)
(HeadingAnchorExtension/create)])
(.build))
^HtmlRenderer md (-> (HtmlRenderer/builder)
(.nodeRendererFactory (code-border-render-factory))
(.build))
d (.format DateTimeFormatter/ISO_DATE
(LocalDate/now))]
(html5
[:main
[:section
[:time {"datetime" d} d]
(.render md
(.parse mdparser
(slurp "./path/to/file.md")))]]))
And you can use hiccup for template rendering in clojure
Parser parser = Parser.builder().build();
HtmlRenderer renderer = HtmlRenderer.builder()
.attributeProviderFactory(new AttributeProviderFactory() {
public AttributeProvider create(AttributeProviderContext context) {
return new ImageAttributeProvider();
}
})
.build();
Node document = parser.parse("");
renderer.render(document);
// "<p><img src=\"/url.png\" alt=\"text\" class=\"border\" /></p>\n"
(let [parser (-> (Parser/builder) (.build))
renderer (-> (HtmlRenderer/builder)
(.attributeProviderFactory
(reify AttributeProviderFactory
(create [this context]
(ImageAttributeProvider.))))
(.build))
document (.parse parser "")]
(.render renderer document))
class IndentedCodeBlockNodeRenderer implements NodeRenderer {
private final HtmlWriter html;
IndentedCodeBlockNodeRenderer(HtmlNodeRendererContext context) {
this.html = context.getWriter();
}
@Override
public Set<Class<? extends Node>> getNodeTypes() {
// Return the node types we want to use this renderer for.
return Collections.<Class<? extends Node>>singleton(IndentedCodeBlock.class);
}
@Override
public void render(Node node) {
// We only handle one type as per getNodeTypes, so we can just cast it here.
IndentedCodeBlock codeBlock = (IndentedCodeBlock) node;
html.line();
html.tag("pre");
html.text(codeBlock.getLiteral());
html.tag("/pre");
html.line();
}
}
(defn indented-code-block-node-renderer [context]
(let [html (.getWriter context)]
(reify NodeRenderer
(getNodeTypes [_]
[IndentedCodeBlock])
(render [_ node]
(doto html
(.line)
(.tag "pre")
(.text (.getLiteral node))
(.tag "/pre")
(.line))))))
Isn't CommonMark PegDown based? It can get stuck on some edge cases. I researched about the same a few months back and found FlexMark-java better.
I barely started to wrap flexmark: https://github.com/hindol/flexmark-clj Don't use this library, but maybe you can see how to do interop.
just to reemphasize the point being made: there is a variety in Clojure libraries that wrap the underlying Java things. Some add a lot of leverage, some don't
Yeah, that's not a wrapper per se. I was trying to structurally parse a Markdown and the only library I found did Markdown -> HTML -> Hiccup. So, I wrote my own that will go from Markdown to Flexmark's tree of nodes to Clojure data structures.
I am confused by code in ring-codec
at https://github.com/ring-clojure/ring-codec/blob/master/src/ring/util/codec.clj#L60
Why is url-encode doing all that by hand… but form-encode is using java’s URLEncoder
(form-encode* [unencoded encoding]
(URLEncoder/encode unencoded encoding))
but URL encode is doing its own substitutions (which are using regexes and thus probably not very fast)
I had to look into something similar a couple of weeks ago - URLEncoder is not quite the same as URIEncode from Javascript, and not 100% the same as usual form/multipart encoding. I wouldn't worry about regex performance, usually that's not the bottleneck in request processing, especially if you're doing database calls etc
@nihilazo I have suffered so you don’t have to
(let [^Parser mdparser (-> (Parser/builder)
(.extensions [(StrikethroughExtension/create)
(HeadingAnchorExtension/create)])
(.build))
^HtmlRenderer md (-> (HtmlRenderer/builder)
(.nodeRendererFactory (code-border-render-factory))
(.build))
d (.format DateTimeFormatter/ISO_DATE
(LocalDate/now))]
(html5
[:main
[:section
[:time {"datetime" d} d]
(.render md
(.parse mdparser
(slurp "./path/to/file.md")))]]))
Is there a way to (set! *warn-on-reflection* true)
for all namespaces in src
and test
directories?
I use :global-vars {*warn-on-reflection* true}
in defproject
but that shows me warnings for deps also, e.g. a lot of CIDER warnings at startup.
There is of course the technique of editing all files in your project's src
and test
directories to add that single line to all of them. I do not know a way in Leiningen to do it another way. You could try asking in #leiningen channel to see if anyone there knows another way.
Does that affect performance in production? You can set :global-vars
for :dev
profile in Leiningen to avoid that.
No more than the time it takes to print the warning messages, I believe.
Ah yes, it happens only at compile time. No penalty.
The Clojure compiler must always determine when compiling Clojure code whether it can make a JVM interop call without using reflection, or whether it needs to use the reflection API. The main difference that I know between setting *warn-on-reflection*
to true, versus not, is the printing of the warning messages when it detects that it must use reflection.
Hi guys, I was compiling my project but got an error which I never have previously seen:
Error building classpath. Could not transfer artifact com.taoensso:encore:jar:2.117.0 from/to clojars (
Please help me resolve this if you have faced this before 🙏
Today in typos: If you call
(let [state (atom {})]
(count @atom))
That is @atom
instead of @state
, you get the following stacktrace …
java.lang.ClassCastException: class clojure.core$atom cannot be cast to class java.util.concurrent.Future (clojure.core$atom is in unnamed module of loader 'app'; java.util.concurrent.Future is in module java.base of loader 'bootstrap')
at clojure.core$deref_future.invokeStatic (core.clj:2298)
clojure.core$deref.invokeStatic (core.clj:2320)
clojure.core$deref.invoke (core.clj:2306)