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

can't quite get it right...

 [1/24] from: greggirwin:starband at: 7-Sep-2001 9:42


Hi Anton, I hit Send too quickly...<g> I'm one of those guys that thinks style is very important, and that's a learning curve for me as well. I really like that you can use uppercase? as an alternative to is-uppercase, which many (most) languages wouldn't allow. Are there style guidelines, either written down or just passed by tradition, that most of you expert types agree on? I tend to be a pretty verbose guy, but I'm finding that even in my short time with REBOL, I'm writing things much more concisely and I'm wondering if that will come back to haunt me when maintaining things. Does it matter to experts very much if you use: either all [(ch >= #"A") (ch <= #"Z")] [return true] [return false] versus: return either all [(ch >= #"A") (ch <= #"Z")][true][false] verus: all [(ch >= #"A") (ch <= #"Z")] The last one is quite different to me, and very REBOLish, because it removes everything that isn't necessary. Us newbies have to think for a minute about what the result will be.<g> Do regular REBOLers care if functions return none instead of false or are the two so totally eqivalent that it's a moot point? Thanks again, very much! --Gregg

 [2/24] from: carl:cybercraft at: 8-Sep-2001 11:06


On 08-Sep-01, Carl Read wrote:
>> Does it matter to experts very much if you use: >> either all [(ch >= #"A") (ch <= #"Z")]
<<quoted lines omitted: 8>>
> need to understand regardless to fully understand any of the > versions.
Err, please ignore the style, or lack of any, in that sentence. (: -- Carl Read

 [3/24] from: joel:neely:fedex at: 7-Sep-2001 16:00


Hi, Carl, Carl Read wrote:
> On 08-Sep-01, Carl Read wrote: > >> Does it matter to experts very much if you use:
<<quoted lines omitted: 6>>
> >> all [(ch >= #"A") (ch <= #"Z")] > > I much prefer the latter...
Door Number Three, with a couple of slight variations: all [#"A" <= ch ch <= #"Z"] seems (to my eye, at least) to emphasize ch between #"A" and #"Z" a bit more than the vanilla option three preceding. Unfortunately, there are situations in which a LOGIC! value is expected -- which ALL may not supply -- so one may have to resort to saying to-logic all [#"A" <= ch ch <= #"Z"] (e.g., when the expression is used as an argument to a function that expects a LOGIC! argument...) My experience is that long conditional expressions which end up explicitly returning TRUE or FALSE almost always can be improved (and almost always betray that the writer is not comfortable with the fact that LOGIC!-valued expressions should be just as legitimate as INTEGER!-valued ones -- regardless of language). -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [4/24] from: allenk:powerup:au at: 8-Sep-2001 17:51


> a bit more than the vanilla option three preceding. > Unfortunately, there are situations in which a LOGIC! value > is expected -- which ALL may not supply -- so one may have > to resort to saying > > to-logic all [#"A" <= ch ch <= #"Z"]
In this case I would prefer to use found? all [#"A" <= ch ch <= #"Z"] as (IMHO) "found?" signals the intention of the function better than to-logic Cheers, Allen K

 [5/24] from: arolls::bigpond::net::au at: 8-Sep-2001 18:01

Re: can't quite get it right... - the parse way


Of course, there is also the parse way. s: "a" ; example value uppercase-letter: charset [#"A" - #"Z"] parse s [1 uppercase-letter] ; == false

 [6/24] from: joel:neely:fedex at: 8-Sep-2001 5:26

Re: can't quite get it right...


Allen Kamp wrote:
> > Unfortunately, there are situations in which a LOGIC! value > > is expected -- which ALL may not supply -- so one may have
<<quoted lines omitted: 5>>
> as (IMHO) "found?" signals the intention of the function > better than "to-logic"
YMMV. TMTOWTDI. When using ALL as a "short-circuit" version of AND, my intent is to produce a boolean value. In my mental model for that case, I'm not asking whether a result was found, but asking for the boolean equivalent of the result. This is necessary because of the pun REBOL makes with NONE (just as LISP did with nil). If I were using ALL in a chain-of-defaults context... all [find this-frame "sometag" find prior-frame "sometag" find base-frame "sometag" find base-frame "default"] I'd more likely be thinking in terms of whether a result were FOUND? or not. As long as we're talking about FOUND?, that brings up the possibility of solving the original problem with FIND, as in
>> find charset [#"A" - #"Z"] #"S"
== true
>> find charset [#"A" - #"Z"] #"$"
== none
>>
or (of course) the above prefixed with FOUND? if a boolean result is required. -jn- -- ------------------------------------------------------------ Programming languages: compact, powerful, simple ... Pick any two! joel'dot'neely'at'fedex'dot'com

 [7/24] from: greggirwin:starband at: 6-Sep-2001 22:15


Hi Ryan, I'm not sure if you wanted to replace upper or lower chars with the charset. I'm a newbie and, to me, your code was doing the reverse of what your text said. Anyway...here's a solution which, hopefully, is better than no solution. Maybe a real REBOL will show us both a better way. is-upper-char: func [ch[char!]][ either all [(ch >= #"A") (ch <= #"Z")] [return true] [return false] ] upper-lower-char-repl: func [s[string!] /local new-s up-ch][ new-s: make string! length? s foreach ch s [ up-ch: uppercase copy to-string ch either is-upper-char ch [append new-s ch] [append new-s rejoin [{[} up-ch ch {]}]] ] return new-s ] upper-lower-char-repl "ABC" upper-lower-char-repl "abc" --Gregg

 [8/24] from: al:bri:xtra at: 7-Sep-2001 17:55


> I'm trying to replace uppercase letters in a string with "[Aa]" (if the
letter is "A") Here's a better solution:
>> String: "Forrest Gump"
== "Forrest Gump"
>> String: map String func [Letter [char!]][either all [#"A" <= Letter
Letter <= #"Z"][rejoin ["[" Letter lowercase to-string Letter "]"]][Letter]] == "[Ff]orrest [Gg]ump"
>> string
== "[Ff]orrest [Gg]ump"
>>
Andrew Martin ICQ: 26227169 http://zen.scripterz.org

 [9/24] from: arolls:bigpond:au at: 7-Sep-2001 21:55


> is-upper-char: func [ch[char!]][ > either all [(ch >= #"A") (ch <= #"Z")] > [return true] > [return false] > ]
The above function could be trimmed a little bit: uppercase?: func [ ch [char!] ][ return either all [(ch >= #"A") (ch <= #"Z")][true][false] ] We can trim out the either and the return as well: uppercase?: func [ ch [char!] ][ all [(ch >= #"A") (ch <= #"Z")] ] Although this last one returns none instead of false, it can still be used, since logically none is equivalent to false. You can use it in an if statement. :)

 [10/24] from: greggirwin:starband at: 7-Sep-2001 9:27


Thanks Anton!

 [11/24] from: ryan:christiansen:intellisol at: 6-Sep-2001 17:31


I'm trying to replace uppercase letters in a string with "[Aa]" (if the letter is "A") and the routine runs okay, but doesn't do anything at all to the word 'value. What am I doing wrong? value: "Forrest Gump" foreach value-letter value [ letter: make string! value-letter uppercase-letter: uppercase letter either find/match/case uppercase-letter letter [][ upperlowerletters: rejoin [{^[} uppercase-letter letter {^]}] replace value value-letter upperlowerletters ] ] Ryan C. Christiansen Web Developer Intellisol International 4733 Amber Valley Parkway Fargo, ND 58104 701-235-3390 ext. 6671 FAX: 701-235-9940 http://www.intellisol.com Global Leader in People Performance Software _____________________________________ Confidentiality Notice This message may contain privileged and confidential information. If you think, for any reason, that this message may have been addressed to you in error, you must not disseminate, copy or take any action in reliance on it, and we would ask you to notify us immediately by return email to [ryan--christiansen--intellisol--com]

 [12/24] from: hallvard:ystad:helpinhand at: 29-Dec-2002 22:18


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? Thanks, ~H Dixit Anton (12.55 07.09.2001):

 [13/24] from: greggirwin:mindspring at: 29-Dec-2002 15:28


Hi Hallvard, HY> uppercase?: func [ ch [char! string!] ] [ HY> either char? ch [ HY> return all [(ch >= #"A") (ch <= #"Z")] HY> ] [ HY> foreach ar ch [if not all [(ar >= #"A") (ar <= #"Z")] [return false]] HY> ] HY> true HY> ] HY> ...could this be trimmed further? For the string comparison, you could do this: either ch == uppercase copy ch [true][false] -- Gregg

 [14/24] from: al:bri:xtra at: 30-Dec-2002 12:08


Hallvard wrote:
> could this be trimmed further?
Yes. I think it would be best as a native that exposed the operating system's equivalent of 'Uppercase? (and 'Lowercase? as well) for maximum portability and speed. Andrew Martin ICQ: 26227169 http://valley.150m.com/

 [15/24] from: rotenca:telvia:it at: 30-Dec-2002 2:33


Hi
> > could this be trimmed further? > > Yes. I think it would be best as a native that exposed the operating > system's equivalent of 'Uppercase? (and 'Lowercase? as well) for maximum > portability and speed. >
uppercase?: func [ch [char! string!] /local tmp] [ all [char? ch ch: to-string ch] ch == uppercase copy ch ] --- Ciao Romano

 [16/24] 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
<<quoted lines omitted: 22>>
> 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

 [17/24] from: carl:cybercraft at: 30-Dec-2002 16:05


On 30-Dec-02, Romano Paolo Tenca wrote:
> Hi >>> could this be trimmed further?
<<quoted lines omitted: 5>>
> ch == uppercase copy ch > ]
Very nice Romano! (I take it tmp's just a leftover from an earlier version?) Out of interest, are there any major overheads with a to-string string ? Just wondering if it is best to avoid it if you can, like you've done above. -- Carl Read

 [18/24] from: sunandadh:aol at: 30-Dec-2002 2:20


Romano:
> uppercase?: func [ch [char! string!] /local tmp] [ > all [char? ch ch: to-string ch] > ch == uppercase copy ch > ]
I'd thought of that approach too It's good in that it'll handle issue! and other data types -- you can remove the [Char! string!] from the spec. But it returns 'true if there are non-letters in the input string: uppercase? "AA++" == True The original would return false for that. I don't know what the original requirement was though, so I don't know which approach meets it best. Sunanda

 [19/24] from: hallvard:ystad:helpinhand at: 30-Dec-2002 10:40


Brilliant! Thanks. ~H Dixit Romano Paolo Tenca (02.33 30.12.2002):

 [20/24] from: rotenca:telvia:it at: 30-Dec-2002 12:41


Hi all,
> I take it tmp's just a leftover from an earlier version?
yes
>I'd thought of that approach too It's good in that it'll handle issue! and >other data types -- you can remove the [Char! string!] from the spec
uppercase?: func [ch] [ any [any-string? ch ch: to-string ch] ch == uppercase copy ch ]
> But it returns 'true if there are non-letters in the input string: > > uppercase? "AA++" > == True
The function strictly means: "if the rebol uppercase function is applied to the string, does it remain unchanged?"
>Out of interest, are there any major overheads with a to-string >"string"? Just wondering if it is best to avoid it if you can, like >you've done above.
Only to not copy twice the string (speed/memory). --- Ciao Romano

 [21/24] from: carl:cybercraft at: 31-Dec-2002 19:26


On 30-Dec-02, Andrew Martin wrote:
> Hallvard wrote: >> could this be trimmed further? > Yes. I think it would be best as a native that exposed the operating > system's equivalent of 'Uppercase? (and 'Lowercase? as well) for > maximum portability and speed.
Thinking about this, I was wondering if it'd make sense to add a compare refinement (as with sort) to *all* and *any*. We could then do the likes of this... all/compare "ABC" func [v][all [char? v v >= ##"A" v <= ##"Z"]] That'd check that all values in a series were uppercase, while the following would check for any numbers in a block... any/compare ["a" 1 "b" 2] func [v][number? v] and so on. (*all* and *any* currently only work on blocks, but with compare could be extended to series.) Or is there a word in REBOL that already allows us to do this? -- Carl Read

 [22/24] from: carl:cybercraft at: 31-Dec-2002 20:15


On 31-Dec-02, Carl Read wrote:
> On 30-Dec-02, Andrew Martin wrote: >> Hallvard wrote:
<<quoted lines omitted: 6>>
> do the likes of this... > all/compare "ABC" func [v][all [char? v v >= ##"A" v <= ##"Z"]]
Should be this... all/compare "ABC" func [v][all [char? v v >= #"A" v <= #"Z"]] of course. Odd behaviour from mailer with cutting and pasting... (:
> That'd check that all values in a series were uppercase, while the > following would check for any numbers in a block... > any/compare ["a" 1 "b" 2] func [v][number? v] > and so on. (*all* and *any* currently only work on blocks, but with > compare could be extended to series.) > Or is there a word in REBOL that already allows us to do this?
-- Carl Read

 [23/24] from: g::santilli::tiscalinet::it at: 31-Dec-2002 11:31


Hi Carl, On Tuesday, December 31, 2002, 7:26:57 AM, you wrote: CR> Or is there a word in REBOL that already allows us to do this?
>> up-chars: charset [#"A" - #"Z"] >> parse "ABC" [some up-chars]
== true
>> parse "AbC" [some up-chars]
== false
>> parse "ABC++" [some up-chars]
== false
>> parse ["a" 1 "b" 2] [to number! to end]
== true
>> parse ["a" "1" "b" "2"] [to number! to end]
== false Regards, Gabriele. -- Gabriele Santilli <[g--santilli--tiscalinet--it]> -- REBOL Programmer Amigan -- AGI L'Aquila -- REB: http://web.tiscali.it/rebol/index.r

 [24/24] from: carl:cybercraft at: 1-Jan-2003 9:32


On 31-Dec-02, Gabriele Santilli wrote:
> Hi Carl, > On Tuesday, December 31, 2002, 7:26:57 AM, you wrote:
<<quoted lines omitted: 10>>
>>> parse ["a" "1" "b" "2"] [to number! to end] > == false
Well, I did know of parse. (: But I was thinking of something that applies a standard function to every value in a series. I'm sure I'd seen some word that does that. And parsing's so different from standard REBOL that switching over to it is difficult for those who haven't used it enough for it to have stuck in their mind. I have to re-learn it whenever I have to do more than the most basic splitting. And it doesn't help that strings and blocks are treated differently...
>> parse "ABC" [to char! to end]
** Script Error: Invalid argument: char No doubt it's for performance reasons, but anyway... -- Carl Read

Notes
  • Quoted lines have been omitted from some messages.
    View the message alone to see the lines that have been omitted