This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-09-26
Channels
- # announcements (2)
- # asami (2)
- # aws (34)
- # babashka (6)
- # beginners (9)
- # calva (76)
- # chlorine-clover (10)
- # circleci (5)
- # clj-kondo (2)
- # clojure (40)
- # clojure-australia (3)
- # clojure-europe (15)
- # clojurescript (39)
- # conjure (1)
- # core-async (4)
- # cursive (4)
- # datahike (1)
- # datomic (69)
- # figwheel-main (1)
- # graalvm (16)
- # honeysql (9)
- # hyperfiddle (2)
- # jobs-discuss (2)
- # lsp (36)
- # luminus (1)
- # malli (11)
- # off-topic (13)
- # pathom (1)
- # portal (1)
- # portkey (3)
- # reitit (25)
- # reveal (1)
- # rewrite-clj (5)
- # spacemacs (2)
- # sql (4)
- # vrac (90)
I feel when I write macro I often want do have conditionals like:
`(try
~@try'
~(when catch
catch)
~(when finally
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:
`(try
~@try'
~@(when catch
[catch])
~@(when finally
[finally]))
Seems to workanother technique is to not use ` and instead cons up a coll of clauses which may be conditionally built
(cons `try (cond-> try'
catch (conj catch)
finally (conj finally)))
something like thatusing malli
, is it possible to spec a binary add
function so that we check that the result is greater than the arguments?
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
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.
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: https://github.com/cljfx/cljfx#atoms
#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
It happens when you tell it to happen - no difference from Java here. A handy guide: https://clojure.org/reference/compilation
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.
> 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
Quickly, delete it before someone else sees it and decides to use in the real code, with multiple forms within as->
. :D
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)
~arg
~@(next form)))
(defmacro ->>
[& forms]
`(clojure.core/->>
~@(for [form forms]
(if (and (seqable? form)
(#{'-> 'as->} (first form)))
`(<<- ~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.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".
(there's also a good argument for not mixing the arrow types since it says you're mixing "things" and "sequences" in your pipelines)