This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-01-29
Channels
- # announcements (13)
- # aws-lambda (19)
- # babashka (11)
- # beginners (90)
- # calva (35)
- # chlorine-clover (16)
- # cider (8)
- # clj-kondo (6)
- # cljsrn (10)
- # clojure (44)
- # clojure-australia (1)
- # clojure-europe (35)
- # clojure-nl (6)
- # clojure-norway (7)
- # clojure-uk (101)
- # clojurescript (79)
- # community-development (40)
- # conjure (1)
- # contributions-welcome (4)
- # core-logic (2)
- # cursive (6)
- # datomic (4)
- # figwheel-main (5)
- # fulcro (18)
- # girouette (1)
- # graalvm (3)
- # hoplon (2)
- # hugsql (2)
- # jobs (5)
- # malli (26)
- # off-topic (74)
- # pathom (36)
- # portal (4)
- # reagent (10)
- # reitit (23)
- # remote-jobs (1)
- # shadow-cljs (27)
- # spacemacs (10)
- # sql (3)
- # startup-in-a-month (6)
- # tools-deps (55)
- # vim (11)
- # xtdb (15)
Hello! Does prismatic/schema has some type of union types? Can I do an or
with 2 type definitions? :thinking_face:
it's deprecated for some reason (while cond-pre is alpha... 🌀 in practice it works well)
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"]
@borkdude not easy in the JVM -- Patterns are final and there's no interface that backs them
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
yeah a protocol for IFn (with the normal protocol semantics) would be a big performance penalty
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.
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
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
Based on the docs for doall
and dorun
, it looks like doall retains a pointer to the head, but dorun does not?
"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.
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
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?
@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."
That’s good to know. Thanks @borkdude and @U2FRKM4TW
if you only need to walk the collection with a side effecting fn, you can also use run!
or doseq
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?
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