Dynamic array ot not

Salih Dincer salihdb at hotmail.com
Mon Jan 17 01:06:05 UTC 2022


```d
import std; // If we summarize in code...
void main()
{
   // It's a dynamic array and its copy below:
   size_t[] arr = [1, 2, 3, 4, 5, 6];
   auto arrCopy = arr.dup;

   // This is its lazy range:
   auto range = arr.chunks(2);
   typeid(range).writeln(": ", range);

   // But this is its copy (you'll see that later!)
   auto array = arr.chunks(2).map!array.array;

   // This is a series it's created from slices:
   auto slices =[arr[0..2], arr[2..4], arr[4..6]];
   typeid(slices).writeln(": ", slices);

   // Equal for fleeting moment:
   assert(array == slices);

   // Now, the datasource is sorted but (!)
   // All copies will not be affected!
   arrCopy.writeln(" => ", arr.sort!"a>b");

   // and now they are not equal! Because,
   // slices were affected by the sort process,
   // the range too...
   assert(array != slices);

   // Taaata, magic...
   // Your eyes don't surprise you!
   typeid(range).writeln(": ", range);
   typeid(slices).writeln(": ", slices);
}
/* CONSOLE OUT:
std.range.Chunks!(ulong[]).Chunks: [[1, 2], [3, 4], [5, 6]]
ulong[][]: [[1, 2], [3, 4], [5, 6]]
[1, 2, 3, 4, 5, 6] => [6, 5, 4, 3, 2, 1]
std.range.Chunks!(ulong[]).Chunks: [[6, 5], [4, 3], [2, 1]]
ulong[][]: [[6, 5], [4, 3], [2, 1]]
*/
```


More information about the Digitalmars-d-learn mailing list