On Sunday 14 December 2008 11:08, Randall R Schulz wrote:
> Hi,
>
> I ran into a special case that I'd like to avoid. When (filter ...)
> is either applied to an empty collection or the filter predicate
> returns false for all the elements in the supplied collection, filter
> returns nil.
>
> Would it be acceptable and desirable for it to return and empty list
> in this case?

To make the case concrete and possibly get a suggestion of a better way
to deal with the situation I encountered, here the function that ran
into the "nil return from (filter ...)" condition. It uses (flatten ...) from
clojure.contrib.seq-utils:

This was my original function. It return [nil] when no matching slots
are found:

(defn- gather-slot-values
 "Extract all the values paired with the specified key;
  returns a vector of flattened values"
 [slot-key c-pairs]
 (into [] (flatten (map #(second %) (filter #(= (first %) slot-key) c-pairs))))
)

Here's how I modified it so it returns [] in that case:

(defn- gather-slot-values
 "Extract all the values paired with the specified key;
  returns a vector of flattened values"
 [slot-key c-pairs]
 (let [filtered (filter #(= (first %) slot-key) c-pairs)]
  (if filtered
   (into [] (flatten (map #(second %) filtered)))
   []))
)

--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"Clojure" group.
To post to this group, send email to clojure@googlegroups.com
To unsubscribe from this group, send email to 
clojure+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to