Re: Strange machine-level exception

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

Derek Parnell wrote:
> 
> Daryl Border wrote:
> 
> > I also think the routines
> > should return -1 for an out of bounds condition instead of 0. The current 
> > behavior makes a return value of 0 ambiguous.
> 
> Maybe, but that is likely to break a lot of code that does this sort of thing
> ...
> 
>     if find_from(a,b,c) = 0 then
>         doNotFound()
>     end if
> 
> 

<snipped/>

Another possibility would be to return {} in case of an error. This way:
* "if find_from(...) " will succeed when there is no anomaly, and branch to
found or not found case as find() is doing currently.
* Code like "if find_from(...) " or "p=find_from(...)" will crash on an anomaly
condition because of type check failure, and the coder must decide whether this
is a bug in defining the starting point or an extra step like we use with gets()
is in order.

By the way, I find it a little short sighted not to include an extra argument
for the upper bound. Let's do it now, while the code base using find_from() is
nil. Since Eu doesn't support defaulted arguments contrary to C (sigh), it will
be much tougher to extend it later - that's why we are saddled with a find() and
a find_from() in the first place -. At the same time, if lower_bound >
upper_bound, this will enable reverse find(), something Eu is missing too.

Some special value for the upper bound could be reserved to stand for
"length(target)". For consistency with the current semantics of the $ symbol, I
suggest to use nonpositive numbers to denote offsets from end of sequence.

For example:
p=find_from(x,s,3,-2)

would be an exact equivalent of
object p
if 3<=0 or 3>length(s) then p={}
elsif 3<=length(s)-2 then -- forward find
   p=find(x,s[3..$-2])
   if p then p+= 3-1 end if
else -- reverse find
   p=0
   for i=length(s)-2 to 3 by -1 do
      if equal(s[i],x) then
         p=i
         exit
      end if
   end for
end if

but way faster.

CChris
PS: Derek, by the way, do you check your email these days?

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

Search



Quick Links

User menu

Not signed in.

Misc Menu