(defn fill [coll subs]
  (lazy-seq
    (when-let [[x & nx :as xs] (seq coll)]
      (if-let [[y & ny :as ys] (seq subs)]
        (if (nil? x)
          (cons y (fill nx ny))
          (cons x (fill nx ys)))
        xs))))

On Aug 17, 6:30 am, Rising_Phorce <josh.fe...@gmail.com> 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)
>
> I ended up with a scheme like natural recursion.  I see two
> drawbacks.  The result (presumably empty) must be passed in and the
> recursion will stack overflow for large sequences.  I guess trampoline
> would fix this, but that seems a bit of a dirty trick and workaround
> until Java 7 gives us proper tail calls.  Anyway I looked at partition
> and interleave but without a predicate, I don't see how to make either
> of these work.  Anyone have a more elegant solution?
>
> (defn nil-coalesce [result maybe-nil replacements]
>             (if (empty? maybe-nil)
>                 (if (empty? replacements)
>                   result
>                   nil)
>                 (let [x (first maybe-nil) xs (rest maybe-nil)]
>                 (if (nil? x)
>                     (recur (conj result (first replacements)) xs (rest
> replacements))
>                     (recur (conj result x) xs replacements)))))

-- 
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
Note that posts from new members are moderated - please be patient with your 
first post.
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