How about using the clojure sequence functions?
(require '[clojure.contrib.seq-utils :as seq-utils])

(defn last-index-of [c string]
 (first (seq-utils/find-first (fn [[_ a]] (= a c)) (reverse
(seq-utils/indexed string)))))

P.S. Jong Won, how are you liking Clojure? I've met you in Parramatta
and joined the ADO team :) Nice to have you on the group here :)

Cheers
Andreas

On 13 November 2011 10:19, Tyler Perkins <thinks.outsrst (i...@gmail.com> wrote:
> Interesting. I never knew how to use areduce before. However, it
> always scans the entire array. If you had a very long string (or other
> collection), it might be better to scan backwards:
>
> user> (defn last-indexof [cs c]
>            (loop [n (dec (count cs))]
>               (if (and (<= 0 n) (not= c (nth cs n)))
>                   (recur (dec n))
>                   n)))
> #'user/last-indexof
> user> (last-indexof "aabbccd" \c)
> 5
> user> (last-indexof "aabbccd" \x)
> -1
>
>> areduce can be used too.
>>
>> (defn last-indexof [cs c]
>>   (areduce cs i lst-idx -1
>>     (if (= c (aget cs i)) i lst-idx)))
>>
>> (-> "aabbccd" to-array (last-indexof \c))
>>
>> Regards.
>> Jestan Nirojan
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to clojure@googlegroups.com
> Note that posts from new members are moderated - please be patient with your 
> first post.
> To unsubscribe from this group, send email to
> clojure+unsubscr...@googlegroups.com
> 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 clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to