Fork me on GitHub

How can I attach metadata to a symbol in the def form?


(def credential (with-meta 'crendetial {:type :ratom}))
works. But I really like the following to work:
(def credential ^{:type :ratom} 'crendetial)


'^{:type :ratom} credential works!

👍 1
Søren Sjørup08:04:02

Is there a library that defines, these?

(defn less? [a b] (neg? (compare a b)))
(defn less-equal? [a b] (not (pos? (compare a b))))


So something like < and <= that also work on non-numbers? Why do you need these predicates? Where would you use them?

Søren Sjørup08:04:05

I use them on characters currently, but I’ve had other uses for them. They’re analogous to least and greatest from medley.


I'm not aware of any library that offers these but I feel that maybe it's an XY problem. > I use them on characters currently Yes but to what end? I mean maybe there's a better way to solve your actual problem without resorting to manual comparisons. E.g. to sort arbitrary data it would be better to

Søren Sjørup08:04:41

I’m writing a interval set for character ranges, for instace i have. a function:

(defn in [x [a b]]
  (and (less-equal? a x) (less-equal? x b)))
to test if a character is within an interval.


So you have a set of discrete values and a character and you want to do a membership check. I would: 1. build the set of characters based on the interval bounds 2. run (charset x) That way if charset is #{\b \d \c \e} and x is \e the result will be truthy, if charset is #{\b \d \c}, the result will be falsey.

Søren Sjørup09:04:28

Thank you. I want to avoid representing each character as this is for a regex engine and a interval set could potentially represent all characters.


Oh alright, I assumed ASCII 🙂 I think your solution is fine for bigger character sets. (Still, I'm not sure how meaningful Unicode ordering is, like should a smile emoji be greater than the plus sign emoji?)

Søren Sjørup09:04:31

I haven't tried but I would assume regex character classes us codepoint ordering.


Hey, I'm trying to rewrite some Java code to Clojure but bumping into some issues (maybe this can't be done in Clojure?). Basically I have a Go Library which I'm trying to import and call in Clojure. This works with the following Java code:

public interface GoTest extends Library {
        long Add(long x, long y);

    public static GoTest GO_TEST;

    static {
        String lib = System.getProperty("user.dir") + "/bin/";
        GO_TEST = (GoTest) Native.loadLibrary(lib, GoTest.class);
I'd like to avoid Java if possible. I wrote the following piece of Clojure:
(import '(com.sun.jna Library Native))

  (definterface GoTest
    (Add [this x y]))

  (extend GoTest Library)

  (defn load-library []
    (let [lib (str (System/getProperty "user.dir") "/bin/")]
      (Native/loadLibrary lib GoTest)))
but I get the following error:
Interface (GoTest) of
   library=/home/user/clojure-go/bin/ does not
   extend Library
Any ideas? Or is this just not possible in Clojure?


Not sure whether it'll work but try

(definterface GoTest
  (Add ^long [this ^long x ^long y]))


Same result, sadly


Oh, wait, you have extends in the Java code.


Is that unsupported in Clojure?


Indeed, definterface by itself doesn't have that ability. But you can use gen-interface.


Oh, and also - my bad for not realizing this - methods you specify in definterface don't accept this. So also remove this from there.


Let me try out gen-interface


Awesome! This works:

(gen-interface :name "clojure_go.core.GoTest"
                 :extends [com.sun.jna.Library]
                 :methods [[Add [Long Long] Long]])


Thanks for the tip 😄


@UG9U7TPDZ you could also consider implementing a golang pod so you can call it through the babashka.pods library which also works in the JVM. No native stuff :)


Interesting idea :thinking_face: But you would have a communication bottleneck I think. But maybe that would be really minimal


The reason I want to call Go is for performance


it depends on if you have: 1. many function calls 2. big sized arguments (as they are serialized) So if the overhead of the function call serialization is small compared to the actual work happening in Golang per function call then it would be a good fit. E.g. the sqlite3 pod and file watcher pod fall into this category


but if you add two numbers, and want the result then that's probably not a good fit


Right, for my usecase this might actually be a good fit. Not sure how I'd implement this though. Also I haven't gotten my own example working in GraalVM yet lol


But I'll think about this, thanks


there has to be no graalvm involved. you can look at the existing babashka pods in golang here:


@UG9U7TPDZ Another option might be to use #coffi and project Panama on JVM 17




Coffi also looks interesting, trying that out now


If you're targeting graalvm, then I think is the best option. I don't think coffi works with graalvm. If it does, that would be good to know!


But does @UG9U7TPDZ target graalvm native? I haven't read that he did... Please don't leave us in suspense!


Yeah I'm targetting graalvm (eventually)


> The reason I want to call Go is for performance I'm not sure if I understand that one. Go in general shouldn't be more performant than Java, or is it?


I'm happy to answer any questions. clojure <-> c interop unlocks a lot of potential!


although I agree with @U04V15CAJ that if the goal is performance, I'm not sure calling go from clojure will give you any better results than either calling java or optimizing your clojure code.


I was under the impression that Go did have a bit of an edge in terms of performance from the benchmarks I've seen, but maybe that's a bit misleading


Measure for your specific problem and in the typical scenario you're using it. Performance is hard to speak about in general is usually very contextual.


E.g. the JVM JIT may outperform Go when you run the program long enough


import-vars of potemkin exposes values and macros, is it possible to expose a namespace?

Noah Bogart13:04:19

You want to import all vars (defs, functions, and macros) from one namespace into another and then export them as if they are defined in the second namespace?

Noah Bogart13:04:04

If that's the case, then i would recommend against it lol

☝️ 2

In ns a , i’d like to import ns b and define b as a.b. so that, from ns c, it can access a.b by importing ns a only.

Noah Bogart14:04:26

Oh hmm. I don’t think that's possible. Why not just require ns b from within ns c? Namespace aren't private or hidden, so if ns a can require it, so can ns c.


I will say tho: The reason I know this is I purposefully circumvent it in my code because it messes with cursive symbol resolution.


If you were to make a runtime basis, but it did not include flags - just dependency and repository information - what would you call that?


Is there a way to tell iterate to end? Or I guess a lazy-seq in general? Does returning reduced work for that?


Say I wanted an iterate that generate the numbers from 1 to 10 and stopped


So you want to stop iterate nor from the consumer but from the f itself?


Ya, kind of what (take 5) does where it stops after 5


I wonder how take does that


I found a workaround:

(->> (iterate (fn [x] ...))
     (take-while (complement reduced?)))


That lets me return a reduced from the iterate fn and it'll stop. But I'm still curious, like how does take-while short-circuits the infinite seq to make it finite?


The implementation is rather trivial.


Hum... right so with lazy-seq, nil short-circuits, but I guess nothing can make iterate short-circtuits so you need to wrap it and not poll it anymore


That's right.


That answers my question haha, seems you can't short-circuit iterate, but wrapping it is pretty trivial and solves my issue cleanly enough


There is the new function, maybe iteration? That includes a stop condition