Completing C code with D style

Siarhei Siamashka siarhei.siamashka at gmail.com
Wed Nov 3 00:50:51 UTC 2021


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 programmers care in 
> horizontal thinking style. Is there any additions I can make in 
> the code to make it suitable with D style or it's fine?

By "vertical" vs. "horizontal" thinking, do you mean imperative 
vs. functional style?
https://en.wikipedia.org/wiki/Functional_programming#Imperative_vs._functional_programming

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:

     import std.stdio, std.algorithm, std.conv, std.string;

     void main()
     {
       auto numbers = [-3, 14, 47, -49, -30, 15, 4, -82, 99, 26];
       char negativity, even;

       write("Would you like in list (n=negatives, p=positives, 
b=both)? ");
       readf(" %c", &negativity);

       write("Would you like in list (e=evens, o=odds, b=both)? ");
       readf(" %c", &even);

       numbers.filter!(x => !((negativity == 'n' && x > 0) ||
                              (negativity == 'p' && x < 0)))
              .filter!(x => !((even == 'e' && (x % 2)) ||
                              (even == 'o' && !(x % 2))))
              .map!text.join("\n").writeln;
     }



More information about the Digitalmars-d-learn mailing list