Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 June 2021 at 08:40:47 UTC, ag0aep6g wrote:



`~` works just fine with single elements:

void main()
{
import std.stdio;
int[] a = [2, 3, 4];
writeln(1 ~ a ~ 5); /* [1, 2, 3, 4, 5] */
}


Cool. I've had it in my head for many years now that this was not 
a thing.


Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Ali Çehreli via Digitalmars-d-learn

On 6/20/21 10:36 PM, mw wrote:

i.e append an array of elements into another array:


```Python
x = [1, 2, 3]
x.extend([4, 5])
print(x)  # [1, 2, 3, 4, 5]
```

Thanks.


There is also std.range.chain, which can visit multiple ranges in 
sequence without copying elements. This is a lifesaver when the arrays 
are very large.


import std.range;
import std.algorithm;

void main() {
  auto a = [ 1, 2, 3 ];
  auto b = [ 4, 5 ];
  auto expected = iota(1, 6);
  assert(chain(a, b).equal(expected));
}

Ranges can be very useful e.g. to sort elements of different random 
access ranges:


import std.range;
import std.algorithm;

void main() {
  auto a = [ 5, 1, 3 ];
  auto b = [ 4, 2 ];
  auto expected = iota(1, 6);

  // This time we sort:
  assert(chain(a, b).sort.equal(expected));

  // What? :)
  assert(a == [ 1, 2, 3]);
  assert(b == [ 4, 5 ]);
}

Ali



Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread ag0aep6g via Digitalmars-d-learn

On 21.06.21 09:02, Mike Parker wrote:

On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote:


Ha! great. I didn't know `~` works for both single elements and array!


`~` by itself is the concatenation operator and only works with two 
array operands. `~=` is the append operator and can append arrays or 
single elements.


`~` works just fine with single elements:

void main()
{
import std.stdio;
int[] a = [2, 3, 4];
writeln(1 ~ a ~ 5); /* [1, 2, 3, 4, 5] */
}


Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 June 2021 at 06:16:15 UTC, mw wrote:

Ha! great. I didn't know `~` works for both single elements and 
array!


`~` by itself is the concatenation operator and only works with 
two array operands. `~=` is the append operator and can append 
arrays or single elements.


Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread mw via Digitalmars-d-learn

On Monday, 21 June 2021 at 06:04:56 UTC, Mike Parker wrote:

On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote:

i.e append an array of elements into another array:


```Python
x = [1, 2, 3]
x.extend([4, 5])
print(x)  # [1, 2, 3, 4, 5]
```

Thanks.


```d
x ~= [4, 5];
```



Ha! great. I didn't know `~` works for both single elements and 
array!


Re: what is D's idiom of Python's list.extend(another_list)?

2021-06-21 Thread Mike Parker via Digitalmars-d-learn

On Monday, 21 June 2021 at 05:36:36 UTC, mw wrote:

i.e append an array of elements into another array:


```Python
x = [1, 2, 3]
x.extend([4, 5])
print(x)  # [1, 2, 3, 4, 5]
```

Thanks.


```d
x ~= [4, 5];
```


what is D's idiom of Python's list.extend(another_list)?

2021-06-20 Thread mw via Digitalmars-d-learn

i.e append an array of elements into another array:


```Python
x = [1, 2, 3]
x.extend([4, 5])
print(x)  # [1, 2, 3, 4, 5]
```

Thanks.