Re: we need official libs : Swap

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

Roderick Jackson wrote:

> Um, I'm not sure I understand. Are you saying we
> should weed the concept of sequence indexing out
> of Euphoria?

Obviously not. I am saying (and it appeared that you were agreeing) that
code indexing heterogeneous (of different types) data is often less clear
than code that uses 'simple' variables. The example was:

   -- with sequences
   pt1 = mapToParent( x1, y1 )
   pt2 = mapToParent( x2, y2 )
   draw_line( color, pt1[1], pt1[2], pt2[1], pt2[2] )

   -- with mapping
   { x1, y1 } = mapToParent( x1, y1 )
   { x2, y2 } = mapToParent( x2, y2 )
   draw_line( color, x1, y1, x2, y2 )

You had agreed that the code using mapping was less confusing, error prone
and difficult to debug. You went on to say that:

>> But that's just an inherent side effect of sequence (or array)
>> indexing in general.

Which is the point of mapping - you don't have to store function results in
sequences, and avoid the problems of then indexing that sequence.

When a function returns heterogeneous data, the name of the sequence may
convey some amount of information (although it's often called 'tmp' or
'result'), and a numeric index such as '2' almost always provides *no*
information as to what the content is. So mapping makes sense in these
situations, because it allows sensible names to be attached to the returned
results, instead of tmp[3].

Mapping also makes sense when one function returns a sequence, but the
routine that uses that data requires elements. Here's my favorite example:

   s = get_position()
   ...
   position( s[1], s[2] )

Not all functions returning heterogeneous data necessarily benefit from
mapping. For example, video_config() returns a 8 different values back:

   { mode, lines, cols, xpix, ypix, colors, pages } = video_config()

If the user is only interested in a single value, it would be easier to
write:

   video = video_config()
   mode = video[ VC_MODE ]

Using the index also shields the user from changes in the data structure -
very important!

Obviously, when the data is homogeneous (such as with strings), sequence
indexing is the *only* method that makes sense.

-- David Cuny

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

Search



Quick Links

User menu

Not signed in.

Misc Menu