This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-10-22
Channels
- # announcements (9)
- # asami (52)
- # aws (1)
- # babashka (7)
- # babashka-sci-dev (12)
- # beginners (72)
- # calva (24)
- # cider (9)
- # clj-kondo (76)
- # cljs-dev (15)
- # clojure (19)
- # clojure-australia (4)
- # clojure-europe (33)
- # clojure-france (9)
- # clojure-gamedev (17)
- # clojure-nl (6)
- # clojure-portugal (5)
- # clojure-uk (5)
- # clojurescript (61)
- # clojureverse-ops (4)
- # code-reviews (23)
- # conjure (1)
- # data-science (2)
- # datalevin (6)
- # datomic (49)
- # gratitude (1)
- # helix (24)
- # holy-lambda (14)
- # jobs (3)
- # lsp (92)
- # malli (7)
- # missionary (8)
- # pathom (12)
- # proletarian (3)
- # re-frame (4)
- # remote-jobs (1)
- # shadow-cljs (4)
- # spacemacs (3)
- # sql (9)
- # tools-build (90)
- # vim (1)
- # xtdb (11)
I need to process a finite vector as a repeating sequence and start at an arbitrary point in the sequence.
(defonce notes [:a :bb :b :c :db :d :eb :e :f :gb :g :ab])
In this case I need to start at an arbitrary spot in the vector and then get the next 24 items where the end of the list would loop back to the beginning (thus why I was thinking a sequence would be ideal).
I went with the following for now:
(defonce notes-seq (cycle notes))
(map f (take 24 notes-seq))
(drop (rand-int (count notes)) notes)
Is there a way to short circuit from a let? Let's say I'm doing something like this
(let [parsed-data (parse data)
final-result (do-stuff result)])
If the parsing doesn't work correctly I want to abort and return a different value, but I don't want to throw an exception. Is there another way to do it?If it throws an exception, you can wrap it in a try
block. If it signals error by returning a special value, you can check for that explicitly and then decide the course of action.
yeah has Fredrik says, if it's throwing out an exception you can catch the offending thing with try
, if it's just returning nil you can use or
e.g:
(let [parsed-data (try (parse data) (catch Exception e "default"))
final-result (do-stuff result)])
;;or
(let [parsed-data (or (parse data) "default")
final-result (do-stuff result)])
it will return a spec error like :
:clojure.spec.alpha/invalid
that or
function might be usefulBut maybe I wasn't clear. I want to exit from the let if the parse function returns invalid
You could try something like
(let [parsed-data (parse data)]
(when-not (= parsed-data :clojure.spec.alpha/invalid)
DO-STUFF))
(def early-return ::early-return)
(defmacro early-returnable-let [bindings body]
(loop [form body
[first & rest] bindings]
(if first
(let [[k v] first]
(recur `(let* [~k ~v]
(when (not= early-return ~k)
~body))
rest))
body)))
Inst already refers to: #'clojure.core/Inst in namespace: schema.core, being replaced by: #'schema.core/Inst does anyone has any idea about this error ?
Hi, i cant find any good source that explains what i should use instead of :refer :all (:require [compojure.api.sweet :refer :all]
what would be a good alternative?
purely as a sytlistic choice I will almost always prefer using :as
( for example: [compojure.api.sweet :as sweet]
) and use sweet/whatever-function because it's more clear that it's not being defined in this namespace.
but yeah :refer all
is rarely a "Good Idea"™️
at least prefer :refer [specific-function1 specific-function2]
@jonas.hinrichs (:require [compojure.api.sweet :as sweet :refer [commonly-used-function]])
works, Thanks!
Greetings everyone, any one in windows use Cursive and connect to nREPL
within WSL
? It works fine if I'm using VSCode(with remote plugin) connect to WSL
but ,I have to move to Cursive to be align with teammates.
I tried to connect but Cursive always failed to connect .
Perhaps this article about WSL 2 is relevant https://dev.to/vishnumohanrk/wsl-port-forwarding-2e22
Thank you! John, it solve my problem and save my day ! one more thing to add is that, in the WSL, user need to bind to 0.0.0.0
explicitly, then IntelliJ from windows can connect to WSL
Having some trouble with this. I think the error is in the notes-from-intervals function, but I'm not sure how to debug it.
(defonce notes [:a :bb :b :c :db :d :eb :e :f :gb :g :ab])
(defonce notes-seq (cycle notes))
(defn notes-start-n [n]
(drop (.indexOf notes n) notes-seq))
(defonce maj7-intervals [0 4 7 11])
; :f [0 4 7 11] => #{ :f :a :c : e }]
(defn notes-from-intervals [n intervals]
(set (map take-nth intervals (notes-start-n n))))
(defonce active-notes (notes-from-intervals :f maj7-intervals))
That doesn't look like idiomatic Clojure. What are you trying to do?
I haven't seen so many defonces in the same ns before 😛 Your inputs might not change for now but they will at some point, right? Try to write functions that do not depend on globals so much. E.g. put all the defonces in a function and be honest about your functions' inputs. If notes-start-n needs to know about notes, make it a parameter. edit: On the phone right now but if I understand what your goal is I could have a go at it when I get back
How do I expand a list of vectors to use as arguments in fn call? My fn looks something like this:
(defn olp->csv->write [accounts]
(with-open [writer (io/writer "olp.csv")]
(csv/write-csv writer [olp->csv->headers (olp->csv->create-rows accounts)])))
Where olp->csv->create-row accounts
returns the list of vectors, each representing a csv rowWhen I look on stackoverflow I see solutions using apply
but I can’t wrap my head around how I’d use that here
I think the easiest option here is to just use cons
to slap the headers on the front
(defn olp->csv->write [accounts]
(with-open [writer (io/writer "olp.csv")]
(csv/write-csv writer (cons olp->csv->headers (olp->csv->create-rows accounts)))))
you lose the vector-ness but all you're doing here is sequential iteration so it really doesn't matter
you could also do things like (concat [olp->csv->headers] ...)
or (into [olp->csv->headers] ...)
etc
for what you're doing though, those are probably worse
the first is effectively the same, but harder to say. the second is going to move all the rows into the first coll, which is not worth doing
Gotchya. Thank you. So is it uncommon to run into situations where you’d need want to spread a collection?
it's fairly common, and that's what apply
is for
it may be confusing how to use it here because you would need to spread into a collection constructor, like:
(apply vector olp->csv->headers (olp->csv->create-rows accounts))
but that's another option
like the into
option above, would mean copying a lot of data to no benefit
What is the rationale behind string/join and string/split putting their "separater" argument in different positions?
in general, the string functions always take the string first (similar to how collection functions take the collection first)
string/join is a special case as the separator is often partial
-ed in
Thanks @alexmiller. I did read somewhere - I think in a Stuart Sierra article - that the "main thing" usually comes as the first argument, and seeing as it's the string namespace, I expect the string arg to come first, but the pragmatism makes sense. It would be nice if there was a resource that listed the common "special cases" for reference.
I think the clojure.string namespace docstring does have some of this in it, not at a computer
How can I map a function to a collection and always pass the same parameter for the second parameter to the function?
If you can change the function and can swap the order of parans then you could use partial
Is there a function that is the equivalent of?
(defn find-first-match [pred coll]
(some #(when (pred %)
%)
coll))
;; usage
> (find-first-match :b [{:a 1} {:b 2} {:c 3}])
;; {:b 2}
If not, what would be a good name for it? I use this idiom at the repl a lot and having a shorthand for it would be really useful.If it doesn’t exist, I want an accurate, short name for it. Being concise is a feature for my use case.
that’s actually pretty good
matches fnext, ffirst, etc
you’re a genius!
fkeep?