Hello! Does prismatic/schema has some type of union types? Can I do an or
with 2 type definitions? :thinking_face:
it also has either
it's deprecated for some reason (while cond-pre is alpha... ๐ in practice it works well)
its or
is called cond-pre
(odd, I know)
I recently asked how to get the matches with core.match from a regex, now I figured it out:
(require '[clojure.core.match :refer [match]])
(defn regex [x]
(re-matches #"(\w+)->(\w+)" x))
(match "foo->bar"
([_ x y] :<< regex) [x y]
:else [])
;;=> ["foo" "bar"]
it would be very funky if regexes were also IFn
s in clojure ;)
@borkdude not easy in the JVM -- Patterns are final and there's no interface that backs them
so making a Pattern that is an IFn is not possible
you'd have to use a wrapper class, but then interop becomes indirect
I guess it would have worked if IFn was a protocol from the start. Not sure what would have been the perf implication
but also, not sure if this is a good idea at all, since it's not clear what implementation for IFn would be the best, re-find, or re-matches
Just a funky idea ;)
yeah a protocol for IFn (with the normal protocol semantics) would be a big performance penalty
not sure if that's still true in the age of indy/condy
I have a question regarding Clojure and GC. Suppose I have code like this:
(defn my-fn []
(let [xs (fn-which-produces-lazy-seq)]
(->> xs
(map some-side-effect-fn)
(doall))
nil))
Will the entirety of xs
be held in memory at least until the function exits?@stephenmhopper with doall
yes, with dorun
no
if you only need to walk the collection with a side effecting fn, you can also use run!
or doseq
Why, given that the value of doall
ends up being unused?
Based on the docs for doall
and dorun
, it looks like doall retains a pointer to the head, but dorun does not?
yes
"Retains a pointer" means just that it returns its argument instead of returning nil
.
But later that argument is still ignored in your my-fn
, so I still don't see how using doall
would cause the pointer to that coll being retained.
doall
is, abridged:
(defn doall [coll]
(dorun coll)
coll)
The entire collection xs
will be realized and kept into memory at one point, which is what I meant. After the doall
call it can be GC-ed if it's no longer used
which is, in the example, the end of the function
Just so we're on the same page here - if we replace that last nil
in my-fn
with (Thread/sleep forever)
then with both doall
and dorun
the xs
collection will be GC'ed despite the function never exiting, right?
xs
is still pointing to the head of the collection though, so wouldnโt that prevent it from being GCโd regardless of whether we use doall
or dorun
?
Or does the Clojure compiler work some kind of magic to release that pointer early?
@p-himik yes
@stephenmhopper Clojure has locals clearing. So if a local isn't used in the body anymore, it releases it for GC
It also might be true in Java, according to a SO post: > While the object won't be garbage collected if it is still in scope, the JIT compiler might take it out of scope if the variable isn't actually used any further in the code [...] even though when you read the source code the variable still seems to be "in scope."
Can you elaborate on the performance penalty? I thought it was just another function call & a type dispatch, but I admit Iโve never looked into the internals here.
I've written some code to convert some java objects to clojure data structures, i want to write some tests, whats the best/easiest way to creat java classes/objects in clojure?
The most obvious one is to just instantiate these Java objects in your Clojure tests
I need to define what the classes are though, i.e. I want to define a class with the methods getX()
and getY()
Right - there's a couple of ways to do it - you can add Java files to your project, and let your tooling compile them and make them available in your class path, or you can use the interop and create Clojure namespaces which will produce classes/objects you need, or lastly if you have interfaces to implement you can use something like proxy
: https://clojure.org/reference/java_interop#_implementing_interfaces_and_extending_classes or https://clojuredocs.org/clojure.core/gen-class
And if you want to avoid AOT hassles, you can use https://github.com/tailrecursion/javastar or https://github.com/ztellman/virgil
The latter requires writing Java and there's no immediate way of making it work with a deps.edn
-based project - you will have to write a few lines of code.
I haven't used the former so can't really comment on it.
invoking an IFn
is just a checkcast
followed by an invokeinterface
call
on the other hand, a protocol function invocation is around 20 bytecodes (inline caches, direct invocation paths etc)
even in the fast case where it bouls down to an invokeinterface, there's an interface instanceof + goto , which is fast but still slower than not doing it
but the extra unused bytecodes will mean that JVM inlining will be affected
clojure.java.data
if you are just working with library stuff