1. in_range()

Here's a suggested addition to MISC.E - a function that returns TRUE if an
index is in the range of a sequence:

   in_range( index, sequence )

Now, if you're like me, you're probably thinking that this is too trivial to
warrant a new function - just write:

   if index > length( s ) then
      ... bad index

which is exactly what I have all over my code. But the test lets negative
numbers slip by, which is a Bad Thing. The new function does the test
correctly, and it's clear what the intent is:

   if not in_range( index, s ) then
      ... bad index

I'm suggesting that it be added to MISC.E because it's such a common test,
and it would produce safer code as a result.

-- David Cuny

new topic     » topic index » view message » categorize

2. Re: in_range()

------=_NextPart_000_002E_01BEB44E.0575AF40
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

I like David's idea for this function, but think that Gabriel's type =
idea has merit also.  If the latter approach is used, how about:

global type index_value(sequence x)  --x[1] is the index, x[2] is the =
sequence
    return x[1]>0 and x[1]<length(x[2])
end type

--Mike Nelson

------=_NextPart_000_002E_01BEB44E.0575AF40
        charset="iso-8859-1"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD W3 HTML//EN">
<HTML>
<HEAD>

<META content=3Dtext/html;charset=3Diso-8859-1 =
http-equiv=3DContent-Type>
<META content=3D'"MSHTML 4.72.3110.7"' name=3DGENERATOR>
</HEAD>
<BODY bgColor=3D#ffffff>
<DIV><FONT color=3D#000000 size=3D2>I like David's idea for this =
function, but think=20
that Gabriel's type idea has merit also.&nbsp; If the latter approach is =
used,=20
how about:</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>global type index_value(sequence =
x)&nbsp; --x[1]=20
is the index, x[2] is the sequence</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>&nbsp;&nbsp;&nbsp; return x[1]&gt;0 =
and=20
x[1]&lt;length(x[2])</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2>end type</FONT></DIV>
<DIV><FONT color=3D#000000 size=3D2></FONT>&nbsp;</DIV>
<DIV><FONT color=3D#000000 size=3D2>--Mike =

------=_NextPart_000_002E_01BEB44E.0575AF40--

new topic     » goto parent     » topic index » view message » categorize

Search



Quick Links

User menu

Not signed in.

Misc Menu