Re: Completing C code with D style

2021-11-15 Thread pascal111 via Digitalmars-d-learn
On Sunday, 14 November 2021 at 04:29:53 UTC, forkit wrote: On Saturday, 13 November 2021 at 23:02:15 UTC, pascal111 wrote: I touch that D is big language, it's not small like standard C. This will cost me much studying, so I think I need slow down and learn it step by step. Yes. C is so

Re: Completing C code with D style

2021-11-13 Thread forkit via Digitalmars-d-learn
On Saturday, 13 November 2021 at 23:02:15 UTC, pascal111 wrote: I touch that D is big language, it's not small like standard C. This will cost me much studying, so I think I need slow down and learn it step by step. Yes. C is so much smaller, and thus simpler (till you wanna do something

Re: Completing C code with D style

2021-11-13 Thread pascal111 via Digitalmars-d-learn
On Thursday, 11 November 2021 at 22:30:12 UTC, forkit wrote: On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: [...] ok.. for a more on topic response.. First: Please name your variables sensibly: char negativity, even; // grrr!!! char answer1, answer2; // makes so

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 19:34:42 UTC, Stanislav Blinov wrote: Original C code from the first post can only fail on I/O, which is arguably out of your control. And the meat of it amounts to 10 conditional stores. Your implementations, in both C and D, are a very, very far distance

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Friday, 12 November 2021 at 01:05:15 UTC, Stanislav Blinov wrote: On Thursday, 11 November 2021 at 22:10:04 UTC, forkit wrote: It's called 'staged learning'. Staged learning is the only way for humans to learn, due to the limitations of the human cognitive system. Specifically, the way

Re: Completing C code with D style

2021-11-11 Thread foxit via Digitalmars-d-learn
On Friday, 12 November 2021 at 01:05:15 UTC, Stanislav Blinov wrote: On Thursday, 11 November 2021 at 22:10:04 UTC, forkit wrote: It's called 'staged learning'. Staged learning is the only way for humans to learn, due to the limitations of the human cognitive system. Specifically, the way

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 22:10:04 UTC, forkit wrote: It's called 'staged learning'. Staged learning is the only way for humans to learn, due to the limitations of the human cognitive system. Specifically, the way short-term memory and long-term memory facilitate learning. Those who

Re: Completing C code with D style

2021-11-11 Thread foxit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 23:41:48 UTC, kdevel wrote: On Thursday, 11 November 2021 at 22:30:12 UTC, forkit wrote: [...] for(int i = 0; i < sizeof(numbers) / sizeof(int); ++i) // is so much safer - in C style I made it even safer: for (int i = 0; i < sizeof numbers / sizeof

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 21:56:19 UTC, Ali Çehreli wrote: On 11/11/21 11:34 AM, Stanislav Blinov wrote: > Pessimization, though, is laughably easy, and > should be avoided at all costs. I am not passionate about this topic at all and I am here mostly because I have fun in this forum.

Re: Completing C code with D style

2021-11-11 Thread kdevel via Digitalmars-d-learn
On Thursday, 11 November 2021 at 22:30:12 UTC, forkit wrote: [...] for(int i = 0; i < sizeof(numbers) / sizeof(int); ++i) // is so much safer - in C style I made it even safer: for (int i = 0; i < sizeof numbers / sizeof *numbers; ++i) Maybe the type of numbers is changed in the

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D

Re: Completing C code with D style

2021-11-11 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 21:13:03 UTC, Stanislav Blinov wrote: On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: It depends on what you're doing. In the OP's example, yeah worrying about allocations is totally blowing things out of proportions. But that's the thing.

Re: Completing C code with D style

2021-11-11 Thread Ali Çehreli via Digitalmars-d-learn
On 11/11/21 11:34 AM, Stanislav Blinov wrote: > Pessimization, though, is laughably easy, and > should be avoided at all costs. I am not passionate about this topic at all and I am here mostly because I have fun in this forum. So, I am fine in general. However, I don't agree that

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: It depends on what you're doing. In the OP's example, yeah worrying about allocations is totally blowing things out of proportions. But that's the thing. How would one ever learn to know where that dividing line is if all the

Re: Completing C code with D style

2021-11-11 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 23:15:09 UTC, forkit wrote: On Wednesday, 10 November 2021 at 22:17:48 UTC, russhy wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations

Re: Completing C code with D style

2021-11-11 Thread H. S. Teoh via Digitalmars-d-learn
On Thu, Nov 11, 2021 at 01:09:26AM +, forkit via Digitalmars-d-learn wrote: > On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: > > On Wed, Nov 10, 2021 at 11:39:40PM +, forkit via Digitalmars-d-learn > > wrote: [...] > > > I still remember compiling code on my 286x86 ...

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Thursday, 11 November 2021 at 00:11:07 UTC, H. S. Teoh wrote: On Wed, Nov 10, 2021 at 11:39:40PM +, forkit via Digitalmars-d-learn wrote: [...] I still remember compiling code on my 286x86 ... talk about low memory..whoaaah. ... But if you're in a time-constrained inner loop, you do

Re: Completing C code with D style

2021-11-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 10, 2021 at 11:39:40PM +, forkit via Digitalmars-d-learn wrote: [...] > I still remember compiling code on my 286x86 ... talk about low > memory..whoaaah. That's nothing! ;-) When I was a kid, I programmed a computer that had only 48K of RAM (that's 48 *kilo*bytes, not

Re: Completing C code with D style

2021-11-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 10, 2021 at 03:13:08PM -0800, Ali Çehreli via Digitalmars-d-learn wrote: > On 11/10/21 3:05 PM, H. S. Teoh wrote: > > > I cannot compile even simple programs on a low-memory system because > > the compiler runs out of memory > > Did the -lowmem switch help in some cases? On my

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 23:05:06 UTC, H. S. Teoh wrote: On Wed, Nov 10, 2021 at 10:17:48PM +, russhy via Digitalmars-d-learn wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: > btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. > So I really don't

Re: Completing C code with D style

2021-11-10 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 22:17:48 UTC, russhy wrote: On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations .. not one little bit ;-) Having the right mindset helps

Re: Completing C code with D style

2021-11-10 Thread Ali Çehreli via Digitalmars-d-learn
On 11/10/21 3:05 PM, H. S. Teoh wrote: > I cannot compile > even simple programs on a low-memory system because the compiler runs > out of memory Did the -lowmem switch help in some cases? Is -betterC any better? ;) Ali

Re: Completing C code with D style

2021-11-10 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 10, 2021 at 10:17:48PM +, russhy via Digitalmars-d-learn wrote: > On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: > > btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I > > really don't give a damn about allocations .. not one little bit ;-) > > It's

Re: Completing C code with D style

2021-11-10 Thread russhy via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations .. not one little bit ;-) It's not a good mindset to have Give room for the unexpected, don't burn all of your options

Re: Completing C code with D style

2021-11-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 06:47:32 UTC, forkit wrote: btw. My pc has 24GB of main memory, and my CPU 8MB L3 cache. So I really don't give a damn about allocations .. not one little bit ;-) That's not the point. The point is the program is doing unnecessary non-trivial work while

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 04:54:58 UTC, Stanislav Blinov wrote: On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Wednesday, 10 November 2021 at 04:54:58 UTC, Stanislav Blinov wrote: On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of

Re: Completing C code with D style

2021-11-09 Thread Stanislav Blinov via Digitalmars-d-learn
On Tuesday, 9 November 2021 at 11:03:09 UTC, forkit wrote: They both produce exactly the same output. But do vastly different things. But I tell ya.. the cognitive load .. well.. it increased dramatically ;-) Of course it did. Cuz you overthunk it. Dramatically. Your D version allocates

Re: Completing C code with D style

2021-11-09 Thread forkit via Digitalmars-d-learn
On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote: On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it

Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn
On Monday, 8 November 2021 at 12:04:26 UTC, forkit wrote: case 'o' : result = result.filter!(a => (a % 2 == 1)).array; oops. case 'o' : result = result.filter!(a => (a % 2 != 0)).array;

Re: Completing C code with D style

2021-11-08 Thread forkit via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D

Re: Completing C code with D style

2021-11-06 Thread kdevel via Digitalmars-d-learn
On Saturday, 6 November 2021 at 13:48:59 UTC, jfondren wrote: On Saturday, 6 November 2021 at 13:27:55 UTC, kdevel wrote: On Thursday, 4 November 2021 at 00:53:11 UTC, jfondren wrote: On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote: Keeping things simple helps debugging! As

Re: Completing C code with D style

2021-11-06 Thread jfondren via Digitalmars-d-learn
On Saturday, 6 November 2021 at 13:27:55 UTC, kdevel wrote: On Thursday, 4 November 2021 at 00:53:11 UTC, jfondren wrote: On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote: Keeping things simple helps debugging! I'd still have to run your program to be sure of its simple logic,

Re: Completing C code with D style

2021-11-06 Thread kdevel via Digitalmars-d-learn
On Thursday, 4 November 2021 at 00:53:11 UTC, jfondren wrote: On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote: Keeping things simple helps debugging! I'd still have to run your program to be sure of its simple logic, though. The real star d feature that would help with debugging

Re: Completing C code with D style

2021-11-03 Thread jfondren via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote: Keeping things simple helps debugging! I'd still have to run your program to be sure of its simple logic, though. The real star d feature that would help with debugging is unittest: ```d enum sign { negatives = 'n', positives =

Re: Completing C code with D style

2021-11-03 Thread Ali Çehreli via Digitalmars-d-learn
On 11/3/21 11:41 AM, Stanislav Blinov wrote: > And Ali... associative arrays? For this? What are you trying to teach > the good beginner here? :D Obviously, we're all enjoying this thread but once the OP made it clear that they wanted a C-like solution, I took some liberty with alternative

Re: Completing C code with D style

2021-11-03 Thread Salih Dincer via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 20:36:08 UTC, russhy wrote: I don't understand why you guys offer OP such complicate/bloated examples, it'll only make things confusing and slow down compilation time with templates and imports, this is not needed at all I don't like complicated things

Re: Completing C code with D style

2021-11-03 Thread russhy via Digitalmars-d-learn
The code "as is" is perfectly fine I don't understand why you guys offer OP such complicate/bloated examples, it'll only make things confusing and slow down compilation time with templates and imports, this is not needed at all One change, use .length property instead of the hardcoded

Re: Completing C code with D style

2021-11-03 Thread Stanislav Blinov via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 00:50:51 UTC, Siarhei Siamashka wrote: !text.join("\n").writeln; Ahem... You've turned a program that does not allocate to a program that allocates who knows how much memory? And Ali... associative arrays? For this? What are you trying to teach the

Re: Completing C code with D style

2021-11-02 Thread pascal111 via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 00:57:38 UTC, Ali Çehreli wrote: On 11/2/21 5:50 PM, Siarhei Siamashka wrote: Your code can be changed to something like this: And I over-engineered it. :) import std.stdio; import std.algorithm; import std.range; import std.exception; import std.format; . .

Re: Completing C code with D style

2021-11-02 Thread pascal111 via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 00:50:51 UTC, Siarhei Siamashka wrote: On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: By "vertical" vs. "horizontal" thinking, do you mean imperative vs. functional style?

Re: Completing C code with D style

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn
On 11/2/21 5:50 PM, Siarhei Siamashka wrote: Your code can be changed to something like this: And I over-engineered it. :) import std.stdio; import std.algorithm; import std.range; import std.exception; import std.format; // A readable name; corresponds to C's typedef alias FilterFunction =

Re: Completing C code with D style

2021-11-02 Thread pascal111 via Digitalmars-d-learn
On Wednesday, 3 November 2021 at 00:50:51 UTC, Siarhei Siamashka wrote: On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: It's supported in many modern programming languages and it's not a unique feature of the D language alone. Your code can be changed to something like this:

Re: Completing C code with D style

2021-11-02 Thread Siarhei Siamashka via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 23:45:39 UTC, pascal111 wrote: Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D

Completing C code with D style

2021-11-02 Thread pascal111 via Digitalmars-d-learn
Next code originally was a classic C code I've written, it's pure vertical thinking, now, I converted it successfully to D code, but I think I made no much changes to make it has more horizontal thinking style that it seems D programmers care in horizontal thinking style. Is there any