Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
October 23, 2022 Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Hi, I have a design question and I'd like to hear some advice. Let's say that I want to create a method to sort an array: arr.sort(asc); I think usually this would usually return a new set of that array but now sorted. But If I want to do this in the original, I think I would do this: arr.sort(asc).save(); The problem with this, it would create a new set and assign/copy back to the caller, which would be a waste. So I thought about this: arr.save.sort(asc); Then save would tell to "sort()" what to do beforehand, like passing some argument saying that the sort should be done direct in the caller and no copy. Is this (The latter) an approach you would use? Or there is a better way to do this. By the way in this design I'd like to have both options, return a new set and/or change the original. Thanks in advance, Matheus. |
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | On Sun, Oct 23, 2022 at 01:32:44PM +0000, matheus via Digitalmars-d-learn wrote: > Hi, > > I have a design question and I'd like to hear some advice. Let's say that I want to create a method to sort an array: > > arr.sort(asc); > > I think usually this would usually return a new set of that array but now sorted. No, it does not. It sorts `arr` in-place. > But If I want to do this in the original, I think I would do this: > > arr.sort(asc).save(); That's not necessary, the .save is completely redundant. [...] > By the way in this design I'd like to have both options, return a new set and/or change the original. If you want a new sorted array without touching the original, do this instead: auto sortedArr = arr.dup.sort(); T -- They say that "guns don't kill people, people kill people." Well I think the gun helps. If you just stood there and yelled BANG, I don't think you'd kill too many people. -- Eddie Izzard, Dressed to Kill |
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | Hi H. S. Teoh, I think you misunderstood my question, since English is not my first language maybe this was a problem from my part, but anyway, I'm not talking about "sort" from main library. This example was if I had designed my "own version". Matheus. |
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | On Sunday, 23 October 2022 at 15:47:27 UTC, matheus wrote:
> Hi H. S. Teoh,
>
> I think you misunderstood my question, since English is not my first language maybe this was a problem from my part, but anyway, I'm not talking about "sort" from main library.
>
> This example was if I had designed my "own version".
>
> Matheus.
Hi, Matheus. I'm not an expert in programming :)
But I believe it should be up to you. How you make your function.
If you will use "ref" in the parameter - you could operates exactly the same memory which will received in the parameter;
Also there are 'in','out', 'inout' parameters that could help with management on that question.
|
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to matheus | On Sunday, 23 October 2022 at 13:32:44 UTC, matheus wrote:
> I have a design question and I'd like to hear some advice. Let's say that I want to create a method to sort an array:
>
> arr.sort(asc);
>
> I think usually this would usually return a new set of that array but now sorted.
>
> But If I want to do this in the original, I think I would do this:
>
> arr.sort(asc).save();
>
> The problem with this, it would create a new set and assign/copy back to the caller, which would be a waste. So I thought about this:
>
> arr.save.sort(asc);
>
> Then save would tell to "sort()" what to do beforehand, like passing some argument saying that the sort should be done direct in the caller and no copy.
>
> Is this (The latter) an approach you would use? Or there is a better way to do this.
You say your idea is "like passing some argument", so why not actually pass an argument?
For example:
enum Direction { asc, desc }
enum InPlace : bool { no, yes }
ref Arr sort(ref Arr arr, Direction dir, InPlace inPlace = InPlace.no)
{
if (inPlace) {
arr.sortInPlace(dir);
return arr;
} else {
return arr.sortedCopy(dir);
}
}
Usage would look like this:
auto sorted = arr.sort(asc); // sorted copy
arr.sort(desc, InPlace.yes); // sort in place
|
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Sergey | On Sunday, 23 October 2022 at 16:16:55 UTC, Sergey wrote:
> On Sunday, 23 October 2022 at 15:47:27 UTC, matheus wrote:
>> Hi H. S. Teoh,
>>
>> I think you misunderstood my question, since English is not my first language maybe this was a problem from my part, but anyway, I'm not talking about "sort" from main library.
>>
>> This example was if I had designed my "own version".
>>
>> Matheus.
>
> Hi, Matheus. I'm not an expert in programming :)
> But I believe it should be up to you. How you make your function...
Hi, yes I know but I'd like to know what is the most "common way" of doing this. I think I'll use the library as example, except that I think that I'd do otherwise, calling "sort()" would give a duplicate, and chaining with "save()" or "inplace()" would sort the caller.
Thanks,
Matheus.
|
October 23, 2022 Re: Design question regarding saving changes in the original array and/or returning a new set | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul Backus | On Sunday, 23 October 2022 at 17:36:25 UTC, Paul Backus wrote:
> On Sunday, 23 October 2022 at 13:32:44 UTC, matheus wrote:
>> ...
>
> You say your idea is "like passing some argument", so why not actually pass an argument?
>
> For example:
> ...
Hi, thanks for the example, and yes I'd like to do that, but I'm looking for this "chaining" things which seems to be some sort of pattern these days.
Like I said to Sergey, I think I'll use the library as example, except that I think that I'd do the otherwise, calling just "sort()" would give a duplicate, and chaining with "save()", "inplace()" or whatever name is would sort in place (Caller).
Thanks,
Matheus.
|
Copyright © 1999-2021 by the D Language Foundation