Clojure

A range function for characters, char-range

Details

  • Type: Enhancement Enhancement
  • Status: Closed Closed
  • Priority: Minor Minor
  • Resolution: Declined
  • Affects Version/s: None
  • Fix Version/s: None
  • Component/s: None
  • Labels:
    None
  • 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.

Activity

Hide
Anthony Simpson added a comment -

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.

Show
Anthony Simpson added a comment - 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.
Hide
Stuart Halloway added a comment -

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

Show
Stuart Halloway added a comment - 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
Hide
Scott Olson added a comment -

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.

Show
Scott Olson added a comment - 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.
Hide
Stuart Halloway added a comment -

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.

Show
Stuart Halloway added a comment - 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.

People

Vote (0)
Watch (2)

Dates

  • Created:
    Updated:
    Resolved: