www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Concatenation of array and range

reply Samuel Redding <samred mail.com> writes:
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.
Jan 19
next sibling parent Sergey <kornburn yandex.ru> writes:
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.
```d 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] } ```
Jan 19
prev sibling next sibling parent Paul Backus <snarwin gmail.com> writes:
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)`).
Jan 19
prev sibling next sibling parent reply user1234 <user1234 12.de> writes:
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: ```d auto list2 = std.range.chain(list1,iota(a,b)); ``` with list2 being fully lazy.
Jan 19
parent Samuel Redding <samred mail.com> writes:
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!
Jan 19
prev sibling parent torhu <torhu yahoo.com> writes:
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))
 """
For the record, you can extend a list using a range object in Python: list1.extend(range(a, b)) or if you just want to iterate over them in order: iter = itertools.chain(list1, range(a, b))
Jan 26