In this particular case, I'd probably treat a nil
value of search
as being "always match" in the filter
call:
(filter (fn [comment] (or (nil? search)
(s/includes? (s/upper-case (:thread comment))
(s/upper-case search))))
comments)
and then you could just have the 0-arity version of the function call the 1-arity version with nil
:
(defn get-threads
"Get all urls threads"
([] (get-threads nil))
([search]
(let [comments ...]
(filter ... comments))))
Another option here would be to use cond->>
:
(cond->> (distinct (map (fn [comment] ...) @db))
search
(filter (fn [comment] (s/includes? ...)))
This will pass the comments into the filter
call, only if search
is non-nil, else it will return all the comments, unfiltered.
Typically, if I have multiple arities due to optional arguments, I will pick an "empty" value for the optional arguments and just have the lower arities call the full arity, passing those "empty" values -- zero, nil
, empty list, etc. Then the full arity version will conditionally handle those "empty" values as appropriate.