This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2021-02-19
Channels
- # announcements (13)
- # asami (43)
- # babashka (35)
- # beginners (175)
- # calva (23)
- # cider (5)
- # clj-kondo (68)
- # cljsrn (4)
- # clojure (61)
- # clojure-australia (7)
- # clojure-europe (20)
- # clojure-gamedev (59)
- # clojure-israel (11)
- # clojure-italy (4)
- # clojure-nl (2)
- # clojure-norway (21)
- # clojure-spec (12)
- # clojure-uk (43)
- # clojurescript (9)
- # cursive (56)
- # data-oriented-programming (5)
- # datascript (1)
- # events (1)
- # fulcro (16)
- # honeysql (46)
- # leiningen (1)
- # malli (4)
- # off-topic (12)
- # pathom (46)
- # re-frame (24)
- # reagent (14)
- # reitit (1)
- # reveal (8)
- # rewrite-clj (16)
- # ring (13)
- # sci (9)
- # spacemacs (14)
- # specter (2)
- # sql (2)
- # tools-deps (1)
- # vim (2)
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
?Case match values must be compile time constants. Classes are not compile time constants.
You can use class name though in this case
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
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>
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
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
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?
@slipset cond/condp and instance? might be quite decent too if you don't care about clj hierarchies
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
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?
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
)
(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))
<!
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 not