This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2019-08-05
Channels
- # announcements (3)
- # beginners (225)
- # calva (3)
- # cider (110)
- # circleci (18)
- # clj-kondo (15)
- # cljdoc (1)
- # cljsrn (12)
- # clojure (77)
- # clojure-dev (39)
- # clojure-europe (3)
- # clojure-houston (2)
- # clojure-italy (9)
- # clojure-nl (16)
- # clojure-romania (1)
- # clojure-spec (5)
- # clojure-uk (20)
- # clojuredesign-podcast (28)
- # clojurescript (89)
- # core-async (4)
- # cursive (10)
- # datomic (3)
- # defnpodcast (5)
- # emacs (17)
- # events (1)
- # figwheel (4)
- # graalvm (6)
- # juxt (1)
- # pathom (4)
- # pedestal (5)
- # re-frame (4)
- # remote-jobs (3)
- # rewrite-clj (4)
- # shadow-cljs (90)
- # spacemacs (2)
- # sql (7)
- # tools-deps (4)
- # vim (52)
- # xtdb (7)
@hiredman :thumbsup::skin-tone-2:
Hello. Do you know any example about web push using clojure? I found the library ”web push-clojure” but I want to know any example about this.
In this code:
(def a ["a" "b" "c" "d" "e" "f" "g"])
(def b (assoc a 3 42))
(identical? (first a) (first b))
; => true
Does Clojure guarantee they'll be identical? Does Clojure's current impl of vector guarantee they'll be identical?
I guess what I wanted to ask is, does assoc'ing something onto a vector of size n produce something that always shares at least n-1 things with the original vector?
Or are there cases when less things are shared?
the values in the vector will all be identical except the one you replaced
what else could they be?
I'm not questioning whether assoc
works. I'm wondering whether each leaf node in the HAMT has only one thing, and will never be copied as part of the path copying required to make the new vector.
If you are asking about the current implementation, it does not make copies of objects like that, that I am aware of. A counter-question for you: When would you care, except for performance?
I am not saying that performance is unimportant -- it definitely is important. It is just that if you are asking because you want to write code that relies upon things being identical? or not, I would advise against doing that, in most cases.
Performance was my concern. For vectors which contain very large things, copying even one thing as part of an assoc
or a conj
might be too expensive.
Definitely intending to program with values as much as possible.
I have looked through several of the Clojure implementations, and while you can find plenty of code for cloning 32-element arrays that are used as tree nodes, I have never seen any calls that try to 'clone' or copy the elements of the collection, when those elements are general Object type. There is no general clone method that is guaranteed to do anything useful across all Java objects, I don't believe.
There is a Clojure class of vectors restricted to primitive elements, and that one can copy arrays of 32 primitives when assoc/conj calls are done, but in that case, the whole point is to save memory and improve performance by limiting itself to all elements of one primitive type. This is the implementation of the vectors returned by the function vector-of
.
If you would like to explore some low level implementation details of Clojure data structures as they exist in memory, for Clojure on Java, there is this library I wrote that can help visualize the data sharing that occurs. In particular, the 'More example' section of the README has some sample code to color-highlight two HAMT's with different colors for data that is unique to one, or unique to the other, or shared: https://github.com/jafingerhut/cljol
Thanks!
I've made a JSON -> EDN tool and now someone asks about the case when you use key with spaces in JSON and keywordize them in EDN, you get something like this: {"foo bar": 1}
-> {:foo bar 1}
which is invalid EDN. I bet someone has thought of a solution regarding this before?
there are multiple solutions. one of them is to encode the space somehow, but it might not always that this is what users want
you could use string keys, or even emit (keyword "foo bar")
I guess edn/read would give you a useless list for that, so never mind
right
string keys avoid the problem, at the cost of people not liking it
I outlined these solutions: https://github.com/borkdude/jet/issues/10#issuecomment-518123007
can transit handle a space in a keyword?
I suspect the best answer depends on the context and all of those possible solutions are best in some context
that makes me wonder, what do you do with EDN values that have no JSON representation? That is an equally destructive operation right?
like, what happens when you are converting {:a 9007199254740992}
(maximum int fitting in a float continuously plus one) from EDN -> JSON
Is there an easy way to call the constructor for a class that you have the value for at runtime?
it might be easier to just use eval for something like that
clojure.lang.Reflector/invokeConstructor
seems to do the trick, but I believe is internal.
It's a specific class (`javafx.embed.swing.JFXPanel`) that may or may not be on the classpath depending on version of Java being used to run the program.
why not
user=> (if (try (Class/forName "Foo") (catch ClassNotFoundException _)) (eval '(Foo. x y)) :nope)
:nope
@zane does this help?
user=> (.newInstance (Class/forName "java.util.Date"))
#inst "2019-08-05T20:55:21.354-00:00"
only works with nullary constructors
good to know
rather than checking a jdk version, it is much better to check for the existence of a class before using it
That makes intuitive sense to me. Do you generally do that via Class/forName
, or some other method?
@U064X3EF3 Would you extend that to static class methods that were added with a specific version? That is to say, would you recommend checking for the existence of the specific method I want to use?
I think that would be analogous
"before I use a thing that may not exist, check that it exists"
trapping an exception in the failure case might be another option in some cases
Got it. This is really helpful. And the way you'd recommend to check for the presence of a specific method would be via clojure.reflect/reflect
?
as that's much less likely to break than version checking
(having debugged my share of buggy java version check conditionals)
Does anyone know if you can type hint an argument of a type created by gen-class
?
(gen-class :name model.Message)
(defn some-func [^model.Message msg] ...)
Oh nevermind I guess I can, maybe this is better for cursive channel because it’s complaining that the class can’t be found… which makes sense since if it’s not compiled