Re: 'switch' operator improvement

2007-10-16 Thread David Teller
Pattern-matching with views [1,2], anyone ?

Cheers,
 David

[1] http://martin.jambon.free.fr/micmatch-manual.html#htoc10
[2] http://blogs.msdn.com/dsyme/archive/2006/08/16/ActivePatterns.aspx

On Tue, 2007-10-16 at 20:11 +0200, liorean wrote:
> On 16/10/2007, Peter Hall <[EMAIL PROTECTED]> wrote:
> > already you can do something like this:
> >
> > var str;
> > switch(true){
> > case /a/.test(str):
> >   alert('a');
> >   break;
> > case /b/.test(str):
> >   alert('b');
> >   break;
> > }
> 
> I can only imagine that solution being preferable to chained
> if..else-statements in one case, and that would be if you actually
> used the fall through mechanism. If you don't fall through, it's just
> bloat without any gain.
-- 
David Teller --
Security of Distributed Systems ---
Project JStify: Static Analysis for JavaScript 2  -
-- http://www.univ-orleans.fr/lifo/Members/David.Teller
- Laboratoire d'Informatique Fondamentale d'Orleans

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread liorean
On 16/10/2007, Peter Hall <[EMAIL PROTECTED]> wrote:
> already you can do something like this:
>
> var str;
> switch(true){
> case /a/.test(str):
>   alert('a');
>   break;
> case /b/.test(str):
>   alert('b');
>   break;
> }

I can only imagine that solution being preferable to chained
if..else-statements in one case, and that would be if you actually
used the fall through mechanism. If you don't fall through, it's just
bloat without any gain.
-- 
David "liorean" Andersson
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Brendan Eich
On Oct 16, 2007, at 8:19 AM, Dave Herman wrote:

> But that's not what you proposed, is it? I understood your proposal to
> mean something more like:
>
> function f(g) {
>  if (let (tmp = g())// case g():
>  (tmp is RegEx ? tmp.match(x) : x == tmp)) 
>  if 
> }
>
> Dave

Right, and that is not only backward incompatible, but user-hostile  
unless you type everything. It's perl-ish magic.

I say use an if-else and call match if that is what you want. EIBTI,  
the Pythonistas say (Explicit Is Better Than Implicit).

/be

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Peter Hall
Minor nitpick, but that should be "===" not "==".

I really like the idea, but probably it should be done in a way that
could be generalised beyond just the RegExp case. And that doesn't
seem like something that can be done in the es4 time-frame.

But already you can do something like this:

var str;
switch(true){
case /a/.test(str):
  alert('a');
  break;
case /b/.test(str):
  alert('b');
  break;
}

Not quite as neat or readable, but the same functionality and not
overly verbose.

I'd be interested to know how often switch statements are used in the
real-world, that could not be trivially replaced with if..else, ie
without breaks on every case. Has that been previously measured?

Peter


On 10/16/07, Dave Herman <[EMAIL PROTECTED]> wrote:
> But that's not what you proposed, is it? I understood your proposal to
> mean something more like:
>
> function f(g) {
>  if (let (tmp = g())// case g():
>  (tmp is RegEx ? tmp.match(x) : x == tmp)) 
>  if 
> }
>
> Dave
>
> [EMAIL PROTECTED] wrote:
> > I think switch ... case construction must be interpreted as:
> > function f(g) {
> >  if( x == g() )  // case g():
> >  if( //  case ...
> >  }
> >
> > - Original Message -
> > From: "Dave Herman" <[EMAIL PROTECTED]>
> > To: "Lars T Hansen" <[EMAIL PROTECTED]>
> > Cc: <[EMAIL PROTECTED]>; 
> > Sent: Tuesday, October 16, 2007 6:04 PM
> > Subject: Re: 'switch' operator improvement
> >
> >
> >> It's clever, but it's a special case that may not abstract very
> >> smoothly. For example:
> >>
> >> function f(g) {
> >> switch (x) {
> >> case g():
> >> ...
> >> }
> >> }
> >>
> >> The behavior of my function depends on whether g() returns a RegEx or a
> >> non-RegEx. Maybe that's what you want, but it means it's an extra
> >> special case that you have to be aware of whenever abstracting a case
> >> statement.
> >>
> >> Dave
> >>
> >> Lars T Hansen wrote:
> >>> Neat, though it breaks backward compatibility -- each regexp is
> >>> converted to string before the comparison, IIRC.  (Compatibility may
> >>> not be a big problem in practice in this case.)
> >>>
> >>> --lars
> >>>
> >>> On 10/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>  allow RegEx in case
> 
>  var str= 'a';
>  switch( str ) {
>    case /a/:
>  alert('a');
>  break;
> 
>    case /b/:
>  alert('b');
>  break;
>    }
> 
>  ___
>  Es4-discuss mailing list
>  Es4-discuss@mozilla.org
>  https://mail.mozilla.org/listinfo/es4-discuss
> 
> 
> >>> ___
> >>> Es4-discuss mailing list
> >>> Es4-discuss@mozilla.org
> >>> https://mail.mozilla.org/listinfo/es4-discuss
> > ___
> > Es4-discuss mailing list
> > Es4-discuss@mozilla.org
> > https://mail.mozilla.org/listinfo/es4-discuss
>
> ___
> Es4-discuss mailing list
> Es4-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: Possibility of standardized sprintf() in ES4?

2007-10-16 Thread Alex Russell
[ snip ]

> Sure. From what I can see of the Microsoft documentation a
> standardized String.format() function would meet the need for a
> standardized way of formatting a string just as well as a method
> based on C/Perl/PHP/Ruby sprintf() and Python's native string
> formatting, where named parameters are used instead of positional
> parameters.

Python allows both, and I prefer that style.

> Assuming there are no intellectual property concerns with adopting
> Microsoft's specification for this method, it would satisfy my use
> case for a string formatting method that accepts explicit positional
> or named parameters in support of internationalized text. I would
> simply be delighted if ES4 saved me and others from having to roll
> our own string formatter classes!

Eh, just go w/ Python's. It's more flexible, "feels" more like printf 
if/when you need it to, and can be used in the .NET-ish way.

Regards

-- 
Alex Russell
[EMAIL PROTECTED] A99F 8785 F491 D5FD 04D7 ACD9 4158 FFDF 2894 6876
[EMAIL PROTECTED] BE03 E88D EABB 2116 CC49 8259 CF78 E242 59C3 9723


pgp3LKURV6n3e.pgp
Description: PGP signature
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Dave Herman
But that's not what you proposed, is it? I understood your proposal to 
mean something more like:

function f(g) {
 if (let (tmp = g())// case g():
 (tmp is RegEx ? tmp.match(x) : x == tmp)) 
 if 
}

Dave

[EMAIL PROTECTED] wrote:
> I think switch ... case construction must be interpreted as:
> function f(g) {
>  if( x == g() )  // case g():
>  if( //  case ...
>  }
> 
> - Original Message - 
> From: "Dave Herman" <[EMAIL PROTECTED]>
> To: "Lars T Hansen" <[EMAIL PROTECTED]>
> Cc: <[EMAIL PROTECTED]>; 
> Sent: Tuesday, October 16, 2007 6:04 PM
> Subject: Re: 'switch' operator improvement
> 
> 
>> It's clever, but it's a special case that may not abstract very 
>> smoothly. For example:
>>
>> function f(g) {
>> switch (x) {
>> case g():
>> ...
>> }
>> }
>>
>> The behavior of my function depends on whether g() returns a RegEx or a 
>> non-RegEx. Maybe that's what you want, but it means it's an extra 
>> special case that you have to be aware of whenever abstracting a case 
>> statement.
>>
>> Dave
>>
>> Lars T Hansen wrote:
>>> Neat, though it breaks backward compatibility -- each regexp is
>>> converted to string before the comparison, IIRC.  (Compatibility may
>>> not be a big problem in practice in this case.)
>>>
>>> --lars
>>>
>>> On 10/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
 allow RegEx in case

 var str= 'a';
 switch( str ) {
   case /a/:
 alert('a');
 break;

   case /b/:
 alert('b');
 break;
   }

 ___
 Es4-discuss mailing list
 Es4-discuss@mozilla.org
 https://mail.mozilla.org/listinfo/es4-discuss


>>> ___
>>> Es4-discuss mailing list
>>> Es4-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es4-discuss
> ___
> Es4-discuss mailing list
> Es4-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Eugen.Konkov
I think switch ... case construction must be interpreted as:
function f(g) {
 if( x == g() )  // case g():
 if( //  case ...
 }

- Original Message - 
From: "Dave Herman" <[EMAIL PROTECTED]>
To: "Lars T Hansen" <[EMAIL PROTECTED]>
Cc: <[EMAIL PROTECTED]>; 
Sent: Tuesday, October 16, 2007 6:04 PM
Subject: Re: 'switch' operator improvement


> It's clever, but it's a special case that may not abstract very 
> smoothly. For example:
> 
> function f(g) {
> switch (x) {
> case g():
> ...
> }
> }
> 
> The behavior of my function depends on whether g() returns a RegEx or a 
> non-RegEx. Maybe that's what you want, but it means it's an extra 
> special case that you have to be aware of whenever abstracting a case 
> statement.
> 
> Dave
> 
> Lars T Hansen wrote:
>> Neat, though it breaks backward compatibility -- each regexp is
>> converted to string before the comparison, IIRC.  (Compatibility may
>> not be a big problem in practice in this case.)
>> 
>> --lars
>> 
>> On 10/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>>
>>> allow RegEx in case
>>>
>>> var str= 'a';
>>> switch( str ) {
>>>   case /a/:
>>> alert('a');
>>> break;
>>>
>>>   case /b/:
>>> alert('b');
>>> break;
>>>   }
>>>
>>> ___
>>> Es4-discuss mailing list
>>> Es4-discuss@mozilla.org
>>> https://mail.mozilla.org/listinfo/es4-discuss
>>>
>>>
>> ___
>> Es4-discuss mailing list
>> Es4-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es4-discuss
>
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Dave Herman
It's clever, but it's a special case that may not abstract very 
smoothly. For example:

function f(g) {
 switch (x) {
 case g():
 ...
 }
}

The behavior of my function depends on whether g() returns a RegEx or a 
non-RegEx. Maybe that's what you want, but it means it's an extra 
special case that you have to be aware of whenever abstracting a case 
statement.

Dave

Lars T Hansen wrote:
> Neat, though it breaks backward compatibility -- each regexp is
> converted to string before the comparison, IIRC.  (Compatibility may
> not be a big problem in practice in this case.)
> 
> --lars
> 
> On 10/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>>
>> allow RegEx in case
>>
>> var str= 'a';
>> switch( str ) {
>>   case /a/:
>> alert('a');
>> break;
>>
>>   case /b/:
>> alert('b');
>> break;
>>   }
>>
>> ___
>> Es4-discuss mailing list
>> Es4-discuss@mozilla.org
>> https://mail.mozilla.org/listinfo/es4-discuss
>>
>>
> ___
> Es4-discuss mailing list
> Es4-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: SYNTAX BUG

2007-10-16 Thread liorean
On 16/10/2007, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
> In the next line there is ambigious between RegEx and comment
> str.match(//);

Not really. // is always a line comment starter in ES3:

7.8.5 Regular Expression Literals

/- - -/

NOTE Regular expression literals may not be empty; instead of
representing an empty regular expression literal, the characters //
start a single-line comment. To specify an empty regular expression,
use /(?:)/.

-- 
David "liorean" Andersson
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


RE: Possibility of standardized sprintf() in ES4?

2007-10-16 Thread Samuel R. Neff

I would be all for .NET's String.Format() style of formatting.  Adobe Flex
provides a similar method in StringUtil.substitute() which provides ordered
substitution using the same {0} and {1} style tokens, but doesn't have any
formatting.

Sam

---
We're Hiring! Seeking a passionate developer to join our team building Flex
based products. Position is in the Washington D.C. metro area. If interested
contact [EMAIL PROTECTED]
 
-Original Message-
From: [EMAIL PROTECTED]
[mailto:[EMAIL PROTECTED] On Behalf Of zwetan
Sent: Monday, October 15, 2007 5:51 PM
To: Dan Scott
Cc: es4-discuss@mozilla.org
Subject: Re: Possibility of standardized sprintf() in ES4?

Hi,

...
>
> Is there any other interest in adding a native sprintf() (probably as
> String.sprintf()) to the ES4 specs?
>

I'm interested in string formating, but surely not sprintf
I don't find it intuitive at all

I prefer something closer to String.format from .NET
but more dynamic and being able to use sort of tokens
like the ones you can use with E4X ({token})

see here for an implementation
http://maashaack.googlecode.com/svn/trunk/ES4a/src/system/Strings.as
( public static function format( format:String, ...args ):String )

cheers,
zwetan
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss

___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


Re: 'switch' operator improvement

2007-10-16 Thread Lars T Hansen
Neat, though it breaks backward compatibility -- each regexp is
converted to string before the comparison, IIRC.  (Compatibility may
not be a big problem in practice in this case.)

--lars

On 10/16/07, [EMAIL PROTECTED] <[EMAIL PROTECTED]> wrote:
>
>
> allow RegEx in case
>
> var str= 'a';
> switch( str ) {
>   case /a/:
> alert('a');
> break;
>
>   case /b/:
> alert('b');
> break;
>   }
>
> ___
> Es4-discuss mailing list
> Es4-discuss@mozilla.org
> https://mail.mozilla.org/listinfo/es4-discuss
>
>
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss


'switch' operator improvement

2007-10-16 Thread Eugen.Konkov
allow RegEx in case

var str= 'a';
switch( str ) {
  case /a/:
alert('a');
break;

  case /b/:
alert('b');
break;
  }
___
Es4-discuss mailing list
Es4-discuss@mozilla.org
https://mail.mozilla.org/listinfo/es4-discuss