Clojure

[PATCH] Allow negative substring indices for (subs)

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: Release 1.5
  • Fix Version/s: None
  • Component/s: None
  • Labels:
  • Patch:
    Code and Test

Description

This adds Python-style negative string indices for (subs), e.g.:

(subs "foo bar" -3) ;; -> "bar"

Activity

Hide
Andy Fingerhut added a comment -

Ian, thanks for the patch. It is Rich Hickey's policy only to consider applying patches to Clojure from those who have signed a Clojure contributor agreement: http://clojure.org/contributing

Were you interested in doing so, or perhaps it is already in progress?

Show
Andy Fingerhut added a comment - Ian, thanks for the patch. It is Rich Hickey's policy only to consider applying patches to Clojure from those who have signed a Clojure contributor agreement: http://clojure.org/contributing Were you interested in doing so, or perhaps it is already in progress?
Hide
Ian Eure added a comment -

I wasn't aware that this was necessary. I'm mailing the form in.

Show
Ian Eure added a comment - I wasn't aware that this was necessary. I'm mailing the form in.
Hide
Andy Fingerhut added a comment -

Patch clj-1042-negative-subs-patch2.txt dated Aug 27 2012 is identical to Ian Eure's negative-subs.patch, except it is in the desired git format.

Ian, for future reference on creating patches in the desired format, see the instructions under the heading "Development" on this page: http://dev.clojure.org/display/design/JIRA+workflow

Show
Andy Fingerhut added a comment - Patch clj-1042-negative-subs-patch2.txt dated Aug 27 2012 is identical to Ian Eure's negative-subs.patch, except it is in the desired git format. Ian, for future reference on creating patches in the desired format, see the instructions under the heading "Development" on this page: http://dev.clojure.org/display/design/JIRA+workflow
Hide
Ian Eure added a comment -

Thanks, will do.

Show
Ian Eure added a comment - Thanks, will do.
Hide
Steve Miner added a comment -

If Clojure decides to support Python-style negative indices, you should also consider adding support to subvec.

Show
Steve Miner added a comment - If Clojure decides to support Python-style negative indices, you should also consider adding support to subvec.
Hide
Ian Eure added a comment -

Patch extended to support negative indices on (subvec) as well.

Show
Ian Eure added a comment - Patch extended to support negative indices on (subvec) as well.
Hide
Adrian Bendel added a comment -

The arg to rindex should probably be tagged with ^clojure.lang.Counted instead of ^String now.

Show
Adrian Bendel added a comment - The arg to rindex should probably be tagged with ^clojure.lang.Counted instead of ^String now.
Hide
Steve Miner added a comment -

Regarding the previous comment, String is a Java class so it isn't a clojure.lang.Counted. Is the type hint necessary? Maybe it should be on the call rather than the defn.

Ignoring the type hinting, I'll suggest a slightly simpler way to implement the rindex logic:

(defn rindex [coll i]
(if (neg? i) (+ (count coll) i) i))

In any case, I'm not sure rindex should be public even if you want the subs and subvec enhancements. Someone needs to make the case for adding a new function to core.

The Pythonic negative index is a debatable feature since it's pretty easy to implement for yourself if you want it.

Show
Steve Miner added a comment - Regarding the previous comment, String is a Java class so it isn't a clojure.lang.Counted. Is the type hint necessary? Maybe it should be on the call rather than the defn. Ignoring the type hinting, I'll suggest a slightly simpler way to implement the rindex logic: (defn rindex [coll i] (if (neg? i) (+ (count coll) i) i)) In any case, I'm not sure rindex should be public even if you want the subs and subvec enhancements. Someone needs to make the case for adding a new function to core. The Pythonic negative index is a debatable feature since it's pretty easy to implement for yourself if you want it.
Hide
Adrian Bendel added a comment - - edited

Sorry, the type hint on rindex args isn't necessary at all. Just looked up in the source, calling count should never be reflective, since (count ..) emits calls to clojure.lang.RT/count.

Your solution looks good.

Show
Adrian Bendel added a comment - - edited Sorry, the type hint on rindex args isn't necessary at all. Just looked up in the source, calling count should never be reflective, since (count ..) emits calls to clojure.lang.RT/count. Your solution looks good.
Hide
Stuart Halloway added a comment -

Negative indices were considered and rejected a long time ago. (I am merely conveying information--I have no strong opinion on this one.)

Show
Stuart Halloway added a comment - Negative indices were considered and rejected a long time ago. (I am merely conveying information--I have no strong opinion on this one.)
Hide
Andy Fingerhut added a comment -

Note: If some people really like negative index behavior as in Perl or Python, it is straightforward to create a library of functions in a different namespace, perhaps with different names, that can do it. Perhaps a "pythonisms" library?

Show
Andy Fingerhut added a comment - Note: If some people really like negative index behavior as in Perl or Python, it is straightforward to create a library of functions in a different namespace, perhaps with different names, that can do it. Perhaps a "pythonisms" library?
Hide
Ian Eure added a comment -

Would this be accepted as part of clojure.string instead? I considered putting it there, but thought it would be confusing to have multiple substring functions in different namespaces.

This is very helpful in practice, and I'd really like to see at least the (subs) stuff in Clojure.

Show
Ian Eure added a comment - Would this be accepted as part of clojure.string instead? I considered putting it there, but thought it would be confusing to have multiple substring functions in different namespaces. This is very helpful in practice, and I'd really like to see at least the (subs) stuff in Clojure.
Hide
Andy Fingerhut added a comment -

Disclaimer: I'm no Clojure/core member, so can't speak authoritatively on whether something would or would not be accepted into clojure.string.

However, given that clojure.string is distributed with clojure.core, my guess is it would not be accepted. You'd be able to get things like this out for you and others as a separate library distributed on Clojars. That would also make it easy to include other Python-like things that you don't find in Clojure already.

Show
Andy Fingerhut added a comment - Disclaimer: I'm no Clojure/core member, so can't speak authoritatively on whether something would or would not be accepted into clojure.string. However, given that clojure.string is distributed with clojure.core, my guess is it would not be accepted. You'd be able to get things like this out for you and others as a separate library distributed on Clojars. That would also make it easy to include other Python-like things that you don't find in Clojure already.
Hide
Ian Eure added a comment -

This isn't about "python-like things," this is about a useful feature. Lots of languages support this: Perl, PHP, Ruby, Python, JavaScript, to name a few. Are you really suggesting that I should create a whole package for a version of a function in clojure.core with slightly different semantics? That's insane.

Anyway, I'm done wasting my time trying to navigate this hopelessly broken process. Maybe it would have been accepted if I included yet another way to interoperate with Java types.

Show
Ian Eure added a comment - This isn't about "python-like things," this is about a useful feature. Lots of languages support this: Perl, PHP, Ruby, Python, JavaScript, to name a few. Are you really suggesting that I should create a whole package for a version of a function in clojure.core with slightly different semantics? That's insane. Anyway, I'm done wasting my time trying to navigate this hopelessly broken process. Maybe it would have been accepted if I included yet another way to interoperate with Java types.
Hide
Michael Klishin added a comment -

Stuart, do you remember why specifically negative indexes were rejected? Developing a separate library for a minor improvement to an existing function sounds unreasonable.

Show
Michael Klishin added a comment - Stuart, do you remember why specifically negative indexes were rejected? Developing a separate library for a minor improvement to an existing function sounds unreasonable.
Hide
Carlos Cunha added a comment -

some explanation about this topic was given by Rich Hickey himself here: http://news.ycombinator.com/item?id=2053908

citation:
"...Yes, there is a backlog from when it was just me, and it will take a while to whittle down. We have finite resources and have to prioritize. I can assure you we have more important things to concentrate on than your negative index substring enhancement, and are doing so. You'll just have to be patient. Or, if you insist, I'll just reject it now because a) IMO it's goofy, b) you can make your own function that works that way c) we don't get a free ride from j.l.String, d) it begs the question of negative indices elsewhere..."

i've been following this thread hoping this feature would be included. but whatever the reason was for the rejection, i'm sure it was thoughtful. great thanks for this wonderful language, and thanks Ian Eure for his effort.

Show
Carlos Cunha added a comment - some explanation about this topic was given by Rich Hickey himself here: http://news.ycombinator.com/item?id=2053908 citation: "...Yes, there is a backlog from when it was just me, and it will take a while to whittle down. We have finite resources and have to prioritize. I can assure you we have more important things to concentrate on than your negative index substring enhancement, and are doing so. You'll just have to be patient. Or, if you insist, I'll just reject it now because a) IMO it's goofy, b) you can make your own function that works that way c) we don't get a free ride from j.l.String, d) it begs the question of negative indices elsewhere..." i've been following this thread hoping this feature would be included. but whatever the reason was for the rejection, i'm sure it was thoughtful. great thanks for this wonderful language, and thanks Ian Eure for his effort.
Hide
Steve Miner added a comment -

That HN link eventually leads back to CLJ-688 which was rejected.

Show
Steve Miner added a comment - That HN link eventually leads back to CLJ-688 which was rejected.
Hide
Stuart Halloway added a comment -

Michael: A proposal for negative indexes would need to be systematic in considering implications for all functions that have index arguments.

Ian: Clojure is driven by design, not incremental piling of features.

All: clojure.string is incomplete in more important and fundamental ways than negative indexes. This sucks now, and will suck worse as more code is written in different dialects. I find myself wishing string was a contrib, so we could iterate faster.

Show
Stuart Halloway added a comment - Michael: A proposal for negative indexes would need to be systematic in considering implications for all functions that have index arguments. Ian: Clojure is driven by design, not incremental piling of features. All: clojure.string is incomplete in more important and fundamental ways than negative indexes. This sucks now, and will suck worse as more code is written in different dialects. I find myself wishing string was a contrib, so we could iterate faster.
Hide
Andy Fingerhut added a comment -

Stuart: Any specific proposals for how you'd like to see clojure.string improve? If it can be made a contrib, that would be cool, but understood if that would be considered too breaking of a change. Even if it isn't made a contrib, having tickets for improvement ideas you are willing to see means patches might get written, and they'll get in some time.

Show
Andy Fingerhut added a comment - Stuart: Any specific proposals for how you'd like to see clojure.string improve? If it can be made a contrib, that would be cool, but understood if that would be considered too breaking of a change. Even if it isn't made a contrib, having tickets for improvement ideas you are willing to see means patches might get written, and they'll get in some time.

People

  • Assignee:
    Unassigned
    Reporter:
    Ian Eure
Vote (0)
Watch (3)

Dates

  • Created:
    Updated:
    Resolved: