Occasionally things like this remind me of the similarities between
parsing character sequences and dealing with unpredictably ordered
collections. For character sequences, the regular expression mechanism
has been invented. I wonder if any one else has ever wished for the
ability to write a regular expression for a seq of typed objects.

(defn sift [character-sequence]
        (lazy-seq (if-let [
                [_ firstsection remainder] (re-find #"(\w\d*)(.*)" (apply str
character-sequence))
                ]
                (cons (list (first firstsection) (rest firstsection))
                        (sift remainder)))))

The analog to a character class would be a function taking an object
and returning true or false for whether it matches. Something like
this might be an equivalent regular expression for the given problem.

(defn is-delimiter [listitem]
        (re-matches #"\w" (str listitem)))
(defn is-data [listitem]
        (re-matches #"\d" (str listitem)))
(defn wildcard [listitem] true)
(def regular-expression (list \( is-delimiter is-data \* \) \
( wildcard \) ))

Make sense?


On Jan 24, 12:58 pm, CuppoJava <patrickli_2...@hotmail.com> wrote:
> It's an elegant puzzle. Thanks Sean!
>
> Here's my take:
> (defn sift [pred? s]
>   (lazy-seq
>     (if (seq s)
>       (let [key (first s)
>             [vals remaining] (split-with #(not (pred? %)) (rest s))]
>         (cons [key vals] (sift pred? remaining))))))
>
> Running:
> (sift string? ["a" 2 "b" 3 4 "c" "d" 4])
>
> Returns:
> (["a" (2)] ["b" (3 4)] ["c" ()] ["d" (4)])
>
> I'm interested in what you came up with. =)
>   -Patrick

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