Hi

When I read this post in the morning I thought about how I would
implement this function, and the result was:

(defn take-by
  [f coll]
  (when-let [[x & xs] (seq coll)]
    (let [n (f x)]
      (lazy-seq (cons x (take-while #(= n (f %)) xs))))))

I didn't post it, because I really am a beginner in clojure and
functional programming and others had already given different answers
that were far more 'complicated' than my solution.

Now, my question is what's the improvement of

> (defn take-by [f coll]
>   (let [fs (map f coll)
>          ps (map = fs (rest fs))
>          zs (map #(if %1 %2 sentinel) ps (rest coll))]
>     (cons (first coll) (take-while (partial not= sentinel) zs))))

over

> (defn take-by
>   [f coll]
>   (lazy-seq
>     (when-let [s (seq coll)]
>       (let [fst   (first s)
>             value (f fst)]
>         (cons fst (take-while #(-> % f (= value)) (rest s)))))))

Sincerely
Roman

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

Reply via email to