Re: curses/ncurses liberary in D

2021-11-02 Thread harakim via Digitalmars-d-learn

On Wednesday, 3 November 2021 at 01:39:02 UTC, H. S. Teoh wrote:
On Wed, Nov 03, 2021 at 01:33:28AM +, dangbinghoo via 
Digitalmars-d-learn wrote:

On Wednesday, 3 November 2021 at 00:50:31 UTC, pascal111 wrote:
> How can I include "ncurses" liberary in D? I'm using Ubuntu 
> and GDC!


Search ncurses in Dub registray shows that there's 3 ncurses D 
bingdings.


https://code.dlang.org/search?q=ncurses


In addition to that, I highly recommend Adam's arsd.terminal as 
an ncurses replacement.  It's not API equivalent, but is much 
more idiomatic D than any ncurses binding would be.



T


https://github.com/adamdruppe/arsd/blob/master/terminal.d


Re: Using "strcpy" to assign value to dynamic char array

2021-11-02 Thread tsbockman via Digitalmars-d-learn

On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:
Joking aside, I liked the nested struct and its opAssign to 
mimic internal `arr.length = 42` syntax. (I know it involves a 
potentially expensive delegate but still...)


The nested struct is not needed. UFCS works for setters, too:

```D
void assumedLength(S)(ref S slice, size_t length) {
if(slice.length >= length)
slice.length = length;
else
assert(false, "Let's not corrupt memory today.");
}

void main() {
  auto arr = [ 1, 2, 3 ];
  arr.assumedLength = 2;
  writeln(arr);
}
```


Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/2/21 6:49 PM, zjh wrote:

On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote:

On 11/2/21 3:57 PM, Ali Çehreli wrote:



  const x = readln.strip;


this is very interesting .`readln` then `strip;`,Very natural.




Yes, UFCS (universal function call syntax) makes code natural, concise, 
and readable (but things can get out of hand :) ).


Here is an example copied from the home page of dlang.org:

import std.stdio, std.array, std.algorithm;

void main()
{
stdin
.byLineCopy
.array
.sort!((a, b) => a > b) // descending order
.each!writeln;
}

Ali



Re: Using "strcpy" to assign value to dynamic char array

2021-11-02 Thread Steven Schveighoffer via Digitalmars-d-learn

On 11/1/21 9:03 PM, pascal111 wrote:

On Monday, 1 November 2021 at 21:32:21 UTC, Ali Çehreli wrote:



Joking aside

...


This function seems smart and flexible and higher than my current level, 
I'll study it.


Please please, do NOT study this code. It is bad all around. Ali should 
know better ;)


He is joking with the code posted, it should not be used in any real 
code ever. Not only is it corrupting memory, it may not work as expected 
in all cases (and will randomly fail).


Please learn first how memory works before writing the low-level bits of 
arrays!


As a beginner, I suggest studying what a buffer overrun is, and why it 
is bad.


-Steve


Re: A problem in converting C code

2021-11-02 Thread zjh via Digitalmars-d-learn

On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote:

On 11/2/21 3:57 PM, Ali Çehreli wrote:



  const x = readln.strip;


this is very interesting .`readln` then `strip;`,Very natural.




Re: curses/ncurses liberary in D

2021-11-02 Thread H. S. Teoh via Digitalmars-d-learn
On Wed, Nov 03, 2021 at 01:33:28AM +, dangbinghoo via Digitalmars-d-learn 
wrote:
> On Wednesday, 3 November 2021 at 00:50:31 UTC, pascal111 wrote:
> > How can I include "ncurses" liberary in D? I'm using Ubuntu and GDC!
> 
> Search ncurses in Dub registray shows that there's 3 ncurses D
> bingdings.
> 
> https://code.dlang.org/search?q=ncurses

In addition to that, I highly recommend Adam's arsd.terminal as an
ncurses replacement.  It's not API equivalent, but is much more
idiomatic D than any ncurses binding would be.


T

-- 
Truth, Sir, is a cow which will give [skeptics] no more milk, and so they are 
gone to milk the bull. -- Sam. Johnson


Re: curses/ncurses liberary in D

2021-11-02 Thread dangbinghoo via Digitalmars-d-learn

On Wednesday, 3 November 2021 at 00:50:31 UTC, pascal111 wrote:
How can I include "ncurses" liberary in D? I'm using Ubuntu and 
GDC!


Search ncurses in Dub registray shows that there's 3 ncurses D 
bingdings.


https://code.dlang.org/search?q=ncurses


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;

.
.
.

Ali


I think I costed you much efforts. Thank you, it's a great 
version.




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?

https://en.wikipedia.org/wiki/Functional_programming#Imperative_vs._functional_programming


Maybe you are right, I think that, but I don't know the exact 
English terms.


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 = bool function(int);

// These are our filters; they will be initialized not right
// where they are defined but in a following 'shared static
// this()'. (This is a current D implementation issue.)
immutable FilterFunction[string] negativityFilters;
immutable FilterFunction[string] evennessFilters;

// 'shared static this()' blocks are executed before main()
// is executed.
shared static this() {
  const matchAll = (int _) { return true; };

  negativityFilters = [
"negatives" : (int i) { return i <= 0; },
"positives" : (int i) { return i >= 0; },
"both" : matchAll,
  ];

  evennessFilters = [
"evens" : (int i) { return (i % 2) == 0; },
"odds" : (int i) { return (i % 2) == 1; },
"both" : matchAll,
  ];
}

// Picks the filter that corresponds to user input
FilterFunction pickFilter(const(FilterFunction[string]) filters) {
  // The full names of filters e.g. [ "evens", "odds", "both" ]
  auto fulls = filters.byKey.array.sort;

  // The first letters of the names e.g. [ 'b', 'e', 'o' ]
  auto shorts = fulls.map!(key => key.front);

  // A mapping from short to full name e.g. 'b' -> "both"
  auto shortToFull = assocArray(shorts, fulls);

  // Prompt the user by combining the short and full names
  writef!"Would you like in list (%-(%s, %))? "(
zip(shorts, fulls).map!(z => format!"%s=%s"(z[0], z[1])));

  char c;
  readf(" %c", );
  enforce(c in shortToFull, format!"'%s' is not a valid option"(c));

  const full = shortToFull[c];
  return filters[full];
}

// Picks filters according to user input
FilterFunction[] pickFilters() {
  return [ negativityFilters, evennessFilters ].map!pickFilter.array;
}

// The main logic of the program
void run() {
  auto numbers = [ -3, 14, 47, -49, -30, 15, 4, -82, 99, 26 ];

  auto filters = pickFilters();
  auto selection = numbers.filter!(n => filters.all!(f => f(n)));

  writefln!"%-(%s\n%)"(selection);
}

int main() {
  try {
run();
return 0;

  } catch (Exception e) {
stderr.writefln!"ERROR: %s"(e.msg);
return 1;
  }
}

Ali


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:


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", );

  write("Would you like in list (e=evens, o=odds, b=both)? 
");

  readf(" %c", );

  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;
}


Wow! your code seem so nice, I like it although I don't know how 
exactly it works. For now I'll keep learning traditional 
imperative programming style then after that I'll look in the 
functional one, it's new to me.


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 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", );

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

  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;
}



curses/ncurses liberary in D

2021-11-02 Thread pascal111 via Digitalmars-d-learn
How can I include "ncurses" liberary in D? I'm using Ubuntu and 
GDC!


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 additions I can make in the code to 
make it suitable with D style or it's fine?


// D programming language

import std.stdio;

int main()
{


int numbers[10]=[-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", );


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


for(int i=0; i<10; ++i)
{
 if(negativity=='n')
{
if(numbers[i]>0)
continue;
}
 else{
  if(negativity=='p')
if(numbers[i]<0)
continue;
  }

  if(even=='e')
{
if(numbers[i]%2)
continue;
}
 else{
  if(even=='o')
if(!(numbers[i]%2))
continue;
  } 
 writefln("%d",numbers[i]);
}


return 0;
}


Re: A problem in converting C code

2021-11-02 Thread pascal111 via Digitalmars-d-learn

On Tuesday, 2 November 2021 at 23:02:42 UTC, Ali Çehreli wrote:

On 11/2/21 3:57 PM, Ali Çehreli wrote:

Here is a more idiomatic version:

import std.stdio;
import std.string;

void main() {
  const x = strip(readln());
  writeln(x);

  if (x == "hello world!") {
writeln("yes");
  }
}

The first line in main can be written with UFCS syntax as well:

  const x = readln.strip;

Ali


This is D point of view to the same classic C code. I think D has 
the spirit of C++.


Re: A problem in converting C code

2021-11-02 Thread pascal111 via Digitalmars-d-learn

On Tuesday, 2 November 2021 at 22:57:14 UTC, Ali Çehreli wrote:

On 11/2/21 3:36 PM, pascal111 wrote:

can we keep the C style of it as it is


As you hint, this really is not D but still... :) I had to make 
three changes:


import std.stdio;
// Ali - Importing stdin under a different name
// to prevent name conflict with std.stdio.stdin;
import core.stdc.stdio : c_stdin = stdin;
import core.stdc.string;

int main()
{


  // Ali - Changed the type to char[20]
  char[20] x;

  // Ali - The last parameter is the stream
  // to read from.
  fgets([0],x.sizeof,stdin.getFP());

  x[strcspn([0],"\n")]=0;

  writeln(x);

  if(!strcmp([0],"hello world!"))
  {

writeln("yes");

  }

  return 0;
}

Ali


Yes, exactly, this is literal converting as most possibility.


Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/2/21 3:57 PM, Ali Çehreli wrote:

Here is a more idiomatic version:

import std.stdio;
import std.string;

void main() {
  const x = strip(readln());
  writeln(x);

  if (x == "hello world!") {
writeln("yes");
  }
}

The first line in main can be written with UFCS syntax as well:

  const x = readln.strip;

Ali



Re: A problem in converting C code

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/2/21 3:36 PM, pascal111 wrote:

can we keep the C style of it as it is


As you hint, this really is not D but still... :) I had to make three 
changes:


import std.stdio;
// Ali - Importing stdin under a different name
// to prevent name conflict with std.stdio.stdin;
import core.stdc.stdio : c_stdin = stdin;
import core.stdc.string;

int main()
{


  // Ali - Changed the type to char[20]
  char[20] x;

  // Ali - The last parameter is the stream
  // to read from.
  fgets([0],x.sizeof,stdin.getFP());

  x[strcspn([0],"\n")]=0;

  writeln(x);

  if(!strcmp([0],"hello world!"))
  {

writeln("yes");

  }

  return 0;
}

Ali


A problem in converting C code

2021-11-02 Thread pascal111 via Digitalmars-d-learn
In next program I intend it to be the D version of a C program, 
but I found some troubles with it, can we keep the C style of it 
as it is as we can and fix the must-be-fixed parts to make it 
works under D compiler?


// D programming language

import std.stdio;
import core.stdc.stdio;
import core.stdc.string;

int main()
{


char x[20];

fgets([0],x.sizeof,null);

x[strcspn([0],"\n")]=0;

writeln(x);

if(!strcmp([0],"hello world!"))
{

writeln("yes");

}

return 0;
}


Re: Are there anything like leetcode.com but that supports D?

2021-11-02 Thread Dr Machine Code via Digitalmars-d-learn

On Friday, 29 October 2021 at 17:29:57 UTC, harakim wrote:
On Sunday, 24 October 2021 at 05:46:48 UTC, Dr Machine Code 
wrote:
I'd like that to some friends getting start with programming. 
Sadly that platform doesn't support D.


I wouldn't mind helping out by reviewing code or answering 
questions if they get stuck. My email is my username at 
gmail.com. I'm not a great D developer, but I've been doing 
development for 17 years and I started casually using D about 2 
years later. Let me know if I can help!


That's very kind of you! :) I'll let them know that


Re: Average function using Variadic Functions method

2021-11-02 Thread Ali Çehreli via Digitalmars-d-learn

On 11/2/21 9:35 AM, pascal111 wrote:

> "input..." seems nice, where can I get more information about it?

I include most of the language in this free book:

  http://ddili.org/ders/d.en/index.html

which has an Index Section that I find useful to locate information:

  http://ddili.org/ders/d.en/ix.html

I searched for '...' in that page and was happy that one of the 
candidates was "..., function parameter" which took me here:





Note: I've heard before that the links are broken but it works for me. 
(I enclosed the link with <> to help with it.)


Ali



Re: Average function using Variadic Functions method

2021-11-02 Thread Mike Parker via Digitalmars-d-learn

On Tuesday, 2 November 2021 at 16:35:40 UTC, pascal111 wrote:



"input..." seems nice, where can I get more information about 
it?


"Typesafe variadic functions"

https://dlang.org/spec/function.html#typesafe_variadic_functions



Re: Average function using Variadic Functions method

2021-11-02 Thread pascal111 via Digitalmars-d-learn
On Tuesday, 2 November 2021 at 16:29:07 UTC, rikki cattermole 
wrote:

You probably don't want to be using C variadics.

Instead try the typed one:

float mean(float[] input...) {
// you don't want to divide by zero
if (input.length == 0)
return 0;

float temp = 0;
// floats and double initialize to NaN by default, not zero.

foreach(value; input) {
temp += value;
}

return temp / input.length;
}


"input..." seems nice, where can I get more information about it?


Re: Average function using Variadic Functions method

2021-11-02 Thread rikki cattermole via Digitalmars-d-learn

You probably don't want to be using C variadics.

Instead try the typed one:

float mean(float[] input...) {
// you don't want to divide by zero
if (input.length == 0)
return 0;

float temp = 0;
// floats and double initialize to NaN by default, not zero.

foreach(value; input) {
temp += value;
}

return temp / input.length;
}


Average function using Variadic Functions method

2021-11-02 Thread pascal111 via Digitalmars-d-learn
I'm programming average calculating function by using Variadic 
Functions method, but I didn't get yet what is the wrong in my 
code:


// D programming language

import std.stdio;
import core.vararg;
import std.conv;

float foo(...)
{

float x=0;

for(int i=0; i<_arguments.length; ++i){

x+=to!float(_arguments[i]);

}

x/=_arguments.length;

return x;

}

int main()
{


writeln(foo(2,3));

return 0;

}


Re: __cpuid like in C

2021-11-02 Thread WebFreak001 via Digitalmars-d-learn

On Monday, 1 November 2021 at 16:16:12 UTC, Arsium wrote:

On Monday, 1 November 2021 at 16:02:20 UTC, Paul Backus wrote:

On Monday, 1 November 2021 at 16:00:05 UTC, Arsium wrote:

Hello,

Currently, I'm working to implement myself WinAPI functions. 
However, I could not find anything matching with  : __cpuid 
like : https://gist.github.com/boxmein/7d8e5fae7febafc5851e


Any idea ?


Not sure if it's exactly the same, but there is 
[`core.cpuid`][1].


[1]: https://druntime.dpldocs.info/core.cpuid.html


Oh thx my bad I did not see it!


if you want even more cpuid details there is also

https://github.com/dd86k/ddcpuid/
https://code.dlang.org/packages/ddcpuid