On Tue, Aug 17, 2010 at 9:30 AM, Rising_Phorce <[email protected]> wrote:
> Hi All:
>
> First of all thanks for the replies to my last post the clojure
> community is great!
>
> I've been trying to find a succinct way to do the following. Given
> two sequences, product a new sequence which takes items from the first
> sequence unless null in which case it will take an item from the
> second, until the first sequence is exhausted. If nil is encountered
> in the first sequence and the second sequence is exhaused, nil will be
> returned:
>
> e.g. user> (nil-coalesce (nil 1 2 3 nil nil) (4 5))
> (4 1 2 3 5 nil)
Clojure golf is the most fun golf!
(defn nil-coalesce [a b]
(map #(or %1 %2) a (concat b (repeat nil))))
Or if you really want to treat nil and false differently:
(defn nil-coalesce [a b]
(map #(if (nil? %1) %2 %1) a (concat b (repeat nil))))
--Chouser
http://joyofclojure.com/
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en