Fork me on GitHub
#clojure
<
2021-02-19
>
slipset13:02:11

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?

alexmiller13:02:20

Case match values must be compile time constants. Classes are not compile time constants.

alexmiller13:02:52

You can use class name though in this case

jkxyz13:02:25

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

slipset13:02:12

Nope, doesn’t help with the full symbol 😕

borkdude13:02:58

@slipset (case (symbol (.getName (class "lol"))) java.lang.String 1) ;;=> 1

jkxyz13:02:02

Ah right, because class returns the actual class but case doesn’t resolve the symbol to the class

slipset13:02:47

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> 

slipset13:02:15

So for case String is not good enough, but it is good enough for defmulti

borkdude13:02:37

@slipset You should read the case matches as if they are quoted values

borkdude13:02:15

roughly: (condp = .... 'String ...)

slipset13:02:17

(reason I came over this was that I was converting a defmulti to a case.)

borkdude13:02:51

This is a major hack you should probably not use:

(case (class "lol") #=java.lang.String 1)
1

vlaaad13:02:27

you are now banned from hashtag equals club

borkdude13:02:41

Is there a support group for those who are banned?

p-himik13:02:47

Why is it a hack though?

p-himik13:02:05

Hmm, *read-eval* exists which makes that code prone to be broken.

slipset13:02:47

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> 

borkdude13:02:35

yeah, defmethods work with hierarchies, a Long isa? Number, but when you compare the exact types that won't work

borkdude13:02:14

a cond with predicates will though, number? ?

slipset13:02:52

ended up with cond and isa?

slipset13:02:39

actually ended up with cond and number? , string? etc

flefik14:02:13

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!

alexmiller14:02:27

ha, that was a long time ago

flefik14:02:09

🙂 nevertheless great read!

sveri14:02:01

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?

KJO12:02:37

(not (some (fn [[f s]] (< f s)) (partition 2 1 target-coll)))

mpenet14:02:45

@slipset cond/condp and instance? might be quite decent too if you don't care about clj hierarchies

alexmiller14:02:38

well that's what you're getting with the predicate checks he ended up with

mpenet14:02:46

yes, it's only useful if you need to check something else than string/numbers and other "?" fns

borkdude14:02:53

(apply > [5 4 3 2]) @sveri

👍 1
👏 2
borkdude14:02:48

@sveri beware of empty collections, they will crash

sveri15:02:45

yea, no problem, thank you.

sveri14:02:41

Thanks @borkdude I know there was a function 🙂

deadghost19:02:32

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.

awb9923:02:51

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?

jjttjj23:02:19

What do you mean? Is the collection you want to map over the one item in the channel?

hiredman23:02:29

async/merge or async/into

awb9923:02:50

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.

jjttjj23:02:51

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)

awb9923:02:10

This brings me the error <! Was used outside go block.

jjttjj23:02:26

you need <!! not <!

awb9923:02:26

(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))

awb9923:02:00

the 2 commented out blocks are what I want to do with the map above

jjttjj23:02:56

<!! should work there.

awb9923:02:53

that works!

awb9923:02:00

Why is that?

awb9923:02:13

My other code used only <!

jjttjj23:02:06

<! 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

jjttjj23:02:56

(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

awb9923:02:48

The function part is the Problem

awb9923:02:27

I guess clojure cannot pass the go block into a function.

awb9923:02:54

This is where the code Analysis fails then.

awb9923:02:29

Thank you so much @U064UGEUQ

👍 2
😍 1
awb9923:02:42

Googling for issues with core async is really hard

awb9923:02:46

Bad naming

awb9923:02:52

Too many irrelevant results in google.

awb9900:02:15

Many thanks