Dynamic arrays in D 1.0

Edward Diener eddielee_no_spam_here at tropicsoft.com
Tue May 13 14:06:53 PDT 2008


Bill Baxter wrote:
> Edward Diener wrote:
>> boyd wrote:
>>> I think it's because erase and insert are really just the inverse of 
>>> slicing and concatenation.
>>>
>>> int[] a, c;
>>>
>>> // insert
>>> c = a.insert(b, 10);
>>> c = a[0..10] ~ b ~ a[10..$];
>>>
>>> // delete
>>> c = a.delete(&a[10..20]);
>>> c = a[0..10] ~ a[20..$];
>>>
>>> I essence delete and insert are just another way of expressing slices 
>>> and concatenations. Personally I prefer slicing and concatting stuff, 
>>> because it expresses better what's actually happening.
>>>
>>> Delete and Insert on the other hand are higher level and generally 
>>> more intuitive. They leave optimization to the compiler/library. And 
>>> I think most D-users like more control of optimizations than users of 
>>> more high level languages.
>>
>> You make a good point that things like delete, insert, and replace can 
>> be done with slicing and concatentaion. Thanks for bringing up these 
>> solutions. 
> 
> Part of the comment, though, was that delete/insert are "higher-level 
> and generally more intuitive".  With that I agree.
> 
>  > Having used Python extensively, where slicing is also a big
>> part of lists, I should have realized that such operations are largely 
>> subsumed by slicing and concatenation in D. I have to take my C++ hat 
>> off and put my Python hat one when dealing with dynamic arrays in D.
> 
> Fine point, except Python lists also have append, extend, index, insert, 
> pop, and remove methods which you can use when they express your intent 
> better than fidgety slicing expressions.

I don't know if slicing expressions are really "fidgety" but Python has 
more flexiblity in its mutable sequence functionality than D, even using 
slicing syntax.

It hijacks its own 'del' keyword to express 'remove' of some inner 
sequence using possible slicing notation, so maybe D can hijack 'delete' 
for a similar purpose with its dynamic array. It does have both a more 
flexible 'replace' than D, where the number of elements in the 
replacement does not have to equal the number of elements being 
replaced, as it has to in D. Finally it has a neat 'insert' which lets 
you insert another sequence, possibly using slicing, at any index.

Obviously D, or Python itself, can do these things purely through 
slicing-concatenation-reassignment but I agree the "higher level" 
equivalent syntax in Python is easier to understand, as well as possibly 
being bettered optimized.



More information about the Digitalmars-d mailing list