digitalmars.D.learn - what is D's idiom of Python's list.extend(another_list)?
- mw (7/7) Jun 20 2021 i.e append an array of elements into another array:
- Mike Parker (4/11) Jun 20 2021 ```d
- mw (3/17) Jun 20 2021 Ha! great. I didn't know `~` works for both single elements and
- Mike Parker (4/6) Jun 21 2021 `~` by itself is the concatenation operator and only works with
- ag0aep6g (8/15) Jun 21 2021 `~` works just fine with single elements:
- Mike Parker (3/10) Jun 21 2021 Cool. I've had it in my head for many years now that this was not
- =?UTF-8?Q?Ali_=c3=87ehreli?= (27/37) Jun 21 2021 There is also std.range.chain, which can visit multiple ranges in=20
i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) ``` Thanks.
Jun 20 2021
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]) ``` Thanks.```d x ~= [4, 5]; ```
Jun 20 2021
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:Ha! great. I didn't know `~` works for both single elements and array!i.e append an array of elements into another array: ```Python x = [1, 2, 3] x.extend([4, 5]) ``` Thanks.```d x ~= [4, 5]; ```
Jun 20 2021
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.
Jun 21 2021
On 21.06.21 09:02, Mike Parker wrote:On Monday, 21 June 2021 at 06:16:15 UTC, mw 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] */ }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.
Jun 21 2021
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.
Jun 21 2021
On 6/20/21 10:36 PM, mw wrote:i.e append an array of elements into another array: =20 =20 ```Python x =3D [1, 2, 3] x.extend([4, 5]) ``` =20 Thanks.There is also std.range.chain, which can visit multiple ranges in=20 sequence without copying elements. This is a lifesaver when the arrays=20 are very large. import std.range; import std.algorithm; void main() { auto a =3D [ 1, 2, 3 ]; auto b =3D [ 4, 5 ]; auto expected =3D iota(1, 6); assert(chain(a, b).equal(expected)); } Ranges can be very useful e.g. to sort elements of different random=20 access ranges: import std.range; import std.algorithm; void main() { auto a =3D [ 5, 1, 3 ]; auto b =3D [ 4, 2 ]; auto expected =3D iota(1, 6); // This time we sort: assert(chain(a, b).sort.equal(expected)); // What? :) assert(a =3D=3D [ 1, 2, 3]); assert(b =3D=3D [ 4, 5 ]); } Ali
Jun 21 2021