On Wed, Dec 16, 2009 at 1:33 PM, samppi <[email protected]> wrote:
> I'm using a vector as a stack. I want to apply a function–let's call
> it modify-element, one argument—to the elements from k to (- (count a-
> vector) k).
>
> Right now, I have something like (not tested yet):
> (reduce #(update-in %1 [%2] modify-element) a-vector (range k (-
> (count a-vector) k)))
>
> Is there a better way?
Not sure it's better, but I find this more readable:
(defn stack-apply [n f stack]
(if (zero? n)
stack
(conj (stack-apply (dec n) f (pop stack))
(f (last stack)))))
user> (let [stack [1 2 3 4 5]
f #(* 100 %)]
(stack-apply 3 f stack))
[1 2 300 400 500]
Best,
Graham
>
> --
> 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
--
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