This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2022-11-02
Channels
- # announcements (2)
- # babashka (10)
- # beginners (61)
- # calva (17)
- # cider (27)
- # clj-kondo (14)
- # clojure (230)
- # clojure-austin (4)
- # clojure-europe (17)
- # clojure-france (6)
- # clojure-hungary (3)
- # clojure-norway (30)
- # clojure-sweden (9)
- # clojure-uk (2)
- # clojurescript (58)
- # conjure (11)
- # core-async (7)
- # cursive (74)
- # datalog (2)
- # datomic (15)
- # events (8)
- # figwheel-main (5)
- # fulcro (2)
- # graalvm (23)
- # graphql (2)
- # helix (17)
- # humbleui (2)
- # jobs (2)
- # kaocha (6)
- # lsp (19)
- # malli (7)
- # nbb (51)
- # off-topic (33)
- # pathom (26)
- # pedestal (2)
- # polylith (1)
- # portal (4)
- # re-frame (17)
- # react (3)
- # reitit (5)
- # releases (2)
- # remote-jobs (2)
- # shadow-cljs (18)
- # sql (65)
- # tools-deps (8)
- # xtdb (28)
Hello friends! ♥️ Form (take-nth 5 (range)) produce lazy sequence 0 5 10 15 20 25 ... Is there some kind of "reverse take-nth" which produce lazy sequence 1 2 3 4 6 7 8 9 11 12 13 14 16 17 ... Thanks!
There's not, but you could make one using lazy-cat:
(defn drop-nth [n coll]
(when-let [s (seq coll)]
(lazy-cat (take (dec n) (rest s)) (drop-nth n (drop n s)))))
:hugging_face:
Also keep-indexed
may be a simpler approach anyway:
(defn drop-nth [n coll]
(keep-indexed #(when (pos? (rem %1 n)) %2) coll))
:star-struck:
(defn drop-nth [nr] (concat (range nr) (drop (inc nr) (range)))) this is what I would do :)
@U0404ASMSCT unfortunately that just drops n. @U03D1PUDM6W is asking how to drop every nth
oh, that‘s true, I read the question wrong :woman-facepalming::skin-tone-2:
But this may work (defn drop-every-nth [nr coll] (remove (set (range 0 (inc (count coll)) 5)) coll))
(where 5
is replaced with nr
)
yes, it works, but it's less performant, especially for small nr
and large coll
:
user> (time (do (doall (drop-every-nth 2 (range 1e7))) nil))
"Elapsed time: 7732.980416 msecs"
nil
user> (time (do (doall (drop-nth 2 (range 1e7))) nil))
"Elapsed time: 4933.956869 msecs"
nil
I would think of it as partitioning and then dropping, maybe something like
(->> (range 30)
(partition-all 5)
(mapcat rest))
In my mind, an advantage of this approach (and the keep-indexed
approach mentioned by @UE1N3HAJH) over the lazy-cat
or concat
version is that it converts very easily to a transducer form, which is useful if you need to refactor later
(into [] (comp (partition-all 5) (mapcat rest)) (range 30)) ;; => [1 2 3 4 6 7 8 9 11 12 13 14 16 17 18 19 21 22 23 24 26 27 28 29]
Hi all, apologies if this is done to death but I’m wondering what would be a recommended HTTP client? So far clj-http seems like a good option (keen to have retries built in)
Thanks, does it have support for retries?
if you want something much more sophisticated there are libs like https://github.com/xsc/claro
http-kit says it's based on clj-http, which has retries. so i'm guessing that kit probably has that too
Where does it say that?
I think clj-http just has retries as it’s based on the Apache HTTP client
kit doesn’t seem to mention retries
you have to look in it's code to be sure. may also want to look at how clj-http does it, cus if it's just a line of code or something you may be putting too much value on that feature
There doesn’t seem to be anything in the kit code for it I can see
that's a lot better, now you have the same retry interface for different things in your app
True, though HTTP requests will be the only thing in this case
Are there any reasons in particular to go with kit over clj-http? Lack of dependencies in the former perhaps but that’s not much of a concern
I certainly like the idea of retries out of the box without having to think about it
I tend to go for clj-http, if only because clj-http-lite often doesn't have features that I want (like proxy support)
http-kit has a focus on concurrent features, probably could be implemented with core.async or manifold without much effort
How can I make a function that takes a collection and a variable number of functions and maps every function to the collection? A sketch of the function:
(defn map-multiple-functions [coll & funcs]
(->> coll
(map func1)
(map func2)
...
(map func-n))
Stylistically, is one solution more readable than the other?
don't you need a reverse in there?
(map (apply comp (reverse funcs)) coll)
oh, maybe, could be I switched the reverse in the first comp and the sequence examples
Considering this:
(let [foo "bar" baz foo] baz)
;; => "bar"
I’d like to construct the let bindings outside of the let, is this possible if it’s self-referential?
e.g.
(let bindings baz)
;; => "bar"
let takes a literal vector - it's a special form that expects that as part of its syntax, so this is not possible regardless. but what are you really wanting to do?
Well - I have a set of configuration variables such as:
(def company_auth_params {
:dev {:code-challenge "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:code_verifier "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:client_id "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:auth-header "Basic XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
:redirect_uri ""
:login_uri ""
:authorize_uri ""
:token_uri ""}
})
and in my function, I have a let
and that let has its own bindings. I though it would be nice to just “combine” the pre-defined params into that let binding so that I could refer to them directly.
you can destructure to get that
oooooh
(let [ [:code-challenge :code-verifier] (company_auth_params :dev) more stuf…]
(let [{:keys [code-challenge verifier client_id]} (:dev company_auth_params)]
...)
aha! Even better, thank you sir this is super helpful. Trying now.
That worked awesome, thanks @U064X3EF3 👍
happy Clojureing!
It has been happy Clojureing indeed 🙂
btw you can learn more about destructuring in the guide https://clojure.org/guides/destructuring