clojure.string/substring

311 views
Skip to first unread message

Pierre-Yves Ritschard

unread,
Feb 26, 2016, 2:47:54 AM2/26/16
to Clojure Dev
Hi list,

Since string operations are making their way to clojure.string to ease writing code for both clojurescript and clojure, would you be open to a patch that would add substring, using a one and two argument arity?

This is a very common need, and while java.lang.String already has a substring method, clojure.string would benefit from it - it would additionaly allow for negative offsets, as commonly found in most high level languages.

If you're open to the idea, I'm happy to open a JIRA and propose a patch for it.

Cheers!

Thomas Heller

unread,
Feb 26, 2016, 4:20:39 AM2/26/16
to Clojure Dev
There already is a substring function in core, so you can just do (subs "123" 1 2).

cljs.core/subs (or clojure.core/subs)
([s start] [s start end])
  Returns the substring of s beginning at start inclusive, and ending
  at end (defaults to length of string), exclusive.

HTH,
/thomas

Pierre-Yves Ritschard

unread,
Feb 26, 2016, 5:15:04 AM2/26/16
to cloju...@googlegroups.com

Can't believe I missed this for the past 6 years :-)
Would then a patch adding support for negative indices be considered
(for both clojure and clojurescript) ?

Andy Fingerhut

unread,
Feb 26, 2016, 7:47:15 AM2/26/16
to cloju...@googlegroups.com
In the past, there have been multiple requests for negative indices in Clojure's core library, and they have all been declined.  This ticket [1] and the links in its comments, provide some background.  I am not the one to approve or disapprove enhancement requests, but unless there has been some significant change in thought here, it seems unlikely such a change would be considered.

If you want them, it is pretty easy to implement your own versions of subs and vec that accept negative args.

Andy


--
You received this message because you are subscribed to the Google Groups "Clojure Dev" group.
To unsubscribe from this group and stop receiving emails from it, send an email to clojure-dev...@googlegroups.com.
To post to this group, send email to cloju...@googlegroups.com.
Visit this group at https://groups.google.com/group/clojure-dev.
For more options, visit https://groups.google.com/d/optout.

Erik Assum

unread,
Feb 26, 2016, 8:03:35 AM2/26/16
to cloju...@googlegroups.com
There is also


Which may or may not contain what you're looking for. 

Erik. 
-- 
i farta

Alex Miller

unread,
Feb 26, 2016, 8:47:14 AM2/26/16
to cloju...@googlegroups.com
There is already clojure.core/subs for this.
--

Alex Miller

unread,
Feb 26, 2016, 9:40:04 AM2/26/16
to Clojure Dev
Sorry on the previous email, didn't realize there was more in the inbox I hadn't read yet. :) 

I think the primary reasons negative indices have been declined is that the host (java.lang.String) doesn't give us a hand in implementing it and that it raises the question of whether negative indices should be supported in other contexts (unlikely). I think there's also a possible disconnect in substring that is start/end based vs start/length based.

ClojureScript didn't exist back then, but I think the issue of portability between Clojure and ClojureScript with host-supported implementations is worth considering, however I'm not sure that's a strong enough argument there to overcome the prior ones.
Reply all
Reply to author
Forward
0 new messages