when-seq and if-seq as short-hand macros

Description

Per https://ask.clojure.org/index.php/10450/when-seq-and-if-seq-as-short-hand-macros

Add when-seq and if-seq as short-hands for (when-let [x (seq ...)]) and (if-let [x (seq ...)]).

What problem does this solve? It's just a convenience for dealing with functions that either return nil or empty collections.

There are 100-200 instances each of these idiom that can be found on github. I'm not aware of this being implemented or widely used in common utility libraries.

Environment

None

Activity

Show:
Michiel Borkent
April 11, 2021, 2:42 PM
Edited

The reason I needed this was when I created a function that returned a seq, but I also wanted it to be used as a predicate without the user needing to call seq on it in a when-let. If the above macro existed in core I could just recommend to use that and not worry about my return value.

I'm finding about 1700 occurrences in my local .m2 using this script (probably lots of duplicates due to multiple versions of the same lib):

Just make it executable and run from a directory. The script will scan the dir recursively for sources/jars.

Assignee

Unassigned

Reporter

Alex Miller

Approval

Triaged

Priority

Major

Affects versions