Fork me on GitHub
#clj-kondo
<
2021-03-15
>
ray10:03:42

I had a bug a little while ago and not sure if it's something that kondo could detect / warn

ray10:03:48

(defn any-evens?
  [coll]
  (reduce (fn [_ x]
            (when (even? x)
              (reduced true))) false coll))

ray10:03:19

(any-evens? [2 2])
=> true
(any-evens? [3 3 5])
=> nil

ray10:03:31

I wanted false in the second case

ray10:03:52

(defn any-evens?
  [coll]
  (reduce (fn [_ x]
            (if (even? x)
              (reduced true)
              false)) false coll))

ray10:03:20

gives the correct results ... I know nil is falsey but still

borkdude10:03:25

Not sure how kondo could help here (in general), as returning nil is valid in reduce functions.

ray10:03:51

(defn any-evens?
  [coll]
  (reduce (fn [_ x]
            (even? x)) false coll))

ray10:03:06

this is also correct, though not as efficient

borkdude10:03:04

Why not (some even? ....) ?

ray10:03:04

I'm trying to check whether using reduced and not having another part is flaggable

ray10:03:45

sure, it was a lot more complex in reality so I'm showing a trivial example ... I want to get to the fact that incorrect use of reduced ... forgetting to assign the result to the accumulator was actually the issue

ray10:03:06

maybe I need a better example