On Tue, Aug 17, 2010 at 3:56 PM, Rising_Phorce <[email protected]> wrote:
> I posted because clojure *tends* to be so succinct and in this case
> the solution complexity seems disproportionate to the problem. In my
> first post I forgot to mention my second attempt...
>
> (map #(if (nil? %1) %2 %1) [nil 1 2 3 nil nil] [4 5])
>
> but it got ugly because I needed to extend the replacements to the the
> same length as the maybe-nil collection...
>
> (defn nil-coalesce2 [maybe-nil replacements]
> (let [cnt-mn (count maybe-nil)
> cnt-r (count replacements)]
> (if (= cnt-mn cnt-r)
> (map #(if (nil? %1) %2 %1) maybe-nil replacements)
> (map #(if (nil? %1) %2 %1) maybe-nil (concat replacements (repeat
> (- cnt-mn cnt-r) nil))))))
Ok, maybe I understand the task now?
(defn nil-coalesce [a b]
(->> a
(reductions (fn [[_ b] a]
(if (nil? a)
[(first b) (rest b)]
[a b]))
[0 b])
rest
(map first)))
This is much like Jeff Valks solution, but lazy.
--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