Fork me on GitHub
#clojure
<
2021-01-29
>
matheusashton01:01:17

Hello! Does prismatic/schema has some type of union types? Can I do an or with 2 type definitions? :thinking_face:

vemv01:01:55

its or is called cond-pre (odd, I know)

borkdude09:01:01

it also has either

vemv10:01:28

it's deprecated for some reason (while cond-pre is alpha... 🌀 in practice it works well)

borkdude11:01:04

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"]

3
borkdude11:01:47

it would be very funky if regexes were also IFns in clojure ;)

🙌 3
bronsa11:01:51

@borkdude not easy in the JVM -- Patterns are final and there's no interface that backs them

bronsa11:01:06

so making a Pattern that is an IFn is not possible

bronsa11:01:30

you'd have to use a wrapper class, but then interop becomes indirect

borkdude11:01:54

I guess it would have worked if IFn was a protocol from the start. Not sure what would have been the perf implication

borkdude11:01:36

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

borkdude11:01:45

Just a funky idea ;)

bronsa11:01:04

yeah a protocol for IFn (with the normal protocol semantics) would be a big performance penalty

Robert Mitchell16:01:25

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.

bronsa17:01:23

invoking an IFn is just a checkcast followed by an invokeinterface call

👍 3
bronsa17:01:20

on the other hand, a protocol function invocation is around 20 bytecodes (inline caches, direct invocation paths etc)

bronsa17:01:42

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

👍 3
bronsa17:01:12

but the extra unused bytecodes will mean that JVM inlining will be affected

bronsa11:01:15

not sure if that's still true in the age of indy/condy

stephenmhopper12:01:42

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?

borkdude12:01:38

@stephenmhopper with doall yes, with dorun no

p-himik12:01:13

Why, given that the value of doall ends up being unused?

stephenmhopper12:01:01

Based on the docs for doall and dorun, it looks like doall retains a pointer to the head, but dorun does not?

p-himik12:01:05

"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.

p-himik12:01:12

doall is, abridged:

(defn doall [coll]
  (dorun coll)
  coll)

borkdude13:01:36

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

borkdude13:01:49

which is, in the example, the end of the function

p-himik13:01:06

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?

stephenmhopper13:01:11

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?

stephenmhopper13:01:53

Or does the Clojure compiler work some kind of magic to release that pointer early?

borkdude13:01:00

@stephenmhopper Clojure has locals clearing. So if a local isn't used in the body anymore, it releases it for GC

p-himik13:01:03

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."

👍 7
stephenmhopper13:01:36

That’s good to know. Thanks @borkdude and @U2FRKM4TW

borkdude12:01:24

if you only need to walk the collection with a side effecting fn, you can also use run! or doseq

j3vs16:01:36

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?

lukasz17:01:54

The most obvious one is to just instantiate these Java objects in your Clojure tests

j3vs17:01:44

I need to define what the classes are though, i.e. I want to define a class with the methods getX() and getY()

lukasz17:01:13

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

p-himik17:01:11

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.

emccue17:01:13

clojure.java.data if you are just working with library stuff