Re: if then else otherwise ...

2001-07-30 Thread Michael G Schwern

On Sat, Jul 28, 2001 at 04:34:46PM +0300, raptor wrote:
> if (cond)
> { }
> else {}
> otherwise {}
> 
> 
> i.e.
> if cond == 1  then  'then-block'
> if cond == 0  then  'else-block'
> if cond == -1  then  'otherwise-block'

Sounds like you need a switch, yes.  The cases where "cond" will
be 1, 0 and -1 is fairly rare in Perl and is pretty much limited
to cmp and <=>.

I'm curious to see examples of existing code which otherwise would
improve, but it really doesn't seem like there's much improvement
either way and you severely complicate the meaning of C,
since it has to now fail on -1 if C exists.


PS  -1 is true, just to make sure that's clear.

-- 

Michael G. Schwern   <[EMAIL PROTECTED]>http://www.pobox.com/~schwern/
Perl6 Quality Assurance <[EMAIL PROTECTED]>   Kwalitee Is Job One
Hold on while I slip into something a little more naked.



Re: if then else otherwise ...

2001-07-30 Thread Bryan C . Warnock

On Monday 30 July 2001 07:29 am, Bart Lateur wrote:
> On Sun, 29 Jul 2001 19:36:43 -0400, Bryan C. Warnock wrote:
> >$x = ($default,$a,$b)[$b<=>$a];  # Much like I did before
>
> Note that
>
>   $x = cond? a : b
>
> does lazy evaluation, i.e. the value for a or for b is only fetched when
> it's actually needed. In your construct, they're all fetched anyway,
> before the condition is even checked.

Excellent point.  Something to remember for the future.

-- 
Bryan C. Warnock
[EMAIL PROTECTED]



Re: if then else otherwise ...

2001-07-30 Thread Bart Lateur

On Sun, 29 Jul 2001 19:36:43 -0400, Bryan C. Warnock wrote:

>$x = ($default,$a,$b)[$b<=>$a];  # Much like I did before

Note that

$x = cond? a : b

does lazy evaluation, i.e. the value for a or for b is only fetched when
it's actually needed. In your construct, they're all fetched anyway,
before the condition is even checked.

-- 
Bart.



Re: if then else otherwise ...

2001-07-29 Thread raptor

> in ?:: or any other condition checking block, 0 is true, everything else
is
> false.  I am yet to see why otherwise or any third condition is needed.
If
> that's then we can have 4 conditions 1,0,-1,undef, and we can keep going.
> That is why there are conditions, if you want to check for -1 you must
> specifically do it.
>
> if($foo == 1)
> {}
> elsif ($foo == -1)
> {}
> elsif (!$foo)
> {}
> else
> {}

]-  then why to use "else-elseif" at all when we can use "goto" instead..:")
... why the languages use two-state(boolean 1/0) when they can use
one-state(1) and still have their work done  SHORCUTs/PATTERNs that is


> I know this is a dead issue, but I just can't see how some people actually
> see the logic in having three conditions 1,0,-1.  What about -2, -3,
etc...
> The whole purpose in ?:: is to deferentiate between true or false, weather
> it be 1|0 2|0, -1|0, doesn't matter is still true or false.
]- it is not -2,-3 ... it is  -1,0,1 or if u want
t  -infinity,0,+infinity... tristate ... forgot about boolean for one
moment...
IT IS SHORCUT.

ok i'ill not urge u moreenought... see... but what about.if ...
we can... can we ?... ok let's talk about something else.

thanx for your time.
=
iVAN
[EMAIL PROTECTED]
=




Re: if then else otherwise ...

2001-07-29 Thread Bryan C . Warnock

On Sunday 29 July 2001 04:32 pm, raptor wrote:
> index(ref $var, 'A') - 1 ? SCALAR-LVALUE-case : HASH-case : ARRAY-case;

That one is actually rather clever

Most of your examples, however, look like you are attempting to bandage some 
poorly designed code upstream.  (Perhaps not, but writing logic built around 
<=> or cmp as a control flow is rarely a good idea.)  

>
> $x = $a <=> $b ? $a : $default : $b;
>

This was your lone example that actually made some sense, and the only 
non-standard answers (by which I mean conventional control flow methods) I 
can think of are:

$x = ($default,$a,$b)[$b<=>$a];  # Much like I did before

($x) = sort { $a <=> $b or $default } ($a,$b);
# Since <=> and cmp were created more-or-less specifically for sort

The former is faster than the latter, but neither are as quick as the more 
conventional structures.

-- 
Bryan C. Warnock
[EMAIL PROTECTED]



RE: if then else otherwise ...

2001-07-29 Thread Sterin, Ilya

in ?:: or any other condition checking block, 0 is true, everything else is
false.  I am yet to see why otherwise or any third condition is needed.  If
that's then we can have 4 conditions 1,0,-1,undef, and we can keep going.
That is why there are conditions, if you want to check for -1 you must
specifically do it.

if($foo == 1)
{}
elsif ($foo == -1)
{}
elsif (!$foo)
{}
else
{}

given/when will of course decrease the amount of typing needed.

I know this is a dead issue, but I just can't see how some people actually
see the logic in having three conditions 1,0,-1.  What about -2, -3, etc...
The whole purpose in ?:: is to deferentiate between true or false, weather
it be 1|0 2|0, -1|0, doesn't matter is still true or false.

Ilya

> -Original Message-
> From: raptor [mailto:[EMAIL PROTECTED]]
> Sent: Sunday, July 29, 2001 4:32 PM
> To: [EMAIL PROTECTED]; [EMAIL PROTECTED]
> Subject: Re: if then else otherwise ...
>
>
> > Linguistically, "if then else, otherwise" doesn't make sense,
> since 'else'
> > and 'otherwise' are synonymous.
> ]- ok .. I choosed wrong word... I'm not native English sorry...
> but I agree
> that if-else-otherwise construct is not so good, for most of the
> people... I
> forgot about it already :")
>
> > ? : : suffers from the same problem, just in terms of the ternary ?:
> > operator (which of course, wouldn't be ternary anymore) instead of
> English.
> > I'm not sure if there will be ambiguity where the standalone colon is
> being
> > used elsewhere, but I suspect that it would make parsing more difficult.
> > (At least in a BNF grammar.)
> ]- then may be some other way :
>
> cond ? then : else ~ otherwise;# i don't know
>
> > Regardless of how you perceive the results of <=> and cmp, it's
> still two
> > conditionals.  Something has to make the differentiation
> between positive
> > and negative.
> ]- we told trenary not boolean context... is trenary context ok ? we will
> have many more contexts in Perl 6.66 aren't we ];")::
>
> > You're simply asking for another way of writing 'if {} elsif {}
> else {}',
> > because you don't like 'elsif'.  Fine.  As has been said before, use a
> > switch.
> > Still too verbose?  Let's look at your example
> >
> > > $whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
> > > $Query = qq{ SELECT  FROM ... WHERE $whereCond ...};
> >
> > I think if you specify WHERE you need a clause.
> > 'SELECT foo FROM bar WHERE' doesn't make sense.
> ]- my mistake sorry. There is "..." if u see, so it is more specificaly :
>
>  $whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
>  $Query = qq{ SELECT  FROM ... WHERE $whereCond field2 =
> (SELECT fieldX
> FROM blah WHERE fieldA = $id )};
>
> is this way okand this is just example, can figure out some
> other at the
> moment, but that is the reason I'm posting here to see all your
> opinions if
> it is worth it. If (not) I'm ok ( sorry for your time) else (then OK)
> otherwise (will see)  :")
>
> =
> iVAN
> [EMAIL PROTECTED]
> =
>
> PS. What type of query to build :
>
> $qtype = cond ? SELECT : INSERT : UPDATE;
>
> or I'm building a query for update or insert how to decide how the current
> key -> value pair will be used :
> this time we are deciding between  INSERT, UPDATE or UPDATE-WHERE clause :
>
> $kv .= $qtype ? do {$values .= "$v, ", "$k, "} : "$k = $v, " :
> "$k = $v AND
> ";#is this correct
>
> later :
> chop,chop...substr..!!
> $query = "INSERT INTO blah ( $kv ) VALUES ( $values )";
> OR
> $query = "UPDATE blah SET $kv WHERE something...";
> OR
> $query = "UPDATE ..something... WHERE $kv";
>
> More examples  :
> =
> index(ref $var, 'A') - 1 ? SCALAR-LVALUE-case : HASH-case : ARRAY-case;
> i.e.
> index(ref $var) - 1 ? $v = $var : $v alias $var : @v = @$var;
>
> =
> form-field-type ?  :  : ;
>
> =
> output-type ? print-to-web  : print-to-stdout \n : print-to-STDERR;
>
> =
>
> $x = $a <=> $b ? $a : $default : $b;
>
> can figure out more at the moment :")



Re: if then else otherwise ...

2001-07-29 Thread raptor

> Linguistically, "if then else, otherwise" doesn't make sense, since 'else'
> and 'otherwise' are synonymous.
]- ok .. I choosed wrong word... I'm not native English sorry... but I agree
that if-else-otherwise construct is not so good, for most of the people... I
forgot about it already :")

> ? : : suffers from the same problem, just in terms of the ternary ?:
> operator (which of course, wouldn't be ternary anymore) instead of
English.
> I'm not sure if there will be ambiguity where the standalone colon is
being
> used elsewhere, but I suspect that it would make parsing more difficult.
> (At least in a BNF grammar.)
]- then may be some other way :

cond ? then : else ~ otherwise;# i don't know

> Regardless of how you perceive the results of <=> and cmp, it's still two
> conditionals.  Something has to make the differentiation between positive
> and negative.
]- we told trenary not boolean context... is trenary context ok ? we will
have many more contexts in Perl 6.66 aren't we ];")::

> You're simply asking for another way of writing 'if {} elsif {} else {}',
> because you don't like 'elsif'.  Fine.  As has been said before, use a
> switch.
> Still too verbose?  Let's look at your example
>
> > $whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
> > $Query = qq{ SELECT  FROM ... WHERE $whereCond ...};
>
> I think if you specify WHERE you need a clause.
> 'SELECT foo FROM bar WHERE' doesn't make sense.
]- my mistake sorry. There is "..." if u see, so it is more specificaly :

 $whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
 $Query = qq{ SELECT  FROM ... WHERE $whereCond field2 = (SELECT fieldX
FROM blah WHERE fieldA = $id )};

is this way okand this is just example, can figure out some other at the
moment, but that is the reason I'm posting here to see all your opinions if
it is worth it. If (not) I'm ok ( sorry for your time) else (then OK)
otherwise (will see)  :")

=
iVAN
[EMAIL PROTECTED]
=

PS. What type of query to build :

$qtype = cond ? SELECT : INSERT : UPDATE;

or I'm building a query for update or insert how to decide how the current
key -> value pair will be used :
this time we are deciding between  INSERT, UPDATE or UPDATE-WHERE clause :

$kv .= $qtype ? do {$values .= "$v, ", "$k, "} : "$k = $v, " : "$k = $v AND
";#is this correct

later :
chop,chop...substr..!!
$query = "INSERT INTO blah ( $kv ) VALUES ( $values )";
OR
$query = "UPDATE blah SET $kv WHERE something...";
OR
$query = "UPDATE ..something... WHERE $kv";

More examples  :
=
index(ref $var, 'A') - 1 ? SCALAR-LVALUE-case : HASH-case : ARRAY-case;
i.e.
index(ref $var) - 1 ? $v = $var : $v alias $var : @v = @$var;

=
form-field-type ?  :  : ;

=
output-type ? print-to-web  : print-to-stdout \n : print-to-STDERR;

=

$x = $a <=> $b ? $a : $default : $b;

can figure out more at the moment :")




Re: if then else otherwise ...

2001-07-29 Thread Bart Lateur

On Sun, 29 Jul 2001 18:08:00 +0300, raptor wrote:

>But at least the second shortcut is worth it, i think :
>>
>>cond ? then : else : otherwise
>
>This has a vague smell of Fortran.
>
>]- I don't know Fortran sorry :")

Then check this out.



-- 
Bart.



Re: if then else otherwise ...

2001-07-29 Thread Bryan C . Warnock

Linguistically, "if then else, otherwise" doesn't make sense, since 'else' 
and 'otherwise' are synonymous. 

? : : suffers from the same problem, just in terms of the ternary ?: 
operator (which of course, wouldn't be ternary anymore) instead of English.  
I'm not sure if there will be ambiguity where the standalone colon is being 
used elsewhere, but I suspect that it would make parsing more difficult.  
(At least in a BNF grammar.)

Regardless of how you perceive the results of <=> and cmp, it's still two 
conditionals.  Something has to make the differentiation between positive 
and negative.

You're simply asking for another way of writing 'if {} elsif {} else {}', 
because you don't like 'elsif'.  Fine.  As has been said before, use a 
switch.

given ($cond) {
when 1:
when 0:
when -1:
}

Is the marker still ^_?

given ($cond) {
when ^_ > 0:
when ^_ < 0:
default:  # Or whatever default will be named.
}

Still too verbose?  Let's look at your example

> $whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
> $Query = qq{ SELECT  FROM ... WHERE $whereCond ...};

I think if you specify WHERE you need a clause.
'SELECT foo FROM bar WHERE' doesn't make sense.

my @c = qw( = > < );
$x = 60;
foreach my $cond (40 <=> $x,60 <=> $x,80 <=> $x) { 
$query = qq{ SELECT ... FROM ... WHERE field $c[$cond] $x };
print "$query\n";
}

Even less to type.  Maybe not all *that* clear, but no less than ?:, ?::, 
and ?:?: all meaning different things.
-- 
Bryan C. Warnock
[EMAIL PROTECTED]



FW: if then else otherwise ...

2001-07-29 Thread Brent Dax

# This makes no sense. ?: tests a boolean value, which is either true or false.
# There is no ternary state for a boolean value. True/False, Yes/No, On/Off,
# 1/0. Are you suggesting Yes/No/Maybe? Or are you redefining True and False?

No.  He's effectively saying this:

condition ? executed-if-positive : executed-if-zero : executed-if-negative

It doesn't quite fit into simple boolean logic.  This has the potential to be useful 
for sorters:

@indices=sort { $ary[$a] <=> $ary[$b] ? -1 : 0 : 1 } @indices;  #sort by values of 
another array in
reverse order

Yes, that last example can be done with a simple $ary[$b] <=> $ary[$a], but I'm trying 
to show you
what this feature does and I just woke up so my brain isn't up and running yet.  :^)

# Doesn't matter. What you're asking has no counterpart in boolean logic, and
# as such would make no sense in any computer language. You may have an idea,
# but you are saying it wrong if you do.

There are plenty of things that have no counterpart in boolean logic.  Where are loops 
defined in
Boolean logic?

--Brent Dax
[EMAIL PROTECTED]




Re: if then else otherwise ...

2001-07-29 Thread raptor

But at least the second shortcut is worth it, i think :
>
>cond ? then : else : otherwise

This has a vague smell of Fortran.

]- I don't know Fortran sorry :")

=
iVAN
[EMAIL PROTECTED]
=




Re: if then else otherwise ...

2001-07-29 Thread raptor

> This makes no sense. ?: tests a boolean value, which is either true or
false.
> There is no ternary state for a boolean value. True/False, Yes/No, On/Off,
> 1/0. Are you suggesting Yes/No/Maybe? Or are you redefining True and
False?

]- I'm not talking about boolean's... but mostly this can be result of some
expression...
The simplest example is <=> and cmp but it also can be some function
call that returns : -1, 0, 1
Let me give you one example (that was the reason for my thouights about
this ):

I have to build a SELECT query and there was three possible combinations to
insert one condition into WHERE part i.e.:

$whereCond =  $cond ? ' field > $x AND ' : '' : ' field < $x AND';
$Query = qq{ SELECT  FROM ... WHERE $whereCond ...};

gotcha...short and clear...

> Doesn't matter. What you're asking has no counterpart in boolean logic,
and
> as such would make no sense in any computer language.

]- yes you are right about this... but in the real live we don't have
real-TRUE and real-FALSE  let's not go further but i think that things
like semiTrue and semiFalse incorporated in some way into the language will
be very cool addition that no one have...:")  i'm dreaming here ... &&&
... |||  :")

>You may have an idea,  but you are saying it wrong if you do.
]- sorry, you are may be right again :")

=
iVAN
[EMAIL PROTECTED]
=




Re: if then else otherwise ...

2001-07-29 Thread John Porter

Bart Lateur wrote:
> This has a vague smell of Fortran.

Nothing vague about it.  It is exactly analogous to Fortran's three-way if.

-- 
John Porter




Re: if then else otherwise ...

2001-07-29 Thread John Porter

David Grove wrote:
> There is no ternary state for a boolean value. True/False, Yes/No, On/Off, 
> 1/0. Are you suggesting Yes/No/Maybe? Or are you redefining True and False?

He's suggesting True/False/-True (as in, 1/0/-1, which is what you get
from cmp and <=>).  How hard is that to understant?


> What you're asking has no counterpart in boolean logic, and 
> as such would make no sense in any computer language. 

Screw boolean.  It's just a three-way switch, exactly as if/else
is a two-way switch.  (As has been noted, switch() would handle this
and the more general case.)

-- 
John Porter




Re: if then else otherwise ...

2001-07-29 Thread Bart Lateur

On Sun, 29 Jul 2001 14:22:23 +0300, raptor wrote:

>But at least the second shortcut is worth it, i think :
>
>cond ? then : else : otherwise

This has a vague smell of Fortran.

-- 
Bart.



Re: if then else otherwise ...

2001-07-29 Thread David Grove

This makes no sense. ?: tests a boolean value, which is either true or false. 
There is no ternary state for a boolean value. True/False, Yes/No, On/Off, 
1/0. Are you suggesting Yes/No/Maybe? Or are you redefining True and False?

Doesn't matter. What you're asking has no counterpart in boolean logic, and 
as such would make no sense in any computer language. You may have an idea, 
but you are saying it wrong if you do.

On Sunday 29 July 2001 07:22, raptor wrote:

> cond ? then : else : otherwise



Re: if then else otherwise ...

2001-07-29 Thread raptor

> I'm lost.  How would you decrease the number of elsif statements with
> otherwise???
]- it is not to decrease the number of  "elsif" (this that i hate elsif was
just comment :") not that u have to stop using it ), but to give a shortcut
... ok forget about "otherwise" ( i also think no one will accept it, no
matter that i like it :") ).
But at least the second shortcut is worth it, i think :

cond ? then : else : otherwise

>And as a matter effect the new perl switch construct would
> take care of that.  It would be called switch but rather I believe given,
if
> I remember right.
]- yep "given". now "switch" and "case" are free for something else :")


=
iVAN
[EMAIL PROTECTED]
=
PS. to David u are welcome to recomend a book, especialy some of the "Learn
it in just 24 hours" ... :"), but I already read a couple of books or
better recomend some SF-book




RE: if then else otherwise ...

2001-07-28 Thread Sterin, Ilya



> -Original Message-
> From: raptor [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, July 28, 2001 12:32 PM
> To: Sterin, Ilya; [EMAIL PROTECTED]
> Subject: Re: if then else otherwise ...
>
>
> I've/m never used/ing "elseif" ( i hate it :")  from the time I
> have to edit
> a perl script of other person that had 25 pages non-stop if-elsif
> sequence)

I'm lost.  How would you decrease the number of elsif statements with
otherwise???  And as a matter effect the new perl switch construct would
take care of that.  It would be called switch but rather I believe given, if
I remember right.

Ilya


> ... never mind there is two conditions in your example...
> of coruse i've think of this just like a shortcut nothing special
> ... later
> on :
>
> $x = cond ? $then : $else : $otherwise;#at least this is a good
> shortcut :")
>
>
> > What's the point, you can accomplish the same with if/elsif/else.  Maybe
> I'm
> > not understanding this correctly, but
> >
> > if (cond)
> > {}
> > elsif (cond)
> > {}
> > else
> > {}
> >
> > Ilya
>



Re: if then else otherwise ...

2001-07-28 Thread David Grove

Oh boo hoo. Might I suggest a good introductory Perl book?

p


On Saturday 28 July 2001 12:32, raptor wrote:
> I've/m never used/ing "elseif" ( i hate it :")  from the time I have to
> edit a perl script of other person that had 25 pages non-stop if-elsif
> sequence) ... never mind there is two conditions in your example...
> of coruse i've think of this just like a shortcut nothing special ... later
> on :



Re: if then else otherwise ...

2001-07-28 Thread raptor

I've/m never used/ing "elseif" ( i hate it :")  from the time I have to edit
a perl script of other person that had 25 pages non-stop if-elsif sequence)
... never mind there is two conditions in your example...
of coruse i've think of this just like a shortcut nothing special ... later
on :

$x = cond ? $then : $else : $otherwise;#at least this is a good shortcut :")


> What's the point, you can accomplish the same with if/elsif/else.  Maybe
I'm
> not understanding this correctly, but
>
> if (cond)
> {}
> elsif (cond)
> {}
> else
> {}
>
> Ilya





RE: if then else otherwise ...

2001-07-28 Thread Sterin, Ilya

What's the point, you can accomplish the same with if/elsif/else.  Maybe I'm
not understanding this correctly, but

if (cond)
{}
elsif (cond)
{}
else
{}

Ilya

> -Original Message-
> From: raptor [mailto:[EMAIL PROTECTED]]
> Sent: Saturday, July 28, 2001 9:35 AM
> To: [EMAIL PROTECTED]
> Subject: if then else otherwise ...
>
>
> hi,
>
> we have <=> and 'cmp' operators but we don't have the conditional
> constroct
> to use better their result :
> May be forthcomming switch will solve this in some way, but isn't
> it better
> to have shortcut like this :
>
> if (cond)
> { }
> else {}
> otherwise {}
>
>
> i.e.
> if cond == 1  then  'then-block'
> if cond == 0  then  'else-block'
> if cond == -1  then  'otherwise-block'
>
> If the "if" construct doesn't have "otherwise" it behave like the current
> "if-else"..
>
>
> =
> iVAN
> [EMAIL PROTECTED]
> =



if then else otherwise ...

2001-07-28 Thread raptor

hi,

we have <=> and 'cmp' operators but we don't have the conditional constroct
to use better their result :
May be forthcomming switch will solve this in some way, but isn't it better
to have shortcut like this :

if (cond)
{ }
else {}
otherwise {}


i.e.
if cond == 1  then  'then-block'
if cond == 0  then  'else-block'
if cond == -1  then  'otherwise-block'

If the "if" construct doesn't have "otherwise" it behave like the current
"if-else"..


=
iVAN
[EMAIL PROTECTED]
=