Mailing List Archive: 49091 messages
  • Home
  • Script library
  • AltME Archive
  • Mailing list
  • Articles Index
  • Site search
 

[REBOL] Re: can't quite get it right...

From: carl:cybercraft at: 30-Dec-2002 15:10

On 30-Dec-02, Hallvard Ystad wrote:
> Hi Anton, > Your uppercase? function is nice. I looked at it a bit, because I > need a function that could do something like: >>> uppercase? #"n" > == none >>> uppercase? #"Y" > == true >>> uppercase? "No" > == false >>> uppercase? "YES" > == true > So it would have to work with both char! and string!. In other > programming languages, I would have made two methods, and have the > string function repeatedly use the char function. In rebol, here's > what I came up with: > uppercase?: func [ ch [char! string!] ] [ > either char? ch [ > return all [(ch >= #"A") (ch <= #"Z")] > ] [ > foreach ar ch [if not all [(ar >= #"A") (ar <= #"Z")] [return > false]] > ] > true > ] > As you can see, it's an extended version of your function. And so > here's my question: could this be trimmed further?
Hi Hallvard, Assuming you're happy with a false instead of a none being returned for non-uppercase characters, you could skip the character check entirely by just converting it to a string... uppercase?: func [ ch [char! string!] ] [ foreach ar to-string ch [ if not all [(ar >= #"A") (ar <= #"Z")] [return false] ] true ]
>> uppercase? #"a"
== false
>> uppercase? #"A"
== true
>> uppercase? "a"
== false
>> uppercase? "A"
== true
>> uppercase? "AbC"
== false
>> uppercase? "aBc"
== false
>> uppercase? "abc"
== false
>> uppercase? "ABC"
== true Note this though...
>> uppercase? ""
== true which mightn't be the result you want. -- Carl Read