Re: Installing DMD on linux via snap

2022-05-18 Thread matheus via Digitalmars-d-learn

On Wednesday, 18 May 2022 at 15:27:57 UTC, rikki cattermole wrote:

Snap package source: https://github.com/dlang-snaps/dmd.snap/

Hasn't been updated in 3 years.


I see... and even that I found my answer elsewhere, this problem 
was already discussed there: 
https://github.com/dlang-snaps/dmd.snap/issues (GCC isn't part of 
the dependencies).


But there was no further progress.

Matheus.


Installing DMD on linux via snap

2022-05-18 Thread matheus via Digitalmars-d-learn

Hi,

Even my problem is already solved, I'm passing this information 
because I don't know if you are aware.


Yesterday I needed to install DMD on a fresh installed version of 
Linux, so since I was using Xubuntu I decided to use snap.


sudo snap install dmd

Then a warning appeared saying that because some permissions I 
should add --classic, OK I added and the installation proceeded.


So I just ran DMD alone and I got the version and some info, but 
when I tried to compile a simple Hello world to check if 
everything was really fine, I was greeted with this:


cc: No such file or directory
--- errorlevel 255

Looking online I found my answer, this was probably because it 
was missing GCC, then I did:


apt-get install build-essential

And after that it worked, but I wonder... couldn't we get a hint 
about this after or even before installing DMD?


I remember installing other applications and being asked to 
install additional libraries/packages. Couldn't we have the same?


Matheus.


Re: Question on shapes

2022-05-16 Thread matheus via Digitalmars-d-learn

On Tuesday, 17 May 2022 at 04:37:58 UTC, Ali Çehreli wrote:

...
2) If you want to have a shape hierarchy, then you can start by 
defining its interface and implement that interface by concrete 
shape types. Drawing is ordinarily handled by member functions:

...


Hi Ali, I'm not the author but I have a question, in your second 
example, let's say that sometimes it would be required to "draw" 
with some scale factor, so (As a newbie) I would do something 
like this:


interface Shape {
  void draw();
  void draw(float scale);
}

Then in Circle class:

  void draw(float scale) {
writeln("This circle's radius is ", radius*scale);
  }
  void draw(){ draw(1); }

Then in Rectangular class:

  void draw(float scale) {
writefln!"This rectangle's dimensions are 
%sx%s."(width*scale,height*scale);

  }
  void draw(){ draw(1); }


So calling shape.draw() would draw with the original scale, 
otherwise you could call as shape.draw(some_scale);


The problem is these are just 2 shapes and it could be much more, 
so it would required to repeat all this.


In D there would be a better way to do such thing?

Thanks,

Matheus.


Re: What are (were) the most difficult parts of D?

2022-05-11 Thread matheus via Digitalmars-d-learn

On Wednesday, 11 May 2022 at 05:41:35 UTC, Ali Çehreli wrote:
What are you stuck at? What was the most difficult features to 
understand? etc.


To make it more meaningful, what is your experience with other 
languages?


Ali


I don't know if this will be helpful but here it goes, my user 
case is different since I use D as C on steroids.


My first language was C and I have projects in this language 
which I still maintain and my brain is used to it, but depending 
the project I'll use D instead of C to use features like: GC, AA, 
Strings, Modules and maybe Classes if I feel like it, but I don't 
go to far from these and one reason is sometimes in a rich 
language with lots of features like D It gets hard to stay up to 
date.


Now I know they must be useful to many others, but one thing that 
I don't like are the Attributes part, for me there are so many 
and this is a bit overwhelming (Again to me), for example need to 
think about: "@safe, @trusted, @system" while still prototyping 
may be a cumbersome, and if it's giving me trouble I'll just get 
rid of it. :)


Again I should sit and read the documentation or your book again 
but it's hard to keep up-to-date sometimes with work and so on.


By the way I used to program in C# at work too, but I stopped on 
whatever was the version that runs with .Net 4.6, again this is 
another case of language changing or adding so many features that 
I decided not to go on with it, since it's not required where I 
work (Occasionally I write a few batch programs here and there), 
and I'm current doing more database business at the company these 
days,


Sorry for any English mistakes,

Matheus.


Re: Library for image editing and text insertion

2022-04-27 Thread matheus via Digitalmars-d-learn

On Wednesday, 27 April 2022 at 00:03:25 UTC, Adam Ruppe wrote:

...


I know about Adam Ruppe's work, I already used his terminal.d, 
but I think that unfortunately most people don't and I think it 
should be announced more in these parts. For me arsd is for D 
what stb is for C.


I think in the first time running any D compiler, it should blink 
in the terminal in Yellow/Pink or whatever color you like, and 
show some info like: Are you starting a new project any need some 
libs? Do you know about arsd? If not then go to: 
https://github.com/adamdruppe/arsd. :)


matheus.


Re: How to remove all characters from a string, except the integers?

2022-03-04 Thread matheus via Digitalmars-d-learn

On Friday, 4 March 2022 at 21:20:20 UTC, Stanislav Blinov wrote:

On Friday, 4 March 2022 at 19:51:44 UTC, matheus wrote:

OK but there is another problem, I tested your version and 
mine and there is a HUGE difference in speed:



string s, str = "4A0B1de!2C9~6";


Unless I did something wrong (If anything please tell). By the 
way on DMD was worse, it was like 5x slower in your version.


To add to the already-mentioned difference in allocation 
strategies, try replacing the input with e.g. a command-line 
argument. Looping over a literal may be skewing the results.


Interesting and I'll try that way. Thanks.

Matheus.


Re: How to remove all characters from a string, except the integers?

2022-03-04 Thread matheus via Digitalmars-d-learn

On Friday, 4 March 2022 at 20:38:11 UTC, ag0aep6g wrote:

...
The second version involves auto-decoding, which isn't actually 
needed. You can work around it with 
`str.byCodeUnit.filter!...`. On my machine, times become the 
same then.


Typical output:

str: 401296
Tim(ms): 138
Tim(us): 138505

str: 401296
Tim(ms): 137
Tim(us): 137376


That's awesome my timing are pretty much like yours. In fact now 
with "byCodeUnit"  it's faster. :)


Thanks,

Matheus.


Re: How to remove all characters from a string, except the integers?

2022-03-04 Thread matheus via Digitalmars-d-learn

On Friday, 4 March 2022 at 20:33:08 UTC, H. S. Teoh wrote:

On Fri, Mar 04, 2022 at 07:51:44PM +, matheus via ...
I don't pay any attention to DMD when I'm doing anything 
remotely performance-related. Its optimizer is known to be 
suboptimal. :-P


Yes, in fact I usually do my coding/compiling with DMD because is 
faster, then I go for LDC for production and speed.


Matheus.


Re: How to remove all characters from a string, except the integers?

2022-03-04 Thread matheus via Digitalmars-d-learn

On Thursday, 3 March 2022 at 23:46:49 UTC, H. S. Teoh wrote:

...
This version doesn't even allocate extra storage for the 
filtered digits, since no storage is actually needed (each 
digit is spooled directly to the output).


OK but there is another problem, I tested your version and mine 
and there is a HUGE difference in speed:


LDC 1.27.1, with -O2:

import std.datetime.stopwatch;
import std.stdio: write, writeln, writef, writefln;
import std;

void printStrTim(string s,StopWatch sw){
writeln("\nstr: ", s
,"\nTim(ms): ", sw.peek.total!"msecs"
,"\nTim(us): ", sw.peek.total!"usecs"
   );
}

void main(){
auto sw = StopWatch(AutoStart.no);
string s, str = "4A0B1de!2C9~6";
int j;

sw.start();
for(j=0;j<1_000_000;++j){
s="";
foreach(i;str){
(i >= '0' && i <= '9') ? s~=i : null;
}
}
sw.stop();
printStrTim(s,sw);

s = "";
sw.reset();
sw.start();
for(j=0;j<1_000_000;++j){
s="";
s = str.filter!(ch => ch.isDigit).to!string;
}
sw.stop();
printStrTim(s,sw);
}

Prints:

str: 401296
Tim(ms): 306
Tim(us): 306653

str: 401296
Tim(ms): 1112
Tim(us): 1112648

---

Unless I did something wrong (If anything please tell). By the 
way on DMD was worse, it was like 5x slower in your version.


Matheus.


Re: How to remove all characters from a string, except the integers?

2022-03-03 Thread matheus via Digitalmars-d-learn

On Thursday, 3 March 2022 at 21:03:40 UTC, H. S. Teoh wrote:

...

--
void main() {
string s = "blahblah123blehbleh456bluhbluh";
auto result = s.filter!(ch => ch.isDigit).to!int;
assert(result == 123456);
}
--

Problem solved.  Why write 6 lines when 3 will do?


Just because I'm a simple man. :)

I usually program mostly in C and when in D, I go in the same way 
but using features like: GC, strings, AA etc.


Of course your version is a D'ish way of handling things, and I 
can't contest it looks better visually. But if size was problem I 
could have written:


void main(){
string s, str = "4A0B1de!2C9~6";
foreach(i;str){
(i >= '0' && i <= '9') ? s~=i : null;
}
writeln(s);
}

Well still 1 line off, but I goes with my flow. I mean this 
example is a simple one, but usually I can see and understand 
what a code in C is doing (more) easily than D just looking at 
it. Don't even ask about C++, because I gave up. :)


Matheus.

PS: I spotted something on your code, you're converting the 
result to int, this can lead to a overflow depending the values 
in the string.


Re: How to remove all characters from a string, except the integers?

2022-03-03 Thread matheus via Digitalmars-d-learn

On Thursday, 3 March 2022 at 12:14:13 UTC, BoQsc wrote:
I've looked around and it seems using regex is the only closest 
solution.


I'm a simple man who uses D with the old C mentality:

import std.stdio;

void main(){
string s, str = "4A0B1de!2C9~6";
foreach(i;str){
if(i < '0' || i > '9'){ continue; }
s ~= i;
}
writeln("Result: ", s);
}

Result: 401296

Matheus.


Re: https://run.dlang.io/ vs All dmd compilers (2.060 - latest)

2022-02-28 Thread Matheus via Digitalmars-d-learn

On Monday, 28 February 2022 at 19:00:58 UTC, Matheus wrote:

On Monday, 28 February 2022 at 17:49:36 UTC, Mike Parker wrote:

...
Please try again.


Testing.

Matheus.


It worked thanks!

Matheus.


Re: https://run.dlang.io/ vs All dmd compilers (2.060 - latest)

2022-02-28 Thread Matheus via Digitalmars-d-learn

On Monday, 28 February 2022 at 17:49:36 UTC, Mike Parker wrote:

...
Please try again.


Testing.

Matheus.




Re: https://run.dlang.io/ vs All dmd compilers (2.060 - latest)

2022-02-28 Thread Matheus via Digitalmars-d-learn

On Monday, 28 February 2022 at 02:31:57 UTC, Mike Parker wrote:

...


Hey Parker, I think my IP still under surveillance, everytime I 
post I get:


"Your message has been saved, and will be posted after being 
approved by a moderator."


With VPN I can post without problem. Could you please take a look?

Matheus.


Re: https://run.dlang.io/ vs All dmd compilers (2.060 - latest)

2022-02-28 Thread Matheus via Digitalmars-d-learn

On Monday, 28 February 2022 at 08:11:15 UTC, Basile B. wrote:
This was [reported before]. Apparently this would be caused by 
a timeout.


[reported before]: 
https://forum.dlang.org/post/skc2dd$1o52$1...@digitalmars.com


Apparently yes, but I think the return error should be clear to 
avoid guessing.


I've been trying this for 2 weeks, I think there is something 
wrong there.


Matheus.


https://run.dlang.io/ vs All dmd compilers (2.060 - latest)

2022-02-27 Thread Matheus via Digitalmars-d-learn

Hi,

In "https://run.dlang.io; is the "All dmd compilers (2.060 - 
latest)" not working anymore?


Because I always get: "Server error:"

I've been trying for like 2 weeks and I always get this "Server 
Error: " message.


I even tried with this basic example:

void main(){
import std.algorithm, std.stdio;
"Starting program".writeln;
enum a = [ 3, 1, 2, 4, 0 ];
static immutable b = sort(a);
pragma(msg, "Finished compilation: ", b);
}

After one minute I think I get:


rdmd playground.d


Server error:

Thanks,

Matheus.


Re: how to handle very large array?

2022-02-14 Thread Matheus via Digitalmars-d-learn

On Monday, 14 February 2022 at 13:20:45 UTC, MichaelBi wrote:
thanks, you are all correct. i just change the algorithm and 
use the AA, previously using the naïve method...:), now solved 
perfectly. thanks again.


You could have used a normal Int Array for this task too, you're 
dealing with numbers and could go with array of 9 elements 0..8, 
then move data circularly and adding new lives as hit -1.


I pretty sure there is a Mathematical way to solve this, I mean 
without any Arrays, but I didn't bother about it since that even 
my basic JavaScript implementation runs in less than 1ms for 256 
days in an old computer.


Matheus.


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread Matheus via Digitalmars-d-learn

On Saturday, 11 December 2021 at 01:02:36 UTC, frame wrote:

...
You probably want this:

```d
int j;
for({int i=0; j=0;} i<10; ++i){}
```

Beware, this syntax comes directly from hell


Well this works! :)

I'm just a bit intrigued by your last sentence. Is there anything 
evil this may result or anything that I should be aware of?


Matheus.


Re: Wouldn't the compiler be smart with this shadowing variable?

2021-12-11 Thread Matheus via Digitalmars-d-learn
On Friday, 10 December 2021 at 21:55:17 UTC, Siarhei Siamashka 
wrote:

...
   2. reuse the existing "j" variable.


Yes.

But only one of them is a correct description of what actually 
happens when the compiler processes this code. So it's a good 
thing that the compiler is smart enough to reject ambiguous 
code and prevent humans from making mistakes.


Hmm I see your point. And I just thought in cases like this the 
reuse should be the way, but I can see that this is a small 
snippet and in a big code this may hurt someone.


Matheus.


Wouldn't the compiler be smart with this shadowing variable?

2021-12-10 Thread Matheus via Digitalmars-d-learn

Hi,

Wouldn't the compiler be smart with this shadowing variable, 
example:


void main(){
int j;
for(int i=0,j=0;i<10;++i){}
return;
}

onlineapp.d(3): Error: variable `j` is shadowing variable 
`onlineapp.main.j`


So in the "for loop" shouldn't "i" be declared and "j" just be 
assigned to 0?


I mean this is a bit weird, imagine I have different "for loops" 
and in this situation I'll need to do this:


j=0;
for(int i=0;i<10;++i){}

Otherwise:

for(i=0,j=0;i<10;++i){}

This works, but now "i" variable will be out of the "for loop" 
scope.


Matheus.


Re: How to loop through characters of a string in D language?

2021-12-10 Thread Matheus via Digitalmars-d-learn

On Wednesday, 8 December 2021 at 11:23:45 UTC, BoQsc wrote:

...
The character I want to skip: `;`


My C way of thinking while using D:

import std;

string stripsemicolons(string input){
char[] s = input.dup;
int j=0;
for(int i=0;i

Re: Without multiples inheritance, how is this done?

2021-05-08 Thread matheus via Digitalmars-d-learn

On Saturday, 8 May 2021 at 18:33:35 UTC, Jack wrote:

...
but the class ExtendFoo and ExtendedBaa  must inherit from Foo 
and Baa, respectively. But how can I make it inherit the 
routines from DRY class too without multiples inheritance? in 
C++ I'd just do:


class ExtendedFoo : DRY, Base { /* ... */ }
class ExtendBaa : DRY, Base { /* ... */ }


What about this model:

class baseFoo{
   // Implement foo stuff
}

class baseFooBar : baseFoo{
   // Implement bar stuff
}

class FooBar : baseFooBar{
   // Do your FooBar thing!
}

Matheus.


Re: Need for speed

2021-04-01 Thread matheus via Digitalmars-d-learn

On Thursday, 1 April 2021 at 19:00:08 UTC, Berni44 wrote:


Try using ldc2 instead of dmd:

```
ldc2 -O3 -release -boundscheck=off -flto=full 
-defaultlib=phobos2-ldc-lto,druntime-ldc-lto speed.d

```

should produce much better results.


Since this is a "Learn" part of the Foruam, be careful with 
"-boundscheck=off".


I mean for this little snippet is OK, but for a other projects 
this my be wrong, and as it says here: 
https://dlang.org/dmd-windows.html#switch-boundscheck


"This option should be used with caution and as a last resort to 
improve performance. Confirm turning off @safe bounds checks is 
worthwhile by benchmarking."


Matheus.


Re: D's Continous Changing

2021-03-03 Thread matheus via Digitalmars-d-learn

On Thursday, 4 March 2021 at 05:44:53 UTC, harakim wrote:

...


Yes it's a problem indeed. I had the same problem and that's 
worse when you don't upgrade very often.


But let me tell something, where I work we have software in C#, 
do you think that upgrading was smoothly with all the tools that 
Microsoft provides?


No it wasn't, and it gets worse with third party components.

So this guy was hired just for that, port a very old code to the 
new framework, and after a month he did, yes it compiled 
alright... but the software didn't work as expected is some 
cases, some controls wasn't acting right and was very unreliable.


Guess what? They are still developing with old framework until 
everything works correctly on the new framework.


Matheus.


Development: Work vs Lazy Programmers... How do you keep sanity?

2020-12-03 Thread matheus via Digitalmars-d-learn

Hi,

I didn't know where to post this and I hope this is a good place.

I'm a lurker in this community and I read a lot of discussions on 
this forum and I think there a lot of smart people around here.


So I'd like to know if any of you work with Lazy or even Dumb 
programmers, and If yes how do you keep your sanity?


Matheus.

PS: Really I'm almost losing mine.


Re: What is the difference between enum and shared immutable?

2020-10-28 Thread matheus via Digitalmars-d-learn

On Thursday, 29 October 2020 at 01:26:38 UTC, Mike Parker wrote:


enum int[] arr = [1,2,3];
someFunc(arr);

This is identical to

someFunc([1,2,3]);

Manifest constants have no address. They are effectively 
aliases for their values.


Hi Mike,

I really didn't know about this.

Thanks for the information,

Matheus.


Re: What is the difference between enum and shared immutable?

2020-10-28 Thread matheus via Digitalmars-d-learn

On Wednesday, 28 October 2020 at 22:07:06 UTC, H. S. Teoh wrote:
... (This is why it's a bad idea to use enum with an array 
literal, because every time it's referenced you get a new copy 
of the array.)

...


Could you please give an example (Snippet) about this?

Matheus.


Re: Is there something I'm not getting here?

2020-10-26 Thread matheus via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 02:21:39 UTC, matheus wrote:
On Tuesday, 27 October 2020 at 01:26:56 UTC, James Blachly 
wrote:

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides 
that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


It works for me.
...


I think he is referring to this:
...


I mean he may be having a duplicate method in the same class.

Matheus.


Re: Is there something I'm not getting here?

2020-10-26 Thread matheus via Digitalmars-d-learn

On Tuesday, 27 October 2020 at 01:26:56 UTC, James Blachly wrote:

On 10/26/20 9:19 PM, Ruby The Roobster wrote:
Following code doesn't work(it's not the actual code but it 
represents it). Is there some rule about function overrides 
that I don't know about?



...
The error I keep getting no matter what says: Error: Multiple 
Overrides of Same Function. Anybody know what I should do?


It works for me.
...


I think he is referring to this:

import std;

class B{
public override string toString(){
return null;
}

public override string toString(){
return toString(null);
}
}

void main() {
B b = new P();
}

Error: function `B.toString` multiple overrides of same function

You can view: https://www.jdoodle.com/iembed/v0/3rm

Matheus.


Re: Question about: ("1.1").to!int;

2020-10-24 Thread matheus via Digitalmars-d-learn
On Saturday, 24 October 2020 at 04:04:18 UTC, Виталий Фадеев 
wrote:

On Friday, 23 October 2020 at 16:59:06 UTC, matheus wrote:
On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton 
Wakeling wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
Well since the caller is handling a string, shouldn't the 
caller verify the content before any conversion?


What if:
"1.1.1".to!int
?

The algorithm will become more complicated?
Will the speed decrease?


I don't get...

Anyway this should fail, look "1.1" is one thing, "1.1.1" is 
another thing as "1.a" is another thing.


Like I said before I think the behavior should be the same in 
these cases:


(1.1).to!int = 1.
("1.1").to!int = Current is an error and IMO should be 1.

Now for your question:

1.1.1.to!int = Error: found `0.1` when expecting `,`.
"1.1.1".to!int = Should be the same as above.

Matheus.


Re: Question about: ("1.1").to!int;

2020-10-23 Thread matheus via Digitalmars-d-learn
On Friday, 23 October 2020 at 13:57:41 UTC, Joseph Rushton 
Wakeling wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:
Since (1.1).to!int = 1, shouldn't the string value 
("1.1").to!int at least try to convert to float/double and 
then to int?


The thing is, that's a great way for hard-to-identify bugs to 
creep into code.  In these cases:


auto a = (1).to!int; // this works
auto b = ("1").to!int;   // this works
auto c = (1.1).to!int;   // this works and c = 1

... then what the programmer wants is unambiguous.  In the 
first case it's just converting int => int.  In the second, 
it's converting from a string that unambiguously represents an 
integer value, to an int.  And in the third, it's converting 
_at programmer request_ from a double to an int (which has a 
well-defined behaviour).


However, if ("1.1").to!int were to work, this would be the `to` 
function making a judgement call on how to handle something 
ambiguous.  And while that judgement call may be acceptable for 
your current use-case, it won't be for others.


I got it everything you said, but like a said previously:

(1.1).to!int vs ("1.1").to!int

One is a decimal literal while the other is a string 
representation of a decimal.


To be honest I think the function is already making a judgment 
call when I do (1.1).to!int and returns 1, I really fail to see 
the difference when is ("1.1").to!int.


I agree with user1234: "The third case is just like `cast(int) 
1.1` it's not _at programmer request_ from my point of view, it's 
just that the `to` template has not be more restrictive than the 
D `cast` expression. `to` should do at least what a `cast` do and 
do more when there's no rule for the two types that are involved."


In particular, if `to` just accepted any string numerical 
representation for conversion to int, how could the caller 
explicitly _exclude_ non-integer input, if that is their 
use-case?


Well since the caller is handling a string, shouldn't the caller 
verify the content before any conversion?


Because a string may contain a integer, decimal representation or 
neither one.


Finally I don't want to make a fuss of it, I just thought it was 
a bit weird but it can be solved easily.


Thanks,

Matheus.


Re: Question about: ("1.1").to!int;

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

On Friday, 23 October 2020 at 08:09:13 UTC, Виталий Фадеев wrote:

On Wednesday, 21 October 2020 at 22:50:27 UTC, matheus wrote:

Hi,

import std.stdio, std.conv;
void main(string[ ] args) {
auto a = (1).to!int; // this works
auto b = ("1").to!int;   // this works
auto c = (1.1).to!int;   // this works and c = 1
auto d = ("1.1").to!int; // Doesn't work
}

[...]


1.1 is not int.
"to" works fine.

As solution,... "1.1" should be splitted to lexems: "1", ".", 
"1". Then analyze and then converted to int.


Of course 1.1 it's not an integer, but since (1.1).to!int works I 
thought that ("1.1").to!int should work too.


Matheus.


Question about: ("1.1").to!int;

2020-10-21 Thread matheus via Digitalmars-d-learn

Hi,

import std.stdio, std.conv;
void main(string[ ] args) {
auto a = (1).to!int; // this works
auto b = ("1").to!int;   // this works
auto c = (1.1).to!int;   // this works and c = 1
auto d = ("1.1").to!int; // Doesn't work
}

The forth line gives me:

std.conv.ConvException@/usr/include/dlang/dmd/std/conv.d(1898): 
Unexpected '.' when converting from type string to type int


??:? pure @safe int std.conv.toImpl!(int, 
immutable(char)[]).toImpl(immutable(char)[]) [0x55de76d9b4d7]
??:? pure @safe int 
std.conv.to!(int).to!(immutable(char)[]).to(immutable(char)[]) 
[0x55de76d99a17]

??:? _Dmain [0x55de76d9986e]

Question:

Since (1.1).to!int = 1, shouldn't the string value ("1.1").to!int 
at least try to convert to float/double and then to int?


Because for what I see "to!int" converts from: int,real but in 
case of string only when it is a integer representation?


Matheus.


Re: Skipping or Stepping Through an Array?

2020-10-21 Thread matheus via Digitalmars-d-learn

On Wednesday, 21 October 2020 at 12:06:00 UTC, drug wrote:

There are two other way:
...
// using foreach
foreach (i; 0..a.length)
write(a[i], ", ");
...


Yes you can use foreach, but in this case will not act the way 
the OP wanted. In his for loop example the "i" is incremented by 
2: "i+=2".


So to perform what OP want with foreach it should be:

foreach (i,j;a){
if(i%2==0){ write(j, ", ");}
}

By the way it's possible to set a "step" value for "foreach"?

Matheus.


Re: Compiler is calling `_memset64` in betterC

2020-10-19 Thread matheus via Digitalmars-d-learn
On Sunday, 18 October 2020 at 19:24:28 UTC, Ferhat Kurtulmuş 
wrote:
I plan to start a project in reasonable size, I wonder if I 
should really use betterC... if I encounter a bug like this, 
will I be stuck at it?


The bug report says, it is a dmd specific problem, and LDC, my 
favorite d compiler, works well (tried it).


So the "first party" compiler has a bug while the "third party" 
one works. That's weird.


I would expect the other way around.

Matheus.


Re: Working with pointers/adresses

2020-07-09 Thread matheus via Digitalmars-d-learn

On Thursday, 9 July 2020 at 17:24:33 UTC, matheus wrote:

On Wednesday, 8 July 2020 at 20:33:39 UTC, Paul Backus wrote:

import std.stdio;

void main()
{
int i;
readf("%d\n", i); // read a number
ubyte* p = cast(ubyte*) i; // convert it to a pointer
writeln(*p); // write the data at that address to the 
console

}

Note that this program will almost certainly crash if you try 
to run it, since modern computers do not allow programs to 
read and write arbitrary memory locations.


I wonder if the question was more like intended to display the 
value using pointers, ie:


import std.stdio;

void main(){
int i;
readf("%d\n", i);
int *p = 
writeln(*p);
}

Because accessing arbitrary memory location seems very weird.

Matheus.


Or maybe he wanted to do this:

import std.stdio;

void main(){
int i;
readf("%d\n", i);
ulong address = cast(ulong)
ubyte* p = cast(ubyte*)address;
writeln(*p);
}

Matheus.


Re: Working with pointers/adresses

2020-07-09 Thread matheus via Digitalmars-d-learn

On Wednesday, 8 July 2020 at 20:33:39 UTC, Paul Backus wrote:

import std.stdio;

void main()
{
int i;
readf("%d\n", i); // read a number
ubyte* p = cast(ubyte*) i; // convert it to a pointer
writeln(*p); // write the data at that address to the 
console

}

Note that this program will almost certainly crash if you try 
to run it, since modern computers do not allow programs to read 
and write arbitrary memory locations.


I wonder if the question was more like intended to display the 
value using pointers, ie:


import std.stdio;

void main(){
int i;
readf("%d\n", i);
int *p = 
writeln(*p);
}

Because accessing arbitrary memory location seems very weird.

Matheus.




Re: Privatize a few members to allow messing with them #11353

2020-06-30 Thread matheus via Digitalmars-d-learn

On Tuesday, 30 June 2020 at 20:01:43 UTC, Stanislav Blinov wrote:

On Tuesday, 30 June 2020 at 19:58:05 UTC, matheus wrote:


+loc.linnum = loc.linnum + incrementLoc;

This works because it was declared:

void linnum(uint rhs) { _linnum = rhs; }

Right?


Almost. Given these definitions:

@safe @nogc pure @property
{
const uint linnum() { return _linnum; }
void linnum(uint rhs) { _linnum = rhs; }
}

This:

loc.linnum = loc.linnum + incrementLoc;

is rewritten as:

loc.linnum(loc.linnum() + incrementLoc);


Alright and thanks again.

Matheus.


Re: Privatize a few members to allow messing with them #11353

2020-06-30 Thread matheus via Digitalmars-d-learn

On Tuesday, 30 June 2020 at 19:55:56 UTC, matheus wrote:

On Tuesday, 30 June 2020 at 19:46:35 UTC, Stanislav Blinov

...
@safe @nogc pure @property
{
const uint linnum() { return _linnum; }
const uint charnum() { return _charnum; }
void linnum(uint rhs) { _linnum = rhs; }
void charnum(uint rhs) { _charnum = rhs; }
}

...with which the += won't work (at least this variant, as the 
getter isn't returning ref).


Oh I see now and thanks for the information.


By the way:

+loc.linnum = loc.linnum + incrementLoc;

This works because it was declared:

void linnum(uint rhs) { _linnum = rhs; }

Right?

Matheus.


Re: Privatize a few members to allow messing with them #11353

2020-06-30 Thread matheus via Digitalmars-d-learn

On Tuesday, 30 June 2020 at 19:46:35 UTC, Stanislav Blinov wrote:

On Tuesday, 30 June 2020 at 19:42:57 UTC, matheus wrote:

in this case this was more a style thing than anything else 
right? Or is there something I'm not able to see?


Before the change, linnum and charnum are public variables, one 
can do a += on them. After the change, they become properties 
accessing, as the PR says, private variables:


@safe @nogc pure @property
{
const uint linnum() { return _linnum; }
const uint charnum() { return _charnum; }
void linnum(uint rhs) { _linnum = rhs; }
void charnum(uint rhs) { _charnum = rhs; }
}

...with which the += won't work (at least this variant, as the 
getter isn't returning ref).


Oh I see now and thanks for the information.

Matheus.


Privatize a few members to allow messing with them #11353

2020-06-30 Thread matheus via Digitalmars-d-learn
Hi, I was looking the PR in DMD and I found this one: 
https://github.com/dlang/dmd/pull/11353/


One of the changes was:

-loc.linnum += incrementLoc;
+loc.linnum = loc.linnum + incrementLoc;

I usually do the former and I particularly hate the later, so my 
question is, in this case this was more a style thing than 
anything else right? Or is there something I'm not able to see?


Thanks,

Matheus.


Re: What would be the advantage of using D to port some games?

2020-06-24 Thread matheus via Digitalmars-d-learn

On Wednesday, 24 June 2020 at 19:46:55 UTC, IGotD- wrote:
A previous game implementation in D would be interesting and if 
you do it you are welcome to write your about experiences here. 
It's hard to say what features you would take advantage in D as 
I haven't seen the code in C/C++. However, one thing is clear D 
would be an easy port because it is so close to C/C++. Every 
algorithm can be ported directly without any bigger change. If 
it would Rust you would probably have to rethink some of the 
data structures and it would be more difficult. Another thing 
that is clear is that productivity would be high. With today's 
fast machines and old games you don't have to worry about any 
GC pauses as there is plenty of time for that. You can 
basically use the slow "scripting language" features of D. I 
would expect that D is in the C# ball park in productivity for 
such task.


Thanks for the information. Yes I am thinking about doing it but 
at the same time I wonder if it's worth (technically speaking) 
port those games.


I'll analyze if there's any part that could take the advantage of 
some D features that lacks in C/C++.


Matheus.


Re: What would be the advantage of using D to port some games?

2020-06-24 Thread matheus via Digitalmars-d-learn

On Wednesday, 24 June 2020 at 19:14:48 UTC, IGotD- wrote:

On Wednesday, 24 June 2020 at 18:53:34 UTC, matheus wrote:



What I'd like to know from the experts is: What would be the 
advantage of using D to port such games?




Can you elaborate your question a little bit more. Why would 
you want to port existing game code to another language to 
begin with?


To see how the game could fit/run in D, like people are porting 
some of those games to Rust/Go and so on.



When you mention "advantage", advantage compared to what?


To the original language the game was written. For example taking 
DOOM (Written in C), in D what would be the features that someone 
would use to port this game, like using CTFE to generate SIN/COS 
table would be reasonable?


I'm just looking for a roughly idea of this matter.

Matheus.


What would be the advantage of using D to port some games?

2020-06-24 Thread matheus via Digitalmars-d-learn
Hi, I currently use D for small CLI/Batch apps, before that I 
used to program in C.


Despite of using D I usually program like C but with the 
advantage of: GC, AA, CTFE and a few classes here and there.


As we can see there are a lot of old classic games source 
available like: DOOM, Duke Nukem 3D, Red Alert and most them 
written originally in C/C++.


What I'd like to know from the experts is: What would be the 
advantage of using D to port such games?


Thanks in advance,

Matheus.


Re: How to use this forum ?

2020-05-20 Thread matheus via Digitalmars-d-learn

On Wednesday, 20 May 2020 at 20:49:52 UTC, Vinod K Chandran wrote:

Hi all,
I have some questions about this forum.
1. How to edit a post ?
2. How to edit a reply ?
3. How to add some code(mostly D code) in posts & replies.
4. How to add an image in posts & replies.
5. Is there a feature to mark my post as "[SOLVED]" ?


In few words: This Forum is an interface for mailing lists, you 
can't do most of these things, I mean not as easy like you do in 
most Forums (With database to edit content).


Personally I prefer this way, because I really hate when people 
edit their posts, even for good reasons.


Matheus.


Re: DConf 2017 Videos

2020-04-25 Thread matheus via Digitalmars-d-learn

On Saturday, 25 April 2020 at 11:11:07 UTC, Jacob Carlborg wrote:
I have previously downloaded the DConf videos. I sent them to 
Mike for him to upload.


Thank you very much for this.


Re: DConf 2017 Videos

2020-04-24 Thread matheus via Digitalmars-d-learn
On Friday, 24 April 2020 at 21:11:48 UTC, Steven Schveighoffer 
wrote:
... and whomever controlled the sociomantic youtube account 
took down all the videos...


First of all thanks for replying and... Ouch! After that I hope D 
Foundation learned the lesson and keep the videos themselves 
instead of relying in third party next time.


I think it's time to have a Wayback Machine version for videos.

Thanks.


DConf 2017 Videos

2020-04-24 Thread matheus via Digitalmars-d-learn
Hi, please could someone tell me where can I find videos from 
DConf 2017?


I pretty sure I watched them on Youtube sometime ago, but I can't 
find anymore.


By the way, I'm looking from one video where someone shows some 
"C flaws" and how to D as Better C could solve that.


I think it was the second talk in this list: 
https://dconf.org/2017/schedule/


Any idea?

Thanks.


Re: Swedish letters fuck up parsing into SQL querry

2020-03-24 Thread matheus via Digitalmars-d-learn

On Monday, 23 March 2020 at 15:41:50 UTC, Adam D. Ruppe wrote:

On Monday, 23 March 2020 at 15:15:12 UTC, Anders S wrote:

I'm creating a connection to the db and conn.exec(sql)


It depends on the library but it is almost always easier to do 
it right than to do it the way you are.


like with my lib it is

db.query("update celldata set name = ?", new_name);


I'm not the OP but I have a question, isn't this passive to SQL 
injection too, or your LIB will handle this somehow?


If is the later could you please point the code on GitHub?

Matheus.


Re: How to get child class Type and members from parent class?

2019-11-20 Thread Matheus via Digitalmars-d-learn
On Wednesday, 20 November 2019 at 13:46:07 UTC, Jacob Carlborg 
wrote:
On Wednesday, 20 November 2019 at 10:05:11 UTC, zoujiaqing 
wrote:

import std.stdio;


class A
{
this(T)(T t)
{

}

void write()
{
T _this = cast(T) this;
writeln(this.v);
}
}

class B : A
{
string v = "hello";
}

void main()
{
auto b = new B;

writeln(b.write()); // print hello
}


You can use a template this parameter [1], like this:

import std.stdio;

class A
{
void write(this T)()
{
T self = cast(T) this;
writeln(self.v);
}
}

class B : A
{
string v = "hello";
}

void main()
{
auto b = new B;
b.write();
}

[1] https://dlang.org/spec/template.html#template_this_parameter

--
/Jacob Carlborg


I'm not the OP but a lurker, and this is new to me, I mean in 
your example you're accessing a member "v" which wasn't defined 
in the Parent class.


So if someone creates something like this:

class C : A{
string x = "world";  // x instead of v
}

Not the "x" instead of "v", of course it will only get an 
compiler error if that function is called in by "C" object.


I think this is a powerful and weird feature at the same time, 
because some could write a code like this:


import std.stdio;

class A{
void write(this T)(){
T self = cast(T) this;
writeln(self.v);
}

void write2(this T)(){
T self = cast(T) this;
writeln(self.x);
}
}

class B : A{
string v = "hello";
}

class C : A{
string x = "world";
}

void main(){
auto b = new B;
b.write();

auto c = new C;
c.write2();
}

This is a different way of designing things, do people use this 
often?


Matheus.


Re: Help making a game with transparency

2019-09-28 Thread matheus via Digitalmars-d-learn
Ok, I took a look over my old projects and I found exactly what 
you want, by the way it's from 2012.


It uses Derelict 2.0 bindings and will draw a PNG image where you 
can move around with cursor keys.


If you want I can send you the whole project (Makefile, DLL's) 
and everything else to build it.


Code:

/* 02/10/2012 */
import std.stdio;
import derelict.sdl.sdl;
import derelict.sdl.image;

pragma(lib, "DerelictSDL.lib");
pragma(lib, "DerelictUtil.lib");
pragma(lib, "DerelictSDLImage.lib");

void main(){
// Screen width, height and bit per pixel AKA color
int width = 600, height = 480, bpp = 24;
short xPos, yPos;

// Load Derelict SDL bindings
DerelictSDL.load();
DerelictSDLImage.load();

SDL_Surface* screen;
SDL_Rect rScreen;

// Pointing to an Array of the current key state
Uint8 *keystate = SDL_GetKeyState(null);

// Try initiate SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0){
throw new Exception("Failed: Can not initialize SDL!");
}

	screen = SDL_SetVideoMode(width, height, bpp, SDL_ANYFORMAT);// 
SDL_HWSURFACE);


IMG_Init(IMG_INIT_PNG);
auto img = IMG_Load("caco.png");
if(img is null){
throw new Exception("Image not found!");
}

	writeln(" - w: ",img.w, " - h: ", img.h, " - bpp:", 
img.format.BitsPerPixel);


if (screen is null){
throw new Exception("Failed: Can not set video! ");
}

SDL_WM_SetCaption("Simple Example", null);

xPos = cast(short)(width   / 2 - 128);
yPos = cast(short)(height  / 2 - 128);

// Game loop
while(!keystate[SDLK_ESCAPE]){
SDL_Delay(2); // To not stress CPU

// Fill screen with RED
SDL_FillRect(screen, null, 0xFF);

// Update key state array
SDL_PumpEvents();

// User's control
yPos += keystate[SDLK_DOWN]-keystate[SDLK_UP];
xPos += keystate[SDLK_RIGHT]-keystate[SDLK_LEFT];

// Where to plot the image on the screen
rScreen.x = xPos;
rScreen.y = yPos;
rScreen.w = rScreen.h = 256;

SDL_BlitSurface(img,null,screen,); // Draw Image
SDL_Flip(screen); // Update Screen
}
IMG_Quit();
SDL_FreeSurface(img);
SDL_Quit();
}


Matheus.


Re: Help making a game with transparency

2019-09-27 Thread matheus via Digitalmars-d-learn

On Friday, 27 September 2019 at 21:16:07 UTC, Murilo wrote:

...
Here it is, how do I make the ship have a transparent 
background?


First: Your PNG file has transparency data information right?

Second: I was Looking into the drawImage function (Line 854):

https://github.com/adamdruppe/arsd/blob/b0d21de148ef0b23ea845be322af5e6931ca4cb6/screen.d

And I'd suggest you to try to add the glEnable(GL_ALPHA_TEST); 
before glBegin(GL_QUADS);


In fact you should do this only once, maybe inside the 
constructor, but just try it there to see if it works.


Matheus.


Re: Help making a game with transparency

2019-09-27 Thread matheus--- via Digitalmars-d-learn

On Friday, 27 September 2019 at 16:36:14 UTC, Murilo wrote:

...Do you know the arsd library?


Yes but I use mostly terminal.d and others.

On the other hand I use to code games too using SDL and OpenGL.

I know for example in OpenGL you can do: glEnable(GL_ALPHA_TEST); 
to enable alpha channel and set transparency.


Matheus.


Re: Help making a game with transparency

2019-09-27 Thread matheus via Digitalmars-d-learn

On Friday, 27 September 2019 at 02:54:27 UTC, Murilo wrote:
Hi guys, I am making a game but for some reason the sprites do 
not show with the transparent background that they were 
supposed to. I'm using the arsd library. Can anyone help me?


Sorry this is a bit vague. I suppose you're using engine.d or 
screen.d directly right?


Depending on your setup (OpenGL or Software) transparency will be 
different.


For example take a look at line 733, putpixel function and you'll 
see that It handle Color differently if it's OpenGL x Software 
and for the latter it checks if 32bpp or less.


Now if it's OpenGL take a look for "Alpha".

Matheus.


Re: Private variables accessible from outside class

2019-08-08 Thread matheus via Digitalmars-d-learn

On Thursday, 8 August 2019 at 15:51:45 UTC, Drobet wrote:

...
My question is if this is intended behavior, and if yes, why?


This is true if the class is inside the same module:

"Private means that only members of the enclosing class can 
access the member, or members and functions in the same module as 
the enclosing class. Private members cannot be overridden."[1]


Matheus.

[1]https://wiki.dlang.org/Access_specifiers_and_visibility




Re: Question about ubyte x overflow, any safe way?

2019-08-05 Thread matheus via Digitalmars-d-learn

On Monday, 5 August 2019 at 01:41:06 UTC, Ali Çehreli wrote:

...
Two examples with foreach and ranges. The 'ubyte.max + 1' 
expression is int. The compiler casts to ubyte (because we 
typed ubyte) in the foreach and we cast to ubyte in the range:

...


Maybe it was a bad example of my part (Using for), and indeed 
using foreach would solve that specific issue, but what I'm 
really looking for if there is a flag or a way to check for 
overflow when assigning some variable.


ubyte u = 260;  // Here should be given some warning or throw 
exception.


It's ubyte, but it could be any other data type.

Thanks anyway,

Matheus.



Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn

On Sunday, 4 August 2019 at 18:38:34 UTC, Paul Backus wrote:

...
Use std.experimental.checkedint:

import std.stdio;
import std.experimental.checkedint;

void main()
{
for(Checked!(ubyte, Throw) u = ubyte(250); u < 256; ++u) {
writeln(u.get);
}
}

An exception will be thrown when you attempt to increment u 
above 255.


Unfortunately I'm using DMD 2.072 which doesn't support this, but 
I'll upgrade soon as I can and will check this out.


Thanks,

Matheus.


Re: Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn

On Sunday, 4 August 2019 at 18:15:30 UTC, Max Haughton wrote:
What do you want to do? If you just want to count to 255 then 
use a foreach


This was just an example, what I'd like in this code is either: 
Get an error (exception) when overflow or even an warning (Only 
if "some" flag was active).


If you want to prevent overflow you must either use BigInt or 
wrap ubyte in a struct that doesn't allow overflow


Could you please elaborate about this struct wrapping? Do you 
mean manually check on change?


Matheus.


Question about ubyte x overflow, any safe way?

2019-08-04 Thread matheus via Digitalmars-d-learn

Hi,

The snippet below will produce an "infinite loop" because 
obviously "ubyte u" will overflow after 255:


import std.stdio;
void main(){
ubyte u = 250;
for(;u<256;++u){
writeln(u);
}
}

Question: Is there a way (Flag) to prevent this?

Matheus.


Re: Speed of Random Numbers

2019-08-03 Thread matheus via Digitalmars-d-learn
On Saturday, 3 August 2019 at 16:35:34 UTC, Giovanni Di Maria 
wrote:

For me the "goodness of random" is NOT important.


If that's the case, you could roll your own RNG:

//DMD64 D Compiler 2.072.2
import std.stdio;
import std.datetime;
import std.array, std.random;

void main(){
ubyte x;
auto r = benchmark!(f1,f2)(10_000);
writeln(r[0]);
writeln(r[1]);
}

int f1(){
static s = 10; // Seed
s = (214013*s+2531011); // [1]
s = (s>>16)&0x7FFF;
auto y=(s&7)+1;
//writeln(y);
return y;
}

int f2(){
byte c;
c=uniform!ubyte() % 8 +1;
//writeln(c);
return c;
}


/*
[1] 
https://software.intel.com/en-us/articles/fast-random-number-generator-on-the-intel-pentiumr-4-processor/

*/

/*
OUTPUT:
TickDuration(65263)   <-f1
TickDuration(635167)  <-f2
*/

Matheus.


Re: Help me decide D or C

2019-08-01 Thread matheus via Digitalmars-d-learn

On Thursday, 1 August 2019 at 09:43:20 UTC, Kagamin wrote:

On Wednesday, 31 July 2019 at 22:30:52 UTC, Alexandre wrote:

1) Improve as a programmer
2) Have fun doing programs

Thats it basically. I am planning to study all "free" time I 
have. I am doing basically this since last year.


Try Basic. It has builtin graphics, seeing you program draw is 
quite fascinating.


In that case I'd recommend EvalDraw: 
http://advsys.net/ken/download.htm with a C-like syntax with draw 
things while you type.


Description:

"A complete programming environment with built-in compiler, text 
editor, and functions to allow for quick prototyping. With 
Evaldraw, you can make graphs in any dimension (1D, 2D, 3D), 
animations, custom musical instruments, voxel models, and general 
purpose applications. I've included a lot of examples, so even if 
you're not a programmer, you can look at the demos"


It was written by Ken Silverman (Creator of Build Engine - Duke 
Nukem 3D).


Matheus.


Re: Help me decide D or C

2019-07-31 Thread matheus via Digitalmars-d-learn

On Wednesday, 31 July 2019 at 18:38:02 UTC, Alexandre wrote:

...
Should I go for C and then when I become a better programmer 
change to D?

Should I start with D right now?
...


I think it depend your intent, but right now for a beginner 
between C and D I would go with C, because as you noted there are 
plenty of resources for C, C++, Python etc.


In some colleges where I live, 10+ years ago they used to start 
CS class with C and then C++ or Java, now they start with Python 
and then C and so on.


Python was "more" friendly for beginners to understand 
variable/algorithm, and after that they would go with data types, 
pointers... more easily.


Good luck,

Matheus.


Re: Blog Post #0050: MVC III - ComboBoxText, Add & Remove

2019-07-05 Thread matheus via Digitalmars-d-learn

On Friday, 5 July 2019 at 09:34:08 UTC, Ron Tarrant wrote:
Today is a bit of a milestone for the blog as the 50th regular 
post goes up. Also, the facelift is coming along nicely, the 
next phase of which should be ready to push by July 9th.


And today's topic continues with the MVC series by 
demonstrating how to add and remove items from a ComboBoxText 
widget. You can read it here:


https://gtkdcoding.com/2019/07/05/0050-mvc-iii-comboboxtext-add-remove.html


Maybe you should post this on the Announce group?

And on Reddit too.

Matheus.


Re: create and initialise array

2019-06-19 Thread matheus via Digitalmars-d-learn

On Thursday, 20 June 2019 at 01:06:09 UTC, Alex wrote:

Is there a way of creating and initialising a dynamic array ?

for example I am doing this:

auto arr = new float[];
arr[] = 0.0f;

Profiling indicates that the compiler (gdc) is spending 
significant time memsetting the whole array to something (nan 
?) before I immediately memset it to 0.0f.


It would be good if there was a way of either void initialing 
it so that the first memset is avoided or a way of replacing 
the init value with a different one.


Thanks,
Alex


What about:

//DMD64 D Compiler 2.072.2

import std.stdio;
import std.array;

void main(){
auto s = uninitializedArray!(float[])(100);
s[] = 0.0f;
writeln(s[0]);
}

Matheus.


Re: Reuse/reset dynamic rectangular array?

2019-05-25 Thread matheus via Digitalmars-d-learn

On Saturday, 25 May 2019 at 14:28:24 UTC, Robert M. Münch wrote:
How can I reset a rectangualr array without having to loop 
through it?


int[][] myRectData = new int[][](10,10);

myRectData.length = 0;
myRectData[].length = 0;
myRectData[][].length = 0;  

They all give: slice expression .. is not a modifiable lvalue.


This works form me:

//DMD64 D Compiler 2.072.2
import std.stdio;

void main()
{
auto arr = new int[][](10,10);
writeln(arr.length);
arr.length=0;
writeln(arr.length);
}

output:
10
0

Matheus.


Re: Performance of tables slower than built in?

2019-05-21 Thread matheus via Digitalmars-d-learn

On Wednesday, 22 May 2019 at 00:55:37 UTC, Adam D. Ruppe wrote:

On Wednesday, 22 May 2019 at 00:22:09 UTC, JS wrote:
I am trying to create some fast sin, sinc, and exponential 
routines to speed up some code by using tables... but it seems 
it's slower than the function itself?!?


There's intrinsic cpu instructions for some of those that can 
do the math faster than waiting on memory access.


It is quite likely calculating it is actually faster. Even 
carefully written and optimized tables tend to just have a very 
small win relative to the cpu nowadays.


Exactly, and by the way this is old feature already.

I remember like long time ago when I was studying/writing some 
games and I decided to test pre-computed arrays (LUT) for SIN/COS 
vs functions, and the later would beat the arrays pretty easily.


And by the way when porting old games, sometimes you usually (If 
not change the game logic too much), get rid of the LUT and use 
functions directly.


Matheus.


Re: Struct destructor

2019-03-02 Thread Matheus via Digitalmars-d-learn

On Saturday, 2 March 2019 at 11:32:53 UTC, JN wrote:

...
Is this proper behavior? I'd imagine that when doing 
foos.remove("bar"), Foo goes out of scope and should be 
immediately cleaned up rather than at the end of the scope? Or 
am I misunderstanding how should RAII work?


https://dlang.org/spec/struct.html#struct-destructor

"Destructors are called when an object goes out of scope. Their 
purpose is to free up resources owned by the struct object."


Example:

import std.stdio;

struct S
{
~this()
{
import std.stdio;
writeln("S is being destructed");
}
}


void main(){
{
S s = S();
scope (exit)
{
writeln("Exiting scope");
}
}
writeln("Ending program.");
}


Output:

Exiting scope
S is being destructed
Ending program.

Matheus.



Re: How to cast arrays?

2019-03-01 Thread Matheus via Digitalmars-d-learn

On Saturday, 2 March 2019 at 02:14:01 UTC, Murilo wrote:
How do I cast a ubyte[] into uint[]? It keeps raising an error, 
I have read the documentation saying there are restrictions for 
that concerning the length of the arrays.


By the way here is how:

void foo(){
   ubyte[] x = [1,2];
   auto y = (cast(int*) )[0..2];
   writeln(y);
}

Matheus.


Re: How to cast arrays?

2019-03-01 Thread Matheus via Digitalmars-d-learn

On Saturday, 2 March 2019 at 02:14:01 UTC, Murilo wrote:
How do I cast a ubyte[] into uint[]? It keeps raising an error, 
I have read the documentation saying there are restrictions for 
that concerning the length of the arrays.


https://dlang.org/spec/expression.html#cast_expressions

"Casting a dynamic array to another dynamic array is done only if 
the array lengths multiplied by the element sizes match. The cast 
is done as a type paint, with the array length adjusted to match 
any change in element size. If there's not a match, a runtime 
error is generated."


Matheus.


Re: Modulo that 'wraps' the number?

2019-01-21 Thread Matheus via Digitalmars-d-learn
On Monday, 21 January 2019 at 18:39:27 UTC, Steven Schveighoffer 
wrote:

Not a Python user, just hoping to help answer questions :)


Yes I know in fact I'm not the OP but from what I understood from 
his post, he want to replicate, but I may be wrong.


If it's the case, this code may help him:

//DMD64 D Compiler 2.072.2
import std.stdio;
import std.math;

int mod(int i,int j){
if(abs(j)==abs(i)||!j||!i||abs(j)==1){return 0;};
auto m = (i%j);
return (!m||sgn(i)==sgn(j))?(m):(m+j);
}

void main(){
int j,i;
for(j=1;j<9;++j){
for(i=1;i<9;++i){
  writeln(-i, " % ", +j, " = ", (-i).mod(+j));
  writeln(+i, " % ", -j, " = ", (+i).mod(-j));
}
}
}


At least the code above gave me the same results as the Python 
version.


Matheus.


Re: Modulo that 'wraps' the number?

2019-01-21 Thread Matheus via Digitalmars-d-learn
On Monday, 21 January 2019 at 15:01:27 UTC, Steven Schveighoffer 
wrote:


Probably, this optimizes into better code, but maybe the 
optimizer already does this with the expression above:


auto tmp = n % 3;
if(tmp < 0)
   tmp += 3;

It's just not a nice single expression.

-Steve


I don't think you can do this, imagine this case:

auto tmp = -1 % -3; // Note divisor in negative too.

tmp will be "-1" which already matches the Python way, so you 
can't add divisor anymore.


Matheus.


Re: Is there a nice syntax to achieve optional named parameters?

2019-01-17 Thread Matheus via Digitalmars-d-learn

On Thursday, 17 January 2019 at 16:55:33 UTC, SrMordred wrote:

Yes, but there is a mistake there:
alias is part of the template:

foo(alias x)(){} //note extra parens

than u call like an template:

foo!"a"; //equivalent = foo!("a")();
foo!1;


I see now and thanks.

Matheus.


Re: Is there a nice syntax to achieve optional named parameters?

2019-01-17 Thread Matheus via Digitalmars-d-learn

On Thursday, 17 January 2019 at 01:43:42 UTC, SrMordred wrote:

Let me throw this idea here:
...


I usually do this too, I like to use struct and then in another 
language I use reflection do optimize binding.


Anyway I understood all your code, except for this "alias code"


auto NewWindow( alias code )()
{
mixin("Config config = {"~code~"};");
return Window(config);
}


Looking on specs: https://dlang.org/spec/declaration.html#alias

"AliasDeclarations create a symbol that is an alias for another 
type, and can be used anywhere that other type may appear."


So with your example imagine this:

foo(alias x){}

foo("a");
foo(1);

'x' will be string one time and integer another? Or there is 
something that I'm missing.


Matheus.