<< Back to previous view

[CLJ-717] A range function for characters, char-range Created: 15/Jan/11  Updated: 16/Jan/11  Resolved: 15/Jan/11

Status: Closed
Project: Clojure
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Enhancement Priority: Minor
Reporter: Anthony Simpson Assignee: Unassigned
Resolution: Declined Votes: 0
Labels: None

Attachments: File char-range.diff    
Patch: Code

 Description   

I've seen a lot of cases where people would do stuff like this:

(def alphabet "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")

Or other equally meh things in order to get a range of characters. Haskell's range supports creating ranges of characters, so that was inspiration for this addition.

I've created a function that is the analogue of range for characters. I've called it char-range. It works almost precisely the same as range (and uses range under the hood), only the upper bound isn't exclusive. It made more sense to me for it to be inclusive, so that things like (char-range \a \z) work and make sense.

It takes the same argument combinations as range. For no arguments, it produces a lazy sequence of characters from Character/MIN_VALUE to Character/MAX_VALUE. For two arguments, it produces a lazy sequence from Character/MIN_VALUE to end. It allows for the same stepping that range allows for as well.



 Comments   
Comment by Anthony Simpson [ 15/Jan/11 10:29 AM ]

A friend pointed out to me that I shouldn't have set the issue's approval to 'Test'. I took "If the ticket has (or you write) a patch that applies, test it. If it seems to work, set the status to "Test."" out of context. I don't see a way to set the Approval back to "None", so I'm just going to apologize and whistle.

Comment by Stuart Halloway [ 15/Jan/11 4:30 PM ]

Before implementing a feature like this, please document the problem and the other possible solutions/variants. Some things to think about:

  • is the alphabet example a one-off, or representative of a family of things that are causing real pain?
    • wouldn't alphabet be less readable as a range? You'd have to use two ranges, and know the character after Z
  • we already have range, this would add char-range. Are other things rangeable?
    • Haskell comparison useful
    • more so if expanded to a few other languages
  • if rangeable-ness is going to extend to non-numeric things, should it be polymorphic, instead of separate functions?
  • is rangeable-ness a categoric thing, like Comparable?
    • if so, are there ever types that might range in more than one way
  • what are the performance/needs and expectations of char-range users? At a glance, this patch uses higher-order fns and a fair amount of boxing, where the existing range fn is built in a lower-level way.

If you want, please start a design discussion at http://dev.clojure.org/display/design/Home

Comment by Scott Olson [ 15/Jan/11 6:14 PM ]

It's a lot easier to tell that (char-range \a \z) is correct than "abcdefghijkmnopqrstuvwxyz" (did you notice the missing character at a glance?).

Why would you need to know the character after Z? The OP clearly said the upper bound would be inclusive.

As for performance, char-range uses the functions map and range which are themselves built in a fast low-level way, so as long as they perform well, so will char-range.

Comment by Stuart Halloway [ 16/Jan/11 9:12 AM ]

So range will be exclusive while char-range will be inclusive? Tough sell. You will need more motivation that this one example.

I am not saying this couldn't be the right patch. I am saying that the hard part is answering the design questions, which has to come first. Also, threaded comments on a patch isn't the best place to do it. If char-range is important enough to you, then please start a design page under http://dev.clojure.org/display/design/Home.

Generated at Thu Oct 30 09:35:01 CDT 2014 using JIRA 4.4#649-r158309.