Hipreme's #2 Tip of the day - Reducing .di files dependency

2022-10-23 Thread Hipreme via Digitalmars-d-learn
For reducing a D Interface file dependency when generating it 
with the `-H` flag for DMD, you can't import a module on the top 
level.

Take a look at that example:
```d
module a;
import std.stdio;
void printSomething(string a)
{
   writeln(a);
}
```

If you generate the .di interface file while using dmd, it will 
pretty much generate:


```d
module a;
import std.stdio;
void printSomething(string a);
```
Using the import inside the function scope, it will make the work 
easier for dmd:

```d
module a;
void printSomething(string a)
{
import std.stdio;
writeln(a);
}
```

Will actually generate only:
```d
module a;
void printSomething(string a);
```

This will greatly reduce the number of import and dependencies 
you need if you ever need to distribute a library.


Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread matheus via Digitalmars-d-learn

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.




Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread matheus via Digitalmars-d-learn

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.


Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread Paul Backus via Digitalmars-d-learn

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


Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread Sergey via Digitalmars-d-learn

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.


Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread matheus via Digitalmars-d-learn

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.


Re: Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread H. S. Teoh via Digitalmars-d-learn
On Sun, Oct 23, 2022 at 01:32:44PM +, 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


Design question regarding saving changes in the original array and/or returning a new set

2022-10-23 Thread matheus via Digitalmars-d-learn

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.