New slice operator

new topic     » topic index » view thread      » older message » newer message

Jeremy Cowgar wrote:
> 
> CChris wrote:
> > 
> > Performing bound checking in an include file is a waste of time, since the
> > backend
> > will perform some bound checking regardless. Testing twice doesn't add any
> > value
> > and wastes CPU. Even more so as thee operations easily occur in loops.
> 
> Bounds checking was the wrong word. What I mean to say is: I want up to the
> first 30 items in a sequence. That's all I need. That's all I want to display.
> So, to do that currently you must:
> 
> }}}
<eucode>
> if length(s) > 30 then
>     s = s[1..30]
> end if
> puts(1, s)
> </eucode>
{{{

> 
> Now, you can simply:
> 
> }}}
<eucode>
> puts(1, head(s, 30))
> </eucode>
{{{

> 

How about this:
puts(1,s[1~30])


Matt wrote:
> My thought is, "Is there really a need for this stuff?"  There are better
> ways to do this in euphoria.  This seems like a very BASIC-y way to do 
> things.  I don't think we should include this in the standard library.

With the introduction of a new slice operator (~ in the above expression) it's
possible to do what you want with functions like left()/head(), right()/tail()
and mid(). This would respect the Euphoria style.
I know that a new operator cannot be implemented in the standard library, but
possibly the modification in the Euphoria source code (front and backend) isn't
difficult. You would use the new slice operator when you DON'T know the length of
a sequence, otherwise you continue to use the original slice operator '..' to
have bounds checking.
A possible definition of the new operator '~' is:
   s[a~b] =: s[min(max(1,a),$+1) .. min($,b)]

I think this should be made in backend C-code. The implementation could use
conditional statements instead of min and max functions.
Then I think we would have the following equivalences:
   left(s,a) =: s[1~a]
   right(s,a) =: s[$-a+1~$]
   mid(s,a,b) =: s[a~b]

Note that, by the definition, the new operator '~' would continue to alert when
b<a-1 like the '..' operator. For all other cases there wouldn't be any
restriction in a and b. If you access a range outside of a sequence, it results
in {}.
Some advantages:
   1) Use Euphoria style;
   2) Can use less characters than the function call;
   3) Less functions to learn and memorize, more international syntax;
   4) Less functions to conflict with;

Disadvantages:
   1) Modification in the front and backend code;
   2) Learn a new operator;

Examples:
name = "John"
name = name[1~35]
puts(1,name)   -- "John"
name = "John Doe"
name = name[$-3+1~$] 
puts(1,name)   -- "Doe"


For a test of the definition:

function max(atom a, atom b)
   if a > b then return a
   else return b
   end if
end function

function min(atom a, atom b)
   if a < b then return a
   else return b
   end if
end function

function tilde(sequence s, atom a, atom b)
   printf(1,"s[%d~%d] = ",{a,b})
   return s[min(max(1,a),$+1) .. min($,b)]
end function

sequence s
s = {0,1,2,3,4}
?tilde(s,0,1)
?tilde(s,-1,1)
?tilde(s,1,5)
?tilde(s,1,10)
?tilde(s,6,10)
?tilde(s,3,2)


Results:
s[0~1] = {0}
s[-1~1] = {0}
s[1~5] = {0,1,2,3,4}
s[1~10] = {0,1,2,3,4}
s[6~10] = {}
s[3~2] = {}

This post doesn't mean that I'm against the implementation of that functions in
the standard library. This is just an idea that maybe makes sense??

Regards,
  Fernando

new topic     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu