digitalmars.D - Concatenation of array and range
- Samuel Redding (15/15) Jan 19 Hello there,
- Sergey (11/14) Jan 19 ```d
- Paul Backus (21/36) Jan 19 The exact D equivalent of your Python code would be to use
- user1234 (8/23) Jan 19 iota is a lazy range. As shown in the other answer you can yield
- Samuel Redding (2/21) Jan 19 Thanks a lot to all of you!
- torhu (6/13) Jan 26 For the record, you can extend a list using a range object in
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
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
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
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
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:Thanks a lot to all of you!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 [...]
Jan 19
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