Re: Referencing Nested Sequences

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

----- Original Message -----
From: <pblonner at optushome.com.au>
To: "EUforum" <EUforum at topica.com>
Subject: Referencing Nested Sequences


>
>
> I am new to Euphoria but find it an exciting language.

Hi there, a lot of people find it pretty good too.

> One point that bothers me however is as follows -
>
> * to access a sequence nested in a sequence you must make the full
> "path" reference each time if you are to change any values
> You can not simply copy a reference to the inner sequence - otherwise a
> copy of the entire sequence would be made.
>
> e.g   for i = 1 to 10 do
>             nested[9][3][i] = 0
>         end for
>
> if it done as
>       SubList = nested[9][3]
>     for i = 1 to 10 do
>             SubList[i] = 0
>      end for
>
> The latter causes a new list to be made - which is not what I want

Like most languages, there is more that one way to do things. In your
example here, if what you are trying to do is set all values of the
innermost sequence zero, three simpler ways to do it are ...

a) nested[9][3] = repeat(0, length(nested[9][3]))
b) nested[9][3] = and_bits(nested[9][3], 0 )
c) nested[9][3][1..length(nested[9][3])] = 0

in the first two cases, the entire element at [9][3] is replaced with a new
sequence of all zero elements. This is usually faster than replacing each
innermost element one at the time. The third case uses 'slice' technique
which is a useful skill.

> When the subscripts are CONSTANTS it would be so much more readable to
> access the inner element without the full "PATH"

Agreed. This has been asked for year after year but the creators of Euphoria
don't like the idea.

> This is also the same when passing the Sub Sequence as a parameter to a
> function.
> This would be convenient for writing generic sub-functions without them
> knowing how the sub-sequence was accesses

Agreed. This has been asked for year after year but the creators of Euphoria
don't like the idea of passing parameters by reference rather than value. (I
tend to agree with RDS on this one). However there could still be some
shorthand way of specifying a sequence as both a parameter AND a result...
Maybe along the lines of ...

    nested[9][3] = and_bits( @, 0)

> In summary it seems that I am forces to accept a significant reduction
> to efficiency and less readability to write the code I intend.

Efficiency is not much of an issue as Euphoria is extremely efficient and
clever about how sequences are actually implemented. However I must fully
support your call for readability.

> Not also that although I know 'C' pointers would solve this, I would not
> want pointers.
> Rather I would like a "shorthand" way to access the inner elements - I
> would like that  interpreter
> to handle "shorthand" refereneces without the performance loss of
> copying entire sequences

Couldn't agree more. Even we borrowed from Pascal...

   with nested[9][3]
     for i = 1 to 10 do
       .[i] = 0
     end for
   end with

Or if there was an short way of indicating the length of a sequence...

  nested[9][3][1..$] = 0

These are just syntactic sugar, but makes coding a whole lot easier for both
coder and reader.

--
Derek

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

Search



Quick Links

User menu

Not signed in.

Misc Menu