On Sun, Mar 1, 2009 at 10:13 PM, Phil Hagelberg <p...@hagelb.org> wrote:
>
> Phil Hagelberg <p...@hagelb.org> writes:
>
>> It's a pretty common idiom in other languages for substring functions
>> to count from the end if given a negative index.
>>
>>   (substring "hello world!" 6 -1) ;; => "world"
>>
>> I'd be glad if Clojure's "subs" function could work like this. Should
>> I create an issue and patch to implement it?
>
> Haven't heard any response on this positive or negative.
>
> Here's the new function definition:
>
> (defn subs
>  "Returns the substring of s beginning at start inclusive, and ending
>  at end (defaults to length of string), exclusive. Negative arguments
>  count from the end."
>  ([#^String s start] (subs s start (count s)))
>  ([#^String s start end]
>     (let [count-back #(if (< 0 %) (+ (count s) %) %)]
>       (.substring s (count-back start) (count-back end)))))
>
> Am I the only one who finds this useful? Perhaps if it's not suited for
> core it could go in str-utils in contrib as "substring".

Makes sense to me, but there's a bug in your function.  You want (< %
0) instead of (< 0 %).

-- 
Michael Wood <esiot...@gmail.com>

--~--~---------~--~----~------------~-------~--~----~
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
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