Basically I want to apply f on x for n times and I am only interested in the result. Is there any better way than (nth (iterate f x) n)
?
as the commenters said, your solution is already ok.
another option is to use comp
, like this maybe:
user> (defn n-times [n f]
(apply comp (repeat n f)))
#'user/n-times
user> ((n-times 10 inc) 1)
;;=> 11
Still I can't really say if it is better then yours in any aspect. Maybe a bit more functional[ish]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With