Fork me on GitHub
Garrett Hopper03:06:25

I know it's probably almost never used, but is there a good way to unquote and deref an atom in a macro?

(defmacro example []
  (let [a (atom nil)]
    `(println [email protected])))
The best I've got is
(defmacro example []
  (let [a (atom nil)]
    `(println ~(deref a))))
(defmacro example []
  (let [a (atom nil)]
    `(println ~(-> @a))))
Something feels wrong about both though.


Might as well deref in the let

Garrett Hopper03:06:55

I suppose this was a bad example, but you have a point. I can just redefine the symbol as the derefed version, since the atom's state is created in other let bindings.

Timo Freiberg10:06:08

or (to prevent an XY problem), can anybody suggest a way to only eval once instead of once per line?

Timo Freiberg10:06:19

i think i got it.

(defn make-filter
  (let [raw-expr (read-string filter-expr-str)
        filter-expr (partial
                     (eval `(fn [~(symbol "it")]
seems to evaluate the predicate once in the closure!

👍 4
Christian Johansen13:06:12

spec question: is there some way to make spec consider nil the absence of a key? When using s/keys with optional keys, the spec still fails if I include the key with an explicit nil, which causes me to define the key as something that is either nil or a usable thing

Christian Johansen13:06:33

(s/def ::key string?)
(s/def ::mymap (s/keys ::opt [::key]))

(s/valid? ::mymap {}) ;; true
(s/valid? ::mymap {::key nil}) ;; false


you have spec/nilable for that

Christian Johansen13:06:01

a little bit better, but it still defines the key as nilable. ideally I’d like to say that nils are OK for optionals

Christian Johansen13:06:26

with nilable, you can sneak nils into required map keys, which I don’t want

Christian Johansen13:06:50

guess I could write my own instrument that would strip nils from maps


> you can sneak nils into required map keys well, the idea is to make nilable only the optional keys (not the required keys). but I understand that this solution might not be ideal for you.

Christian Johansen13:06:14

this stops working the moment you have a key that is required in one context, and optional in another

Christian Johansen13:06:35

and thus works against the idea of having global reusable specs


I'm not sure I understand, but isn't what you're doing what's actually working "against the idea of having global reusable specs"? If you say in one place that ::key should be a string, but you can pass ::key nil in another place then (s/def ::key string?) is not globally true.


The thing is that you want to make ::key nilable in the context of ::mymap, but then it's no longer ::key according to your spec because now it can be nil


You could do something like

(s/def ::key string?)
(s/def :mymap/key (s/nilable ::key))
(s/def ::mymap (s/keys ::opt [:mymap/key]))


which is more verbose but still stays true to the global specs

Christian Johansen13:06:38

Yes, this works, but is very cumbersome. Given that Clojure has nil punning almost everywhere I’d expect this to be the default behavior of :opt

Christian Johansen13:06:04

e.g., I can’t think of any other area in Clojure where {::key nil} would be handled much differently from {}


(let [{:keys [a b] :or {a 1}} {:a nil :b 3}]
    [a b])
=> [nil 3]

(let [{:keys [a b] :or {a 1}} {:b 3}]
   [a b])
=> [1 3]

Christian Johansen13:06:53

hah, there’s one 🙂


this would be one case 😛

Christian Johansen13:06:47

(s/keys :req [] :opt [] :opt-nillable []) 😛

Christian Johansen13:06:25

I agree with all of the rationale behind spec, that’s why I’m using it. I just slightly disagree with the definition of “optional” and nils

Christian Johansen13:06:51

typically explicit nils will be the result of things like this:

Christian Johansen13:06:12

(some-fn {:some-key (when some-val (compute some-val))})

Christian Johansen13:06:32

but I can write it as:

Christian Johansen13:06:10

(some-fn (merge {:default "Keys"}
                (when some-val {:some-key (compute some-val))}))

Christian Johansen13:06:52

and it’s not the end of the world… just results in a bit more friction when introducing spec into an existing codebase


(cond-> {:default "Keys"}
            some-val (assoc :some-key (compute some-val)))
is also an option if you have a lot of cases


and I can see where you're coming from, it's definitely more verbose

Christian Johansen13:06:31

thanks for chipping in


@andre.stylianos come on man, we have good ol' remove-nil-values-from-map

(defn remove-nil-values-from-map [m]
  (->> m
       (filter second)
       (into {})))
no need for cond->/`as->` + assoc-in hell imo

😂 4

There's that 😂


that will remove false values as well though right?


(filter (comp some? second)) maybe? (had order backwards)


i get those backwards all the time 🙂

😂 4

(me too. also empty and empty?...)


you can squeeze that in an xform for extra points


(into {} (filter val) m)

☝️ 4

(into {} (filter (comp some? val)) m) summing up then?


Hi, I just switched from http-kit to jetty. I have an endpoint that when i request it, i get a "java.lang.StackOverflowError" ... the same endpoint returns fine in http-kit and also, when i repl onto the server and try calling the function directly with the same parameters, it also returns ok. Anyone have any suggestion as to how i would debug this ?


I’ve a question about swap!. According to the doc:

Atomically swaps the value of atom to be:
  (apply f current-value-of-atom args). Note that f may be called
  multiple times, and thus should be free of side effects.  Returns
  the value that was swapped in.
I have println statements in f. Thus, strictly speaking the function has side effects, however, it always returns the same value for given input so it should be safe to use it. Am I missing something fundamental here?


I think that is fine, as long as no one relies on the println statements


Thanks for replying @bherrmann Why is that? What could happen to the print statements?


could be invoked multiple times


swap might try repeatedly to update the atom… so it is possible for f to be invoked more than once…. if there is contention for the atom.


If two threads are calling swap! on the same atom at the same time one of the callers might have to repeat?


Oh, you were faster 🙂 Thanks a lot!


@alexmiller what would be the workflow to get strint templating working in clojurescript? I am willing to work on it but I have never submitted a patch to clojure code 🙂

Alex Miller (Clojure team)15:06:27

if you’re asking about cljs, I’d head to #cljs-dev

Alex Miller (Clojure team)15:06:18

however, there is support for some of this stuff in format and cl-format (not sure the status of those in cljs)


what are thoughts on side effects in macros? bad idea, or horrible idea?


e.g. a macro that performs some analysis and writes to a file


(I'm actually thinking about this more in a CLJS context so maybe I should ask in that channel)


What analysis are you expecting it to write? If it's anything mutable then how would that work?


@lilactown if the side effect can be done in its own function, that would be my preferred way to handle it. Ask yourself why you need the macro.


the usecase I was thinking of was generating a .css file from a clojure form


In that case it seems reasonable, since you want the form unevaluated I would assume.


But there may be better ways that I can't think of yet pre-caffeine 🙂


with deps.edn, I’m trying to override a git dep with a local dep. My deps.edn looks like this:

 {org.clojure/tools.analyzer.jvm {:mvn/version "0.7.2"}
  seesaw                         {:mvn/version "1.5.0"}
  rakk                           {:git/url ""
                                  :sha     "a4b5717086e66989856a45a1f6e5d04628e6aec3"}
  cross-parinfer                 {:mvn/version "1.4.5"}
  org.clojure/tools.deps.alpha   {:git/url ""
                                  :sha     "d492e97259c013ba401c5238842cd3445839d020"}}
 {:repl {:extra-deps {org.clojure/tools.nrepl {:mvn/version "0.2.13"}
                      cider/cider-nrepl       {:mvn/version "0.17.0"}}}
  :local {:override-deps {rakk {:local/root "/Users/sideris/devel/rakk"}}}}}


but when I do clj -Sforce -R:local:repl -Spath I see that rakk is coming from git rather than the local dir

Alex Miller (Clojure team)16:06:09

This doesn’t work yet

Alex Miller (Clojure team)16:06:34

There’s a ticket for it


@alexmiller is it the particular combination of git+local or overriding doesn’t work in general?


@alexmiller also do think in the future there may be a way to say “also merge this local deps.edn into the main map” to avoid having to commit machine-specific paths to git such as "/Users/sideris/devel/rakk"

Alex Miller (Clojure team)16:06:49

The issue is with changing and comparing versions across coord types


hm, I can imagine how that could be a problem

Alex Miller (Clojure team)16:06:08

I’m trying to understand your other question there

Alex Miller (Clojure team)16:06:21

And how it’s different from -Sdeps


-Sdeps replaces deps.edn, correct? I was looking for something that would merge a snippet into an existing deps.edn

Alex Miller (Clojure team)17:06:35

no it merges over the rest

Alex Miller (Clojure team)17:06:50

it acts as a final deps.edn in the merge chain


oh sorry, that would work then


> -Sdeps DEPS Deps data to use as the final deps file the word “final” threw me off, I thought it meant that it replaces everything


maybe “last” would be less ambiguous, maybe it’s just my brain 😄


thanks for clarifying


Hello fellow Clojurians, I just wanted to put out a general question for the community. What is the benefit of writing css using garden compared to just writing plain old css in css files?

Alex Miller (Clojure team)17:06:07

presumably, b/c it’s data and you can manipulate it as data

Alex Miller (Clojure team)17:06:36

but if you don’t need to do that, then that’s not much benefit


Yeah that's what I thought, from what I've seen it's typically written in clojure. The downside I see to this is if you want to manipulate it you'd have to do it on the backend. The main benefit I was hoping to get out of it is to have dynamic css within clojurescript, where events would trigger changes in css. However I haven't found a great way of doing so.


hi i'm trying to use to save an html uploaded image to local disk and when i invoke` (copy (io/file tempfile) (io/file "some-dir-name-i-just-invented-because-i-want-a-new-file-here.jpg"))` i'm informed that the destination file doesn't exist. scratches head


@sova just to check, are you calling:

(io/copy (io/file "tempfile") (io/file "new.jpg"))


@sova I don't think copy creates directories implicitly


but there's a separate method on File that will do that given a File object


i need to have the directories exist already


this is an intentional feature btw - for example it can prevent a bad user input from making you create a file on a malicious path


excellent. now this image board will actually work now.


yeah good call. and i'd rather not accidentally create ten thousand directories just trying to see where stuff gets saved


thanks for your help guys. step by step great accomplishments


Out of curiosity, does anyone recognise getting a StackOverflowError java.util.regex.Pattern$BmpCharProperty.match exception when running (apply str a-large-coll-of-strs)? It’s rather puzzling how it’s happening. If it helps the coll contains seqs of the file contents from an unpacked zipfile.


are you sure the exception is happening there?


I would guess you a regex searching the string after you are constructing it, and the error is being thrown there


it could be an error in something lazy that created a-large-coll-of-strs


Possibly, I’ve got a zipfile and a reducer which pulls out the content of a matching file, so I get a big list of strings. Big being relative, (count coll) gives me 1004. (every? string? coll) gives me true.


so I’m pretty sure I only have strings in there, but (apply str coll) just throws the exception


I’m more than willing to admit my java interop isn’t great, but I’m really not sure whats up here…


how sure are you that the apply str is what is throwing the exception? do you have a stracktrace with its line number?


now running (apply str (take 800 coll)) works perfectly


what about (apply str (drop 800 coll))


(apply str (take 900 coll)) does not, but (apply str (take 200 (drop 800 coll))) does


apply str doesn't do anything with regexs, which is why I am asking the question about how sure you are


Not certain, that’s why I’m really puzzled. The problem is the error is a stackoverflow error, and I have no idea how to read something that deep


what do you mean?


the stracktrace isn't printing out?


*e just gives me: elided at the top as it goes off the terminal

  [java.util.regex.Pattern$BmpCharProperty match "" 3798]
  [java.util.regex.Pattern$GroupHead match "" 4658]
  [java.util.regex.Pattern$Loop match "" 4785]
  [java.util.regex.Pattern$GroupTail match "" 4717]
  [java.util.regex.Pattern$Curly match0 "" 4272]
  [java.util.regex.Pattern$Curly match "" 4234]
  [java.util.regex.Pattern$CharProperty match "" 3777]
  [java.util.regex.Pattern$BmpCharProperty match "" 3798]
  [java.util.regex.Pattern$GroupHead match "" 4658]
  [java.util.regex.Pattern$Loop match "" 4785]]}


It’s pretty long


so print it out in something that will let you scroll


It’s scrolling off the top of the repl


near the top (at the top modula things like reflection) will be the line that caused the error


use a better repl then


or a terminal with scroll back, or tmux, or run it in emacs, or whatever


the point is your error happened a long time before (apply str ... its just the the computation was delayed until it was realized here


yep, just trying to remember how to do print -> str so I can take the first 200 characters. (.printStackTrace *e)


are you just interacting through a terminal?


my guess, entirely unsupported at the moment, is you are printing the big string out at the repl, and your repl client is actually trying to run a regex on it


I’m using a terminal


Ok, so the test would be to do a take on the result


@hiredman, that was brilliant, your supposition is accurate it seems!


Right, time to file a bug with the repl author 😃


or a warning


is it rebel readline?


can't think of how else you would have java in the repl


nice sleuthing hiredman


very nice, very impressed!


Thank you 😃


his tokenizer is regex based


Yep, also found that someone’s added the issue, so he’s aware of it


No point bugging him again.


@dpsutton Spec would have been a more readable solution.


How can I use clojure and avoid the JVM ?


you can use clojurescript (on nodejs). If you wanted some framework, you could use macchiato


I would recommend using learning clojure to get over whatever prejudices your have acquired against the jvm


^Its the fact that its heading in the direction of no longer being open source


there is a self hosted clojurescript effort, but I am not sure how much that is used, and there is a clr clojure port which is battling against the static language trend on the clr


what makes you say that?


Oracle are going to charge for Java SE, the latest developments will be paid for, so over time the openjdk efforts are going to be under a lot of pressure


it sounds like maybe you only read the title on a hacker news link? I see oracle is going to start charging for support of java se releases that are older then what they have stated they will support


and that is oracle's jvm, which is not openjdk


But what If openjdk cannot implement java 11 say?


openjdk is now the official, canonical distribution -- they start with implementing Java specs in openjdk. It's GPL


Clojure works just fine on the Oracle JVM.


clojure targets older style bytecode so that shouldn't be a problem anyways


that's a gist but probably not technically correct. Java 1.6 bytecode?


right, it targets 1.6, there's been talk about upgrading to 1.7 for invoke-dynamic but I don't think that's actually on the radar yet


is it the emitted java code or emitted bytecode that targets 1.6. My knowledge of jvm internals is quite thin


bytecode is generated for 1.6, the java is compiled to emit bytecode for 1.6 also


there is no java code emitted anywhere


Ok, but clojure without java interop is limited so its fairly tied to java


cljs and clj-clr are both up to date, and both are able to do everything to interop with their host that jvm clojure can do, up to the limits of the host


the difference is that there's less of an ecosystem with tooling and libraries for those platforms


(but cljs comes close)


So nodejs is the only choice? Does clj-clr run on the core clr?


cljs runs on nodejs, or rhino, or a browser js, there's also planck which self-hosts with OS provided js apis


but most libraries use features only found in a browser or node


clj-clr runs with microsoft's clr or mono


oracle is not closing up the jdk, basically the opposite


this is a common misconception on ill-informed hacker news and reddit comments


They just opened up a ton of previously commercial features (JFR tools)


Speaking of JDKs -- what do people do on MacOS to switch JDKs quickly?


cool - thanks. Does jenv also install them, or just manage environments?


I think I had to install them, I had a bunch already, I just connected to them


I set JAVA_HOME using java_home, which is sufficient. Example:

export JAVA_HOME=`/usr/libexec/java_home -v 1.8`


Even with different JDKs each project?


export JAVA_HOME doesn't help you much when you have non-cmd apps that use JDKs. I'd like to look at jEnv at some point. Currently, I set JAVA_HOME env variable in ~/Library/LaunchAgents/environment.plist


is there a way i can invoke a function with an optional argument / arity 3 but sometimes 4


what do you imagine it would look like? I can think of a few ways to do this


something like

(([one two three] ... 
([one two thee free] ..))


okay. i call this function on a /post command. it has an optional image parameter. i want to do that only when image isn't nil... i don't want to write nearly duplicate functions for slightly different scenarios... so maybe i will just always pass four and have that fourth one sometimes be nil.


turns out just letting it be nil is a great option. haha

Nick Cabral20:06:19

Anyone see a problem with picking one naming convention for a project, rather than using language-specific conventions at each layer of your stack? Seems more trouble than it’s worth to relate user-id in clojure to user_id in postgres and to userId in frontend (typescript) code.


I don't think it's that much trouble if you have some middleware handle it.

Nick Cabral20:06:36

I know the answer is “do whatever you want if it’s all your code”. Just interested in other opinions


I don't mind the stack specific convention, because trying to force one can sometimes be much more difficult than it seems, say if something you have no control over is introduced and it needs things to be stack specific convention to work