Hi,

Am 29.01.2010 um 16:25 schrieb Wilson MacGyver:

> I saw this on twitter.
> 
> http://pastie.org/796264
> 
> The problem: given a list of strings, produce a list where sequential
> non-empty strings are concatenated
> 
> # Conditions: Do not use iteration.
> #
> # Example: glom (["a", "b", "c"]) = ["abc"]
> #          glom (["a", "", "b"]) = ["a", "b"]
> #          glom (["a", "b", "", "c"]) = ["ab", "c"]
> #          glom (["a", "b", "", "", "c"]) = ["ab", "c"]
> 
> 
> The author had rules like, don't use iteration and avoid recursion.
> 
> anyone have any clever solutions? My first reaction was to use reduce, but
> I'm not sure how that'd work.

I don't know the definition of iteration, but for my (naive, hobbyist) 
understanding you won't get around it. Recursion is just another form. A map 
call is just another form. A reduce call is just another form. You have to walk 
the sequence in one way or the other and I would call that „iteration“. But I 
called the shot already in the sieve thread. So I will shut up now. Please 
enlighten me if this is totally wrong.

Here a recursive definition.

(defn glom
  [coll]
  (lazy-seq
    (when-let [s (seq coll)]
      (let [[items tail] (split-with (complement empty?) s)
            tail         (drop-while empty? tail)]
        (cons (apply str items) (glom tail))))))

Sincerely
Meikel

-- 
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