Fork me on GitHub

I feel when I write macro I often want do have conditionals like:

       ~(when catch
       ~(when finally
But since when returns nil when false, the macro ends up with nils in it. Is there some cool trick for this?


Oh, might have found a trick of my own just as I was asking:

       ~@(when catch
       ~@(when finally
Seems to work

👍 1
duckie 1
🙌 1
Alex Miller (Clojure team)05:09:19

another technique is to not use ` and instead cons up a coll of clauses which may be conditionally built

💯 1
Alex Miller (Clojure team)05:09:38

(cons `try (cond-> try' 
             catch (conj catch)
             finally (conj finally)))
something like that


using malli, is it possible to spec a binary add function so that we check that the result is greater than the arguments?


@meditans fyi, there is also a #malli channel :)


yes, thanks @U04V15CAJ, I posted there, but it seems a fairly low traffic channel, so I hoped to have more visibility here; if the duplication is a problem, I can delete the one in #malli


not a problem with me

Ben Sless10:09:15

malli is a pretty active channel for a lib channel, don't worry


hi! is there a way to have a UI component appear in the REPL to input data that is better done via a UI? (eg picking a date, or picking a set of elements from a list)


If by "is there a way", you mean would it be feasible to have such a REPL, the answer is yes. If by "is there a way", you mean using one of the existing popular REPLs and there UIs, the answer I think is no.


Something like #reveal is probably a good place to start


If you don't care about the input appearing right in the REPL UI and you would be content with such an input to be in a pop-up, then you can define an atom and use any UI library to show you the right control that changes the atom. Like it's done e.g. here:


#reveal , while being a great thing by itself, is probably more confusing in this case than helpful. It's not a REPL, it's an extra UI that you usually use in addition to an existing REPL.


I have a requirement that a particular class exists when the application exists. The class is a concrete implementation of an interface. I tried using reify this, but the class will only exist when the call to reify is loaded. Is it possible/recommended that I implement this class in Java and then distribute a the compiled class files with my project? My understanding is that java is binary compatible so if I compile with an old enough java it should “just work” on virtually any user’s environment. Is that right?


• yes you can just include a class file you got from java • alternatively you can AOT a namespace to get a class file to exist for it with :gen-class


When does the compilation of a :gen-class namespace happen?


Also, thanks for the confirmation about distributing classes!


It happens when you tell it to happen - no difference from Java here. A handy guide:


I think I misunderstood the suggestion to use :gen-class. Is the idea that with a :gen-class namespace there is no need to distribute class files because Clojure can automatically compile them on application start or is the suggestion to use :gen-class as a means of creating some class files to include the distribution?


As mentioned in that guide: > gen-class and the :gen-class directive are ignored when not compiling. It's called Ahead Of Time compilation - you gotta compile it before you run it.


Gotcha. That was my understanding. I have used :gen-class a number of times before when either 1) the entire app is AOT compiled or 2) there is a separate build step for compiling a few gen-class namespaces. I’m not sure if the class files produced by :gen-class bind your application to a particular version of Clojure or not. I believe Clojure libraries are specifically distributed as source to avoid this issue.


If I am wrong about that, then :gen-class seems like good solution to keep the entire codebase in Clojure. Beyond that, it is kind of orthogonal to the issue of distributing a library that requires certain classes exist on the classpath before the class-loader that references them is created.


> I’m not sure if the class files produced by :gen-class bind your application to a particular version of Clojure or not Yes, but at the same time Clojure tries really hard to not introduce backwards-incompatible changes. AFAICT, it's pretty much the same as Java - compiling something will tie you to the version that you used, but it's very likely that you will be able to use the same thing with some future version of Clojure/Java.


Ah, okay. That's good to know. Thanks!


> but it's very likely that you will be able to use the same thing with some future version of Clojure/Java. > Just to confirm we are on the same page, in this context "thing" means the exact class file. No need for recompilation, right?


Right. At least, that's my personal understanding. Maybe someone more knowledgeable will correct me.


Wow, I just realized as-> can be used to destructure!

(-> [1 2]
    (as-> [one two]
        (println one two)))

;;=> 1 2

notbad 1
🤯 3

Quickly, delete it before someone else sees it and decides to use in the real code, with multiple forms within as->. :D

😆 4

I've also been wondering what people think of if the thrrad-last macros were made aware of thread-first macros so they could compose? You know how you can use a thread-last inside a thread-first? But the other way doesn't work. Well what if you did:

(defmacro <<-
  [form arg]
  `(~(first form)
    ~@(next form)))

(defmacro ->>
  [& forms]
    ~@(for [form forms]
        (if (and (seqable? form)
                 (#{'-> 'as->} (first form)))
          `(<<- ~form)
For example, which means that now you can compose them like so:
(->> [1 2 3]
     (map inc)
     (as-> [a b c]
         (+ a b c)))
Does this seem like a good idea? I know it kind of breaks the simplicity of the macro, but I feel it makes them nicely compose.


i think you are unironically suggesting ->->><?as->cond->!


Haha, hum :thinking_face:


What's wrong with:

(-> [1 2 3]
    (->> (map inc))
    (as-> [a b c]
      (+ a b c)))
Why is everyone always trying to make arrow macros more complex? They do exactly what they're supposed to.


Well, when you have longer sequence manipulation, you'll have ->> at first, and suddenly you might realize, oh this function takes the coll as first arg, now you'd need to refactor the whole chain.


Or put some other way, I more often need the composition the other way around, since its generally that I'll modify a sequence, and near the end, use a non sequence function on it, like one of the string functions or something else.


All you need to do is wrap the ->> expression with -> - hardly "refactoring the whole chain".


Its subjective how "annoying" you find it or not I guess.


(there's also a good argument for not mixing the arrow types since it says you're mixing "things" and "sequences" in your pipelines)