I like your version, Roman. It seems as efficient as anything, and is
easy to read. For what it's worth, I'd make a small rewrite:
(defn take-by [f coll]
(lazy-seq
(when-let [[x & xs] (seq coll)]
(let [val (f x)]
(cons x (take-while (comp #{val} f) xs))))))
On Apr 3, 5:45 am, Roman Sykora <[email protected]> wrote:
> 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