MOO-cows Mailing List Archive

[Prev][Next][Index][Thread]

Re: [wish list]: perl stuff



H. Peter Anvin wrote:

> >
> > Somewhat Perly.  The ability to reference '[a..b]' as if it were an
> > inclusive list of all integers 'a' through 'b'.  Like...
> >
> >         if (foo in [5..8])
> >
> > Rather than...
> >
> >         if ((foo > 4) && (foo < 9))
> >
> 
> This is, by the way, hideously inefficient and would cause severe
> server spam, since you are needlessly creating a list when you don't
> need to.  Perl gets around this by having a pretty extensive
> optimizer, but developing one for MOO would be a *lot* of work.

Why does it have to create a list?  Why would it be a lot of work?
Both syntaxes would basically do the same thing...but the
'if (foo in [5..8])' one would be smaller in MOO code and probably
would use fewer bytes when tokenized.  It wouldn't have to generate
a list of {5, 6, 7, 8} to see if foo is in it...the bytecodes for
this particular use of the 'in' operator would just have to generate
a comparison similar to 'if ((foo >= 5) && (foo <= 8))'.

The behavior of 'in' already changes, depending on whether it is
in a normal expression, or in a for..endfor statement.  It wouldn't
be as hard as you think to make [x..y] a valid operand for 'in' in
a normal expression.  ATM it's not.

If you really wanted to have fun, make something like this:

  ;[1..5] in [3..8]
  => [3..5]

What we would then be implementing is a new datatype for ranges.

  ;typeof ([5..8])
  => RANGE

But now that you mention it, it would be neat to be able to create
a list by something like

  foo = {@[5..8]}

which would return

  {5, 6, 7, 8};

or even use object numbers, like

  if (foo in [#1..#90])

or
 
  foo = {@[#1..#90]}

It wouldn't be incredibly frequently used, but it would have
a use.


References:

Home | Subject Index | Thread Index