clojure - ArityException Wrong number of args (0) passed to: core/max -


if run code, error "arityexception wrong number of args (0) passed to: core/max"

(apply max (filter #(zero? (mod % 7)) (range 1 3))) 

however, if run code

(apply max (filter #(zero? (mod % 7)) (range 1 10))) 

then result 7.

is there can me figure out problem?

there no numbers divisible 7 between 1 , 3, result of filter in first example returns empty sequence, means first example if calling (apply max []) same calling (max). max requires @ least 1 parameter, hence arityexception.

a couple of options fix it:

(last (filter #(zero? (mod % 7)) (range 1 3)) 

or

(if-let [by-7 (seq (filter #(zero? (mod % 7)) (range 1 3)))]   (apply max by-7)   nil ;; or whatever value in case collection empty   ) 

Comments