Thread overview
Concatenation of array and range
4 days ago
Samuel Redding
4 days ago
Sergey
4 days ago
Paul Backus
4 days ago
user1234
4 days ago
Samuel Redding
4 days ago

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

4 days ago

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

import std;

void main() {
    ulong[] list1 = [1, 2, 3];
    ulong a = 7;
    ulong b = 10;
	list1 ~= iota(a,b).array;
    writeln(list1); // [1, 2, 3, 7, 8, 9]
}
4 days ago

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

The exact D equivalent of your Python code would be to use std.array.array to create an array with the range's elements. For example:

import std.range : iota;
import std.array : array;

ulong[] list1 = [1, 2, 3];
list1 ~= iota(4UL, 10UL).array;

There is also another way to do this which avoids allocating (and immediately discarding) a temporary array:

import std.range : iota;
import std.array : appender;
import std.algorithm : copy;

ulong[] list1 = [1, 2, 3];
copy(iota(4UL, 10UL), appender(&list1));

In this code:

  • appender(&list1) creates an output range which appends each element inserted into it to list1
  • copy takes each element from an input range (in this case, iota(4UL, 10UL)) and inserts it into an output range (in this case, appender(&list1)).
4 days ago

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems to extend "list1" and then iterate over the range and insert the elements.

iota is a lazy range. As shown in the other answer you can yield the lazy thing using .array. But assuming it's ok to create a distinctive iterator you can also chain the two things:

auto list2 = std.range.chain(list1,iota(a,b));

with list2 being fully lazy.

4 days ago

On Sunday, 19 January 2025 at 19:35:31 UTC, user1234 wrote:

>

On Sunday, 19 January 2025 at 18:57:51 UTC, Samuel Redding wrote:

>

Hello there,

is it possible to concat an array and a range? In Python one would have
"""
list1 = [1, 2, 3]
list1 += array.array('L', range(a, b))
"""

The equivalent in D should be something like
"""
ulong[] list1 = [1, 2, 3];
list1 ~= iota(a, b);
"""

I haven't found a way to do this. The only way to do it seems [...]

Thanks a lot to all of you!