This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-06-13
Channels
- # babashka (7)
- # babashka-sci-dev (3)
- # beginners (29)
- # biff (16)
- # calva (2)
- # clojars (1)
- # clojure (50)
- # clojure-austin (5)
- # clojure-europe (29)
- # clojure-france (8)
- # clojure-nl (3)
- # clojure-uk (3)
- # clojured (10)
- # clojurescript (19)
- # code-reviews (3)
- # core-async (22)
- # cursive (5)
- # data-science (11)
- # datalevin (1)
- # datomic (10)
- # eastwood (4)
- # helix (4)
- # introduce-yourself (2)
- # jobs (1)
- # jobs-discuss (1)
- # joyride (6)
- # leiningen (4)
- # london-clojurians (2)
- # lsp (82)
- # malli (7)
- # meander (12)
- # minecraft (3)
- # nbb (14)
- # off-topic (52)
- # podcasts (3)
- # portal (3)
- # re-frame (32)
- # reagent (9)
- # releases (2)
- # shadow-cljs (95)
- # tools-deps (14)
I forgot the typical naming when do in a for. loop? What shall the the x & xs be ?
(loop [[x & xs] col] ,,,)
feeling like xs and col are competing for the same name.That seems reasonable to me. Singular "thing" (`x`) followed by possibly more "things" (`xs`). I'd use coll
(for collection) rather than col
(which suggests column).
The other possibility is (loop [coll coll] ...)
and refer to (first coll)
inside the loop (and presumably (rest coll)
in the recur
)
I would tend to assume h
was height and t
was time, seen outside that h & t
context.
x & xs
is idiomatic: https://guide.clojure.style/#idiomatic-names
(I think that guide is overly-narrow in assuming x
and y
are numbers)
I admit x & xs is idiomatic. It’s just the coll in [ [x & xs] coll] looks weird. The coll
shall also be something like xs
but sadly the name is already taken.
You can also repeat things if you prefer:
(loop [[x & xs] xs]
...)
Or:
(loop [[x & coll] coll]
...)
Normally I use coll
when it's a collection, like a vector, map, set, etc. And I use xs
when I'm talking about a seq or something I'll use as a seq. Often my function takes a coll, but rest and next return a sequence, so I find what you had first was best:
(loop [[x & xs] coll]
...)
(let [coll [1 2 3 4 5 6]]
(loop [acc 0 [x & xs] coll]
(if x
(recur (+ x acc) xs)
acc)))
coll
is the full collection, x
is the element as you loop, xs
is what's left to loop over.> When you recur, you will do (recur xs)
you won’t use coll anywhere inside the loop (edited)
Sounds like you are using x
to test if coll
is empty or not.
I'm testing x to check if there are any elements left or we've looped over all of them. It's my recursive exit condition. It'll also handle coll being empty, but that's not the main purpose, it's checking if we have anything left to loop over.
I'm not sure, but maybe you're confused about the behavior of loop/recur? It doesn't iterate over coll by itself. It works more like a let binding. You say let x be the first value of coll, and xs be the next values of coll. Then you do something, and you can choose to recur. When you recur, you have to bind new values to x and xs. You need to choose what they'll be bound too in the second loop, it doesn't have to be coll anymore, and most likely it wouldn't be.
Today I jumped from studying Kotlin for server development to Clojure. It feels like throwing off a straightjacket too tight. I started off with Kotlin because a close friend of mine advised me to move off Clojure since "it won't get me a job", but my gut instinct tells me that I use what I like and most of all makes me productive to the most; Everything else is secondary.
> https://www.poetryfoundation.org/poems/44272/the-road-not-taken > https://www.poetryfoundation.org/poems/44272/the-road-not-taken > https://www.poetryfoundation.org/poems/44272/the-road-not-taken.
Thanks! Wish you all and myself luck 🙂
Oh, and also by the way, clj-kondo
is just absolutely amazing to use in Emacs. No need for some Intellij IDEA or something.

how do I ran a single test in a rich comment/in the repl. I have something that looks as follows that is not working
(comment
(run-tests
(deftest test-smart-search-form-fields
(testing "If other fields list is truthy(not nil) then show dropdown"
(re-frame-test/run-test-sync
(js/console.log "does this work?")
(let [other-field-temp (reagent/atom "")
other-fields-focus (reagent/atom false)
other-fields ["one" "two" "three" "four"]]
(with-mounted-component
[smart-search-form-fields @other-field-temp @other-fields-focus other-fields]
(fn [_component]
(js/console.log (.-innerText (js/document.querySelector
"#smart-search-component")))))))))))
I think I found a way
(comment
(deftest test-smart-search-form-fields
(testing "If other fields list is truthy(not nil) then show dropdown"
(re-frame-test/run-test-sync
(js/console.log "does this work?")
(let [other-field-temp (reagent/atom "")
other-fields-focus (reagent/atom false)
other-fields ["one" "two" "three" "four"]]
(with-mounted-component
[smart-search-form-fields other-field-temp other-fields-focus other-fields]
(fn [_component]
(js/console.log (.-innerText (js/document.querySelector
"#smart-search-component")))))))))
(run-tests))
It looks like it's clojurescript. I'd recommend posting this to the clojurescript channel instead 🙂
yeah.. it is... I figured clojure.test and cljs.test APIs are the same... will post in the correct channel next time though
I may not be understanding the question, but once you've run the deftest block, then you can invoke your test as (test-smart-search-form-fields)
... they're just no-argument functions.
Assuming I need to log requests through a web handler, and I'd rather log the request and response together and implement this as a middleware, where would you put it considering it can error out at multiple phases?
All of that is done by some middleware in the chain, right. I think this request logging middleware should be applied very early in request processing, perhaps even be the very first one
Put the interceptor at the front and log in both the :leave and the :error fns. They can in principle even be the same exact function. http://pedestal.io/reference/error-handling
It can be done in with middleware, too, just need to handle it correctly, main concern is losing data before content negotiation
I would like to be able to test whether stdout is in a full-fledged terminal, or is instead a vim buffer when using vim-iced. This affects how I want to display diagnostic output. Any suggestions? (I'm asking in the general clojure channel because I don't think the answer will be specific to vim-iced or even vim.)
I don't know if there is a solution. I would look at class of *out*
variable that holds reference to output writer. I've just tested on my machine that *out*
in repl is instance of OutputStreamWriter
but when I run it using nrepl tooling (in your case vim-iced) it is instance of PrintWriter
. The writer for nrepl is different because server rebinds *out*
to some temporary writer in order to capture output and send to the nrepl client. I suspect that you could hack something around this
Thanks @U076FM90B. That's a good idea. I just checked, and they are both java.io.Printwriter
in my case. But there may be some other indirect way to test--I'll think about it. I normally use Leiningen, which normally starts nrepl. vim-iced also uses nrepl. So I think that's why both the terminal repl and vim-iced use java.io.Printwriter
. I see that when I start clj it uses java.io.OutputStreamWriter
. So a workaround might be to use clj instead, which could be a good idea anyway (though I still haven't got a deps.edn that does what I want). Or maybe I can configure nrepl in a special way so that I can tell the difference?
I don't have more ideas how to hack it 🙂 Would it be hard to reorganise code such that you could run simpler output from vim tools and complex from terminal? One another hack would be to define dynamic var with a flag that you want simple vs complex output and then bind it only for vim evaluation`(binding [your-flag-to-simplify-printing true] (some-code-that-prints))`
I think I have a workaround. (System/getenv "TERM")
returns "xterm-256color" in my terminal. I can change that string, but if I connect vim-iced to my running nrepl, it will send (System/getenv "TERM")
to that nrepl and get back whatever value TERM
has there. However, I can force vim-iced to start its own nrepl, in which case TERM
= "dumb".
I haven’t tested this but you could check (System/console). In theory it will be nil if output is being redirected
@U076FM90B, yes, that's not a bad idea at all. I try to avoid dynamic variables, but this is a situation in which it would be convenient and reasonable to use.
Thanks @U03HTB6V19B. (System/console)
is returning nil in all cases involving nrepl, i.e. all of the ways I'm used to working. In clj there's a non-nil object returned.
Anyone have a recommendation on a JVM library for validating JSON against a JSON schema? Lot of Java options out there, wondering if anyone has testimonials that can narrow the field for me.
Malli can generate JSON schema but parsing JSON schema -> malli is not supported yet. It’s on the backlog though.
With its current feature set, Malli doesn't really fit my needs. JSON Schema is my starting point, not something I'm trying to produce.