Another drawback of are
is the forbidden de-structuring in the args list. During the few hours I have used it (in lieu of are
), I have become more comfortable with doseq
I also like the natural flow of listing what I need (params + data), and as I type these the testing code foments..
So, maybe the wrong thing to do, but why doesn’t
(case (class "lol") String "yay")
Work (it throws “No matching clause: class java.lang.String”)
And, can I do this sort of thing with case
or would I need to use cond
?I would guess that’s because case
evaluates String
as the literal symbol String
, but it would work with the full symbol, java.lang.String
Nope, doesn’t help with the full symbol 😕
@slipset (case (symbol (.getName (class "lol"))) java.lang.String 1)
;;=> 1
Ah right, because class
returns the actual class but case doesn’t resolve the symbol to the class
Hmm, then there is some trickery involved somewhere since:
user> (defmulti lol type)
;; => #'user/lol
user> (defmethod lol String [s] (println "BLA"))
;; => #multifn[lol 0x244e1571]
user> (lol "yay")
BLA
;; => nil
user>
So for case
String is not good enough, but it is good enough for defmulti
@slipset You should read the case matches as if they are quoted values
roughly: (condp = .... 'String ...)
(reason I came over this was that I was converting a defmulti
to a case
.)
This is a major hack you should probably not use:
(case (class "lol") #=java.lang.String 1)
1
Continuing:
user> (defmethod lol Number [s] (println "Number" s))
user> (lol 1)
Number 1
but
user> (condp = (type 1) Number "1")
Execution error (IllegalArgumentException) at user/eval457938 (form-init10937619012906002413.clj:17848).
No matching clause: class java.lang.Long
user>
yeah, defmethods work with hierarchies, a Long isa?
Number, but when you compare the exact types that won't work
a cond with predicates will though, number?
?
ended up with cond
and isa?
Case match values must be compile time constants. Classes are not compile time constants.
You can use class name though in this case
you are now banned from hashtag equals club
actually ended up with cond
and number?
, string?
etc
Is there a support group for those who are banned?
Why is it a hack though?
Hmm, *read-eval*
exists which makes that code prone to be broken.
I recently picked up 97 Things Every Programmer Should Know
. I got very excited when I realised one of the 97 chapters are authored by @alexmiller (alongside other giants such as Scott Meyers). Well done, and thanks!
ha, that was a long time ago
🙂 nevertheless great read!
Hi, I am looking for the oneliner that every number in a list is smaller then the number before so [5 4 3 2]
would return true and [5 4 8 2]
returns false. Any ideas?
(not (some (fn [[f s]] (< f s)) (partition 2 1 target-coll)))
@slipset cond/condp and instance? might be quite decent too if you don't care about clj hierarchies
(apply > [5 4 3 2])
@sveri
magic!
Thanks @borkdude I know there was a function 🙂
well that's what you're getting with the predicate checks he ended up with
yes, it's only useful if you need to check something else than string/numbers and other "?" fns
@sveri beware of empty collections, they will crash
yea, no problem, thank you.
Anyone get lein test error output integrated with github annotations? Sounds like I want to override clojure.test/report
with output like: https://github.com/clj-kondo/clj-kondo/blob/master/doc/ci-integration.md#linter-output-integration. I wouldn't be surprised if this already works or a lib exists.
I have a function that returns a core.async channel that will only contain one item. I want to map over a collection and get all the results back in a collection. Any ideas?
What do you mean? Is the collection you want to map over the one item in the channel?
async/merge or async/into
The collection has different parameters. The function downloads data based on the parameters. I want to sequentially make the async download requests. So wait for each result before making the next request.
I think (map (comp <!! my-fn) my-coll)
would work for that? (keep in mind it would be lazy by default so you'd need doall
or mapv
)
This brings me the error <! Was used outside go block.
you need <!!
not <!
(println "init results: " (map (comp status-e <! exec-sync) ops-sniffer)) #_(let [r (<! (client/exec-async! state {:op "describe"}))] (println "describe result: " (status-e r)) (print-eval-result r)) #_(let [r (<! (client/exec-async! state {:op "eval" :code "(require '[pinkgorilla.nrepl.sniffer.middleware])"}))] (println "require middleware result: " (status-e r)) #_(print-eval-result r))
the 2 commented out blocks are what I want to do with the map above
<!!
should work there.
that works!
Why is that?
My other code used only <!
<!
only works in a go block. You can't wrap the whole thing in a go
and then use <!
because you can't use <!
in anonymous functions even in a go block. You can accomplish it in a go block if you loop
/`recur` across the inputs/build up an output
(go
(loop [in op-sniffer
out []]
(when-some [x (first in)]
(recur
(rest in)
(conj out (<! (exec-sync x)))))))
this should work with <! or <!! (the go block not being necessary if you use <!!. <!! ties up a real thread while <! in a go does notAhhh
The function part is the Problem
I guess clojure cannot pass the go block into a function.
This is where the code Analysis fails then.
https://clojure.org/guides/core_async_go#_unsupported_constructs_and_other_limitations_in_go_blocks
Thank you so much @jjttjj
Googling for issues with core async is really hard
Bad naming
Too many irrelevant results in google.