On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote:
>Why we use "chain" while we have "~":
'''D
int[] x=[1,2,3];
int[] y=[4,5,6];
auto z=chain(x,y);
auto j=x~y;
'''
Chain doesn't allocate any memory. This can be useful occasionally.
Thread overview | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
|
August 07, 2022 "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Why we use "chain" while we have "~": '''D int[] x=[1,2,3]; int[] y=[4,5,6]; auto z=chain(x,y); auto j=x~y; ''' |
August 07, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote: >Why we use "chain" while we have "~": '''D auto z=chain(x,y); Chain doesn't allocate any memory. This can be useful occasionally. |
August 07, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote: >Why we use "chain" while we have "~": '''D auto z=chain(x,y); They are quite different:
If you use
Here is some example code to show the differences:
output:
|
August 07, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Emanuele Torre | On Sunday, 7 August 2022 at 03:55:50 UTC, Emanuele Torre wrote: >On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote: >[...] They are quite different:
[...] I was wondering why they in D repeating ways for the same function, but I know now I'm wrong, "chain" isn't like "~". |
August 08, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Emanuele Torre | On Sunday, 7 August 2022 at 03:55:50 UTC, Emanuele Torre wrote: >On Sunday, 7 August 2022 at 01:22:18 UTC, pascal111 wrote: >[...] They are quite different:
[...] In next program, I used "insertInPlace", not "~" nor "chain", should I use "~" or it's the same as "insertInPlace"? |
August 08, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On Monday, 8 August 2022 at 01:05:40 UTC, pascal111 wrote: >In next program, I used "insertInPlace", not "~" nor "chain", should I use "~" or it's the same as "insertInPlace"? As you may noticed, |
August 08, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to pascal111 | On 8/6/22 18:22, pascal111 wrote: > Why we use "chain" while we have "~": > > '''D > int[] x=[1,2,3]; > int[] y=[4,5,6]; > > auto z=chain(x,y); > auto j=x~y; > ''' To add to what has already mentioned, - chain can be used on ranges that are of different element types - as usual, some of the ranges may be generators - although obscure, one may sort in-place over multiple ranges (requires RandomAccessRange.) This program shows the first two points: import std; // Apologies for terseness void main() { auto ints = [ 10, 3, 7 ]; auto squares = iota(10).map!squared.take(5); auto doubles = [ 1.5, -2.5 ]; auto c = chain(ints, squares, doubles); // Different types but CommonType is 'double' here: static assert(is(ElementType!(typeof(c)) == double)); // Prints [10, 3, 7, 0, 1, 4, 9, 16, 1.5, -2.5] writeln(c); } auto squared(T)(T value) { return value * value; } And this one shows how one can sort in-place multiple arrays: import std; // Ditto void main() { auto a = [ 10, 3, 7 ]; auto b = [ 15, -25 ]; auto c = chain(a, b); sort(c); writeln(a); // Prints [-25, 3, 7] writeln(b); // Prints [10, 15] } Ali |
August 08, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to frame | On Monday, 8 August 2022 at 13:26:49 UTC, frame wrote: > On Monday, 8 August 2022 at 01:05:40 UTC, pascal111 wrote: > >> In next program, I used "insertInPlace", not "~" nor "chain", should I use "~" or it's the same as "insertInPlace"? >> >> https://github.com/pascal111-fra/D/blob/main/coco.d > > As you may noticed, `insertInPlace` has another purpose than just appending data. And it will create a new range (to call itself again), moves memory and places the item there, so it's rather inefficient than just appending a single item via "~". I applied "~" in next program: https://github.com/pascal111-fra/D/blob/main/proj06.d |
August 08, 2022 Re: "chain" vs "~" | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Monday, 8 August 2022 at 18:49:26 UTC, Ali Çehreli wrote:
> On 8/6/22 18:22, pascal111 wrote:
> > [...]
>
> To add to what has already mentioned,
>
> - chain can be used on ranges that are of different element types
>
> [...]
Thanks for explanation!
|