Idiom for filtering mixed lists with ints?

(let [mylist ["" "1234" "blue" :key 5 nil]
      filterfn (constantly "How to do this idiomatically, keeping all but the two empty elements?")]
  (filter filterfn mylist))

using empty? fails because of the non-seq list elements. It can easily be done with an anonymous function, but is there really no other idiomatic way?

filterfn can be (some-fn (complement seqable?) seq) since you want to keep everything that is not seqable or is not empty – but I would probably turn it around like this:

  (let [mylist ["" "1234" "blue" :key 5 nil]
    (remove (every-pred seqable? empty?) mylist)))

You want to remove anything that is both seqable and empty.

5 Likes

That’s excellent! I have never used remove, rarely used every-pred, and didn’t know the difference between seq? and seqable? . TIL!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.