On Sat, Aug 7, 2010 at 9:36 PM, gary ng <[email protected]> wrote:
> if you don't mind about performance, this seems to be natural to me
>
> user=> (reverse (map reverse (reduce (fn [a e] (if (even? e) (cons [e] a)
> (cons
> (cons e (first a)) (rest a)))) (list) [1 2 3 7 5 4 1])))
> ((1) (2 3 7 5) (4 1))
I reworked that a bit, to parameterize it:
(defn pw [f? x]
(let [phi (fn [a e]
(if (f? e)
(cons [e] a)
(cons (cons e (first a))
(rest a))))]
(reverse
(map reverse
(reduce phi () x)))))
This is in the same family as my version 2, but I think it is much
cleaner. This approach gets rid of the recursion, a definite win.
Thanks, Gary.
--
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