Using Switch with Words ??
[1/8] from: tim::johnsons-web::com at: 31-Oct-2001 18:10
Hello All: Time to banish all questions about switches :>)
Now I would like to use 'switch such that an argument is evaluated
agains word values. Not being clear here... I know - so here is the
example:
x1: 1 ;treat as global
x2: 2
x3: 3
switch-with-words: func[arg][
switch/default arg[
x1[print "equal to x1"]
x2[print "equal to x2"]
x3[print "equal to x3"]
][print "no case"]
]
switch-with-words 3
>>no case
Oh fudge! I was hoping to see "equal to x3"
Now how can I write 'switch-with-words to get
equal to x3
as the result.
Thanks again!
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.johnsons-web.com
[2/8] from: al:bri:xtra at: 1-Nov-2001 17:46
Instead of:
> switch/default arg[
use:
switch/default arg reduce [ ; etcetera...
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[3/8] from: tim:johnsons-web at: 31-Oct-2001 20:51
On Thu, Nov 01, 2001 at 05:46:53PM +1300, Andrew Martin wrote:
> Instead of:
> > switch/default arg[
<<quoted lines omitted: 3>>
> ICQ: 26227169 http://valley.150m.com/
> -><-
Hi Martin:
Egad! That does it. It's been a good day. :>)
Thank you.
Tim
[4/8] from: al:bri:xtra at: 1-Nov-2001 19:32
While it's easy to think that:
switch Value [
1 [print "one"]
2 [print "two"]
]
is just like C++'s (IIRC) :
switch value {
1: cout << "1"; break;
2: cout << "2"; break;
}
the power of Rebol is in the ability to manipulate itself. Notice that
'switch is a function, and it's second parameter is a block, so this works:
Switch-Block: [
1 [print "one"]
2 [print "two"]
]
switch Value Switch-Block
and then you can easily see how one can 'compose, 'load, 'do or 'reduce
Switch-Block to create interesting effects.
Andrew Martin
ICQ: 26227169 http://valley.150m.com/
[5/8] from: matt:fitzgerald:bigpond at: 1-Nov-2001 17:54
C++:
switch(value) {
case 1 :
cout << value << endl;
break;
case 2 :
cout << value << endl;
break;
default :
cout << value << endl;
}
Oh well.. not Rebol, but a bit different to what you had.
-Matt
[6/8] from: al:bri:xtra at: 1-Nov-2001 20:57
Matt wrote:
> C++:
How quickly I forget...
Andrew Martin
With Rebol, you don't have to worry about silly sintax... :-)
ICQ: 26227169 http://valley.150m.com/
[7/8] from: joel:neely:fedex at: 1-Nov-2001 5:31
Hi, Matt,
Matt Fitzgerald wrote:
> switch(value) {
> case 1 :
<<quoted lines omitted: 6>>
> cout << value << endl;
> }
Given that the same thing can be coded in C as
printf "%d\n", value;
it's easy to see what an improvement C++ is... ;-)
-jn-
--
; sub REBOL {}; sub head ($) {@_[0]}
REBOL []
# despam: func [e] [replace replace/all e ":" "." "#" "@"]
; sub despam {my ($e) = @_; $e =~ tr/:#/.@/; return "\n$e"}
print head reverse despam "moc:xedef#yleen:leoj" ;
[8/8] from: tim:johnsons-web at: 1-Nov-2001 7:49
On Thu, Nov 01, 2001 at 07:32:24PM +1300, Andrew Martin wrote:
..........
> and then you can easily see how one can 'compose, 'load, 'do or 'reduce
> Switch-Block to create interesting effects.
> -><-
One of my obstacles to learning rebol has been that since I've
written in C for 12 years, I find "C-thinking" getting in the
way.
What this conversation about switches has taught me is
to think of 'switch as a function, the cases block as
an 'argument'. The tendency of a C programmer is to
think of switch as a control structure (such as I was).
Good thread!
>
> --
> To unsubscribe from this list, please send an email to
> [rebol-request--rebol--com] with "unsubscribe" in the
> subject, without the quotes.
--
Tim Johnson <[tim--johnsons-web--com]>
http://www.johnsons-web.com
Notes
- Quoted lines have been omitted from some messages.
View the message alone to see the lines that have been omitted