Clojure

Add starts-with? ends-with? contains? to clojure.string

Details

  • Type: Enhancement Enhancement
  • Status: Open Open
  • Priority: Minor Minor
  • Resolution: Unresolved
  • Affects Version/s: Release 1.6
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code
  • Approval:
    Triaged

Description

Add clojure.string/starts-with? ends-with? and contains?, similar to java.lang.String's startsWith/endsWith/contains. In addition to making these easier to find and use, this provides a place to add a portable ClojureScript variant.

Patch: clj-1449-more-v1.patch (draft version only – a more serious contender would incorporate Alex Miller's comments from Dec 2 2014)

Activity

Hide
Alex Miller added a comment -

Re substring, there is a clojure.core/subs for this (predates the string ns I believe).

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.

Show
Alex Miller added a comment - Re substring, there is a clojure.core/subs for this (predates the string ns I believe). 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.
Hide
Jozef Wagner added a comment -

As strings are collection of characters, you can use Clojure's sequence facilities to achieve such functionality:

user=> (= (first "asdf") \a)
true
user=> (= (last "asdf") \a)
false
Show
Jozef Wagner added a comment - As strings are collection of characters, you can use Clojure's sequence facilities to achieve such functionality:
user=> (= (first "asdf") \a)
true
user=> (= (last "asdf") \a)
false
Hide
Alex Miller added a comment -

Jozef, String.startsWith() checks for a prefix string, not just a prefix char.

Show
Alex Miller added a comment - Jozef, String.startsWith() checks for a prefix string, not just a prefix char.
Hide
Bozhidar Batsov added a comment -

Re substring, I know about subs, but it seems very odd that it's not in the string ns. After all most people will likely look for string-related functionality in clojure.string. I think it'd be best if `subs` was added to clojure.string and clojure.core/subs was deprecated.

Show
Bozhidar Batsov added a comment - Re substring, I know about subs, but it seems very odd that it's not in the string ns. After all most people will likely look for string-related functionality in clojure.string. I think it'd be best if `subs` was added to clojure.string and clojure.core/subs was deprecated.
Hide
Pierre Masci added a comment -

Hi, I was thinking the same about starts-with and .ends-with, as well as (.indexOf s "c") and (.lastIndexOf "c").

I read the whole Java String API recently, and these 4 functions seem to be the only ones that don't have an equivalent in Clojure.
It would be nice to have them.

Andy Fingerhut who maintains the Clojure Cheatsheet told me: "I maintain the cheatsheet, and I put .indexOf and .lastIndexOf on there since they are probably the most common thing I saw asked about that is in the Java API but not the Clojure API, for strings."
Which shows that there is a demand.

Because Clojure is being hosted on several platforms, and might be hosted on more in the future, I think these functions should be part of the de-facto ecosystem.

Show
Pierre Masci added a comment - Hi, I was thinking the same about starts-with and .ends-with, as well as (.indexOf s "c") and (.lastIndexOf "c"). I read the whole Java String API recently, and these 4 functions seem to be the only ones that don't have an equivalent in Clojure. It would be nice to have them. Andy Fingerhut who maintains the Clojure Cheatsheet told me: "I maintain the cheatsheet, and I put .indexOf and .lastIndexOf on there since they are probably the most common thing I saw asked about that is in the Java API but not the Clojure API, for strings." Which shows that there is a demand. Because Clojure is being hosted on several platforms, and might be hosted on more in the future, I think these functions should be part of the de-facto ecosystem.
Hide
Andy Fingerhut added a comment -

Updating summary line and description to add contains? as well. I can back this off if it changes your mind about triaging it, Alex.

Show
Andy Fingerhut added a comment - Updating summary line and description to add contains? as well. I can back this off if it changes your mind about triaging it, Alex.
Hide
Andy Fingerhut added a comment -

Patch clj-1449-basic-v1.patch dated Aug 30 2014 adds starts-with? ends-with? contains? functions to clojure.string.

Patch clj-1449-more-v1.patch is the same, except it also replaces several Java method calls with calls to these Clojure functions.

Show
Andy Fingerhut added a comment - Patch clj-1449-basic-v1.patch dated Aug 30 2014 adds starts-with? ends-with? contains? functions to clojure.string. Patch clj-1449-more-v1.patch is the same, except it also replaces several Java method calls with calls to these Clojure functions.
Hide
Andy Fingerhut added a comment -

Patch clj-1449-basic-v1.patch dated Sep 5 2014 is identical to the patch I added recently called clj-1149-basic-v1.patch. It is simply renamed without the typo'd ticket number in the file name.

Show
Andy Fingerhut added a comment - Patch clj-1449-basic-v1.patch dated Sep 5 2014 is identical to the patch I added recently called clj-1149-basic-v1.patch. It is simply renamed without the typo'd ticket number in the file name.
Hide
Yehonathan Sharvit added a comment -

What about an implementation that works also in cljs?

Show
Yehonathan Sharvit added a comment - What about an implementation that works also in cljs?
Hide
Bozhidar Batsov added a comment -

Once this is added to Clojure it will be implemented in ClojureScript as well.

Show
Bozhidar Batsov added a comment - Once this is added to Clojure it will be implemented in ClojureScript as well.
Hide
Yehonathan Sharvit added a comment - - edited

Great! Any idea when it will be added to Clojure?
Also, will it be automatically added to Clojurescript or someone will have to write a particular code for it.
The suggested patch relies on Java so I am curious to understand who is going to port the patch to cljs.

Show
Yehonathan Sharvit added a comment - - edited Great! Any idea when it will be added to Clojure? Also, will it be automatically added to Clojurescript or someone will have to write a particular code for it. The suggested patch relies on Java so I am curious to understand who is going to port the patch to cljs.
Hide
Bozhidar Batsov added a comment -

No idea when/if this will get merged. Upvote the ticket to improve the odds of this happening sooner.
Someone on the ClojureScript team will have to implement this in terms of JavaScript.

Show
Bozhidar Batsov added a comment - No idea when/if this will get merged. Upvote the ticket to improve the odds of this happening sooner. Someone on the ClojureScript team will have to implement this in terms of JavaScript.
Hide
Alex Miller added a comment - - edited

Some things that would be helpful:

1) It would be better to combine the two patches into a single patch - I think changing current uses into new users is a good thing to include. Also, please keep track of the "current" patch in the description.
2) Patch needs tests.
3) Per the instructions at the top of the clojure.string ns (and the rest of the functions), the majority of these functions are implemented to take the broader CharSequence interface. Similar to those implementations, you will need to provide a CharSequence implementation while also calling into the String functions when you actually have a String.
4) Consider return type hints - I'm not sure they're necessary here, but I would examine bytecode for typical calling situations to see if it would be helpful.
5) Check performance implications of the new versions vs the old with a good tool (like criterium). You've put an additional var invocation and (soon) type check in the calling path for these functions. I think providing a portable target is worth a small cost, but it would be good to know what the cost actually is.

I don't expect we will look at this until after 1.7 is released.

Show
Alex Miller added a comment - - edited Some things that would be helpful: 1) It would be better to combine the two patches into a single patch - I think changing current uses into new users is a good thing to include. Also, please keep track of the "current" patch in the description. 2) Patch needs tests. 3) Per the instructions at the top of the clojure.string ns (and the rest of the functions), the majority of these functions are implemented to take the broader CharSequence interface. Similar to those implementations, you will need to provide a CharSequence implementation while also calling into the String functions when you actually have a String. 4) Consider return type hints - I'm not sure they're necessary here, but I would examine bytecode for typical calling situations to see if it would be helpful. 5) Check performance implications of the new versions vs the old with a good tool (like criterium). You've put an additional var invocation and (soon) type check in the calling path for these functions. I think providing a portable target is worth a small cost, but it would be good to know what the cost actually is. I don't expect we will look at this until after 1.7 is released.
Hide
Andy Fingerhut added a comment -

Alex, all your comments make sense.

If you think a ready-and-waiting patch that does those things would improve the odds of the ticket being vetted by Rich, please let us know.

My guess is that his decision will be based upon the description, not any proposed patches. If that is your belief also, I'll wait until he makes that decision before working on a patch. Of course, any other contributor is welcome to work on one if they like.

Show
Andy Fingerhut added a comment - Alex, all your comments make sense. If you think a ready-and-waiting patch that does those things would improve the odds of the ticket being vetted by Rich, please let us know. My guess is that his decision will be based upon the description, not any proposed patches. If that is your belief also, I'll wait until he makes that decision before working on a patch. Of course, any other contributor is welcome to work on one if they like.
Hide
Alex Miller added a comment -

Well nothing is certain of course, but I keep a special report of things I've "screened" prior to vetting that makes possible moving something straight from Triaged all the way through into Screened/Ok when Rich is able to look at them. This is a good candidate if things were in pristine condition.

That said, I don't know whether Rich will approve it or not, so it's up to you. I think the argument for portability is a strong one and complements the feature expression.

Show
Alex Miller added a comment - Well nothing is certain of course, but I keep a special report of things I've "screened" prior to vetting that makes possible moving something straight from Triaged all the way through into Screened/Ok when Rich is able to look at them. This is a good candidate if things were in pristine condition. That said, I don't know whether Rich will approve it or not, so it's up to you. I think the argument for portability is a strong one and complements the feature expression.

People

Vote (24)
Watch (2)

Dates

  • Created:
    Updated: