www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - As many thanks As possible to who crates D and UFCS feature

reply k-five <vhsu30 yahoo.com> writes:
I was waiting for a stable version of C++17 ( standard library ) 
to add some features of fileSystem in C++17 to my program that 
wants to iterate through all files in a directory recursively.

I was thinking how could I do for implementing that and add it to 
my program.

Now after starting to learn D ( nearby 2 weeks so far ). I can do 
it in 6 lines!

void main( string[] args ){
	
	string[] all_file_name =  dirEntries( ".", SpanMode.depth, false 
)
          .filter!( file => !file.name.matchFirst( regex( args[ 1 
] ) ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
"-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
!file.isSymlink ) ) )
	 .map!( file => file.name )
	 .array;
	foreach( string item; all_file_name ) writeln( item );
	
}

./bin-file '[A-Z]$' -f   ---> print all files that are matched 
against [A-Z]$

./bin-file '[A-Z]$' -d   ---> print all directory that are 
matched against [A-Z]$

./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
directory that are matched against [A-Z]$

I am so happy since after more than one year practicing in C++ 
and putting a collection more than 2000 examples of C++ on my 
github, I was not sure I could do it in 6 lines.

May it is a Spam but I think it is worth it.
May 12 2017
next sibling parent reply cym13 <cpicard openmailbox.org> writes:
On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
 I was waiting for a stable version of C++17 ( standard library 
 ) to add some features of fileSystem in C++17 to my program 
 that wants to iterate through all files in a directory 
 recursively.

 I was thinking how could I do for implementing that and add it 
 to my program.

 Now after starting to learn D ( nearby 2 weeks so far ). I can 
 do it in 6 lines!

 void main( string[] args ){
 	
 	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
 false )
          .filter!( file => !file.name.matchFirst( regex( args[ 
 1 ] ) ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
 "-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
 !file.isSymlink ) ) )
 	 .map!( file => file.name )
 	 .array;
 	foreach( string item; all_file_name ) writeln( item );
 	
 }

 ./bin-file '[A-Z]$' -f   ---> print all files that are matched 
 against [A-Z]$

 ./bin-file '[A-Z]$' -d   ---> print all directory that are 
 matched against [A-Z]$

 ./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
 directory that are matched against [A-Z]$

 I am so happy since after more than one year practicing in C++ 
 and putting a collection more than 2000 examples of C++ on my 
 github, I was not sure I could do it in 6 lines.

 May it is a Spam but I think it is worth it.
Shorter: void main( string[] args ){ dirEntries( ".", SpanMode.depth, false ) .filter!( file => !file.name.matchFirst( regex( args[ 1 ] ) ).empty() ) .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == "-d" ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( !file.isSymlink ) ) ) .map!( file => file.name ) .each!(string item => writeln( item )); } It's more memory efficient too because at no point the actual list is stored.
May 12 2017
parent reply k-five <vhsu30 yahoo.com> writes:
On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:
 On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
-------------------------------------------------------
 Shorter:

 void main( string[] args ){
 	dirEntries( ".", SpanMode.depth, false )
          .filter!( file => !file.name.matchFirst( regex( args[ 
 1 ] ) ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
 "-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
 !file.isSymlink ) ) )
 	 .map!( file => file.name )
 	 .each!(string item => writeln( item ));
 }

 It's more memory efficient too because at no point the actual 
 list is stored.
--------------------------------------------------------- Thanks and the correct syntax for each! is, passing a lambda. So the:
 	 .each!(string item => writeln( item ));
is an error: temp.d(15): Error: found 'item' when expecting ')' following template argument list ... and should be: .each!( ( string item ) => writeln( item ) );
May 12 2017
next sibling parent cym13 <cpicard openmailbox.org> writes:
On Friday, 12 May 2017 at 11:58:23 UTC, k-five wrote:
 On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:
 [...]
-------------------------------------------------------
 [...]
--------------------------------------------------------- Thanks and the correct syntax for each! is, passing a lambda. So the:
 [...]
is an error: temp.d(15): Error: found 'item' when expecting ')' following template argument list ... and should be: .each!( ( string item ) => writeln( item ) );
Ah, yeah, my bad, I should have try compiling it instead of answering directly ;)
May 12 2017
prev sibling parent reply drug <drug2004 bk.ru> writes:
12.05.2017 14:58, k-five пишет:
 On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:
 On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
-------------------------------------------------------
 Shorter:

 void main( string[] args ){
     dirEntries( ".", SpanMode.depth, false )
          .filter!( file => !file.name.matchFirst( regex( args[ 1 ] )
 ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == "-d"  ?
 ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( !file.isSymlink
 ) ) )
      .map!( file => file.name )
      .each!(string item => writeln( item ));
 }

 It's more memory efficient too because at no point the actual list is
 stored.
--------------------------------------------------------- Thanks and the correct syntax for each! is, passing a lambda. So the:
      .each!(string item => writeln( item ));
is an error: temp.d(15): Error: found 'item' when expecting ')' following template argument list ... and should be: .each!( ( string item ) => writeln( item ) );
also .each!writeln should be possible
May 12 2017
parent k-five <vhsu30 yahoo.com> writes:
On Friday, 12 May 2017 at 12:56:50 UTC, drug wrote:
 12.05.2017 14:58, k-five пишет:
 On Friday, 12 May 2017 at 11:41:57 UTC, cym13 wrote:
 On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
-------------------------------------------------------
also .each!writeln should be possible
--------------------------------------------------------- Yes. Worked. Thanks
May 12 2017
prev sibling next sibling parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
 I was waiting for a stable version of C++17 ( standard library 
 ) to add some features of fileSystem in C++17 to my program 
 that wants to iterate through all files in a directory 
 recursively.

 I was thinking how could I do for implementing that and add it 
 to my program.

 Now after starting to learn D ( nearby 2 weeks so far ). I can 
 do it in 6 lines!
Thumbs up, nice post! Bastiaan.
May 12 2017
prev sibling parent reply k-five <vhsu30 yahoo.com> writes:
On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
 I was waiting for a stable version of C++17 ( standard library 
 ) to add some features of fileSystem in C++17 to my program 
 that wants to iterate through all files in a directory 
 recursively.

 I was thinking how could I do for implementing that and add it 
 to my program.

 Now after starting to learn D ( nearby 2 weeks so far ). I can 
 do it in 6 lines!

 void main( string[] args ){
 	
 	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
 false )
          .filter!( file => !file.name.matchFirst( regex( args[ 
 1 ] ) ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
 "-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
 !file.isSymlink ) ) )
 	 .map!( file => file.name )
 	 .array;
 	foreach( string item; all_file_name ) writeln( item );
 	
 }

 ./bin-file '[A-Z]$' -f   ---> print all files that are matched 
 against [A-Z]$

 ./bin-file '[A-Z]$' -d   ---> print all directory that are 
 matched against [A-Z]$

 ./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
 directory that are matched against [A-Z]$

 I am so happy since after more than one year practicing in C++ 
 and putting a collection more than 2000 examples of C++ on my 
 github, I was not sure I could do it in 6 lines.

 May it is a Spam but I think it is worth it.
------------------------------------------------------------------ May it has worth it to be an example on how great D is, in somewhere like, in the tour section or std.file or std.regex to attract others. A full version that I just added to my gitgub: https://github.com/k-five/dren
May 12 2017
next sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:
 On Friday, 12 May 2017 at 11:10:01 UTC, k-five wrote:
 I was waiting for a stable version of C++17 ( standard library 
 ) to add some features of fileSystem in C++17 to my program 
 that wants to iterate through all files in a directory 
 recursively.

 I was thinking how could I do for implementing that and add it 
 to my program.

 Now after starting to learn D ( nearby 2 weeks so far ). I can 
 do it in 6 lines!

 void main( string[] args ){
 	
 	string[] all_file_name =  dirEntries( ".", SpanMode.depth, 
 false )
          .filter!( file => !file.name.matchFirst( regex( args[ 
 1 ] ) ).empty() )
          .filter!( file => ( args[ 2 ] == "-f" || args[ 2 ] == 
 "-d"  ? ( args[ 2 ] == "-f" ? !file.isDir : !file.isFile ) : ( 
 !file.isSymlink ) ) )
 	 .map!( file => file.name )
 	 .array;
 	foreach( string item; all_file_name ) writeln( item );
 	
 }

 ./bin-file '[A-Z]$' -f   ---> print all files that are matched 
 against [A-Z]$

 ./bin-file '[A-Z]$' -d   ---> print all directory that are 
 matched against [A-Z]$

 ./bin-file '[A-Z]$' "anything-else"  ---> print both files and 
 directory that are matched against [A-Z]$

 I am so happy since after more than one year practicing in C++ 
 and putting a collection more than 2000 examples of C++ on my 
 github, I was not sure I could do it in 6 lines.

 May it is a Spam but I think it is worth it.
------------------------------------------------------------------ May it has worth it to be an example on how great D is, in somewhere like, in the tour section or std.file or std.regex to attract others. A full version that I just added to my gitgub: https://github.com/k-five/dren
Is it safe to say that these 40 lines of D do the same as your 324 lines of C++ [1]? This, and your comments on the difficulties of building renrem [2] versus doing "rdmd", and the steepness of the learning curve (1 year C++ vs 2 weeks D), and the productivity (2 hours D vs ?? C++) I think are plenty material for a nice little blog. Mike Parker runs the D blog, and I think he might be interested. No need to worry about the english language, you are safe with Mike. I'll see if I can get you his attention. [1] https://github.com/k-five/renrem [2] https://github.com/k-five/renrem/blob/master/src/README.md [3] https://dlang.org/blog/
May 12 2017
parent reply k-five <vhsu30 yahoo.com> writes:
On Friday, 12 May 2017 at 20:53:56 UTC, Bastiaan Veelo wrote:

 Is it safe to say that these 40 lines of D do the same as your 
 324 lines of C++ [1]?
No. I cannot say that. Since this is not a full port of renrem in C++ to D. It was just an example in D, nothing else.
 This, and your comments on the difficulties of building renrem 
 [2] versus doing "rdmd", and the steepness of the learning 
 curve (1 year C++ vs 2 weeks D), and the productivity (2 hours 
 D vs ?? C++)
I am not sure about understanding your purpose correctly.
 I think are plenty material for a nice little blog.
Which English Grammar rule is used here? Sorry but I do not know! are: linking verb after think: main verb and subject! ------------------------------------------------------- I just want to say D is easy to learn and use; that is it. I have no arguing about which Language is better no not. Of course that program with C++, took me 1 month until it got ready, but in 2 days I could ported to D, since I had the already experience of implementing it.
 Mike Parker runs the D blog, and I think he might be 
 interested. No need to worry about the english language, you 
 are safe with Mike. I'll see if I can get you his attention.
Sorry ... Still could not understand ... except you may want me to put such post in D blog not here, and in this case, your are right, the best way for such examples is on a blog or similar. Sorry for posting it here.
May 13 2017
parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Saturday, 13 May 2017 at 08:23:55 UTC, k-five wrote:
 On Friday, 12 May 2017 at 20:53:56 UTC, Bastiaan Veelo wrote:

 Is it safe to say that these 40 lines of D do the same as your 
 324 lines of C++ [1]?
No. I cannot say that. Since this is not a full port of renrem in C++ to D. It was just an example in D, nothing else.
OK understood.
 This, and your comments on the difficulties of building renrem 
 [2] versus doing "rdmd", and the steepness of the learning 
 curve (1 year C++ vs 2 weeks D), and the productivity (2 hours 
 D vs ?? C++)
I am not sure about understanding your purpose correctly.
 I think are plenty material for a nice little blog.
Which English Grammar rule is used here? Sorry but I do not know! are: linking verb after think: main verb and subject!
I am sorry for expressing myself poorly. What I meant to say is that it looked like you can write an interesting article about your experience learning and using C++ and learning and using D, comparing the two. D could come out of that comparison favourably considering 1) how long it takes to learn, 2) how much code you need to write, 3) whether there are difficulties along the way, and 4) how productive you can be (getting things done). I may have been jumping to conclusions, but it could still be an interesting read, especially for people that consider learning C++ or D. In particular the focus on UFCS is interesting, as that can be rather alien to beginners, and something you are enthusiastic about.
 I just want to say D is easy to learn and use; that is it. I 
 have no arguing about which Language is better no not. Of 
 course that program with C++, took me 1 month until it got 
 ready, but in 2 days I could ported to D, since I had the 
 already experience of implementing it.
Understood.
 Mike Parker runs the D blog, and I think he might be 
 interested. No need to worry about the english language, you 
 are safe with Mike. I'll see if I can get you his attention.
Sorry ... Still could not understand ... except you may want me to put such post in D blog not here, and in this case, your are right, the best way for such examples is on a blog or similar. Sorry for posting it here.
Posting it here is fine. You could also have posted in the general forum, as it is more of a compliment than a question. But if you want to write more about your positive experience, then a blog article might be nice. It would reach more people, and it would maybe help some of them. If you want to do that work, then maybe Mike Parker would want to put it on the D blog, and help you polish it. Whatever you decide to do, thanks for sharing your experience here :-) Bastiaan.
May 13 2017
parent reply k-five <vhsu30 yahoo.com> writes:
On Saturday, 13 May 2017 at 10:15:34 UTC, Bastiaan Veelo wrote:
 On Saturday, 13 May 2017 at 08:23:55 UTC, k-five wrote:
 [...]
OK understood.
 [...]
I am sorry for expressing myself poorly. What I meant to say is that it looked like you can write an interesting article about your experience learning and using C++ and learning and using D, comparing the two. D could come out of that comparison favourably considering 1) how long it takes to learn, 2) how much code you need to write, 3) whether there are difficulties along the way, and 4) how productive you can be (getting things done). I may have been jumping to conclusions, but it could still be an interesting read, especially for people that consider learning C++ or D. In particular the focus on UFCS is interesting, as that can be rather alien to beginners, and something you are enthusiastic about.
 [...]
Understood.
 [...]
Posting it here is fine. You could also have posted in the general forum, as it is more of a compliment than a question. But if you want to write more about your positive experience, then a blog article might be nice. It would reach more people, and it would maybe help some of them. If you want to do that work, then maybe Mike Parker would want to put it on the D blog, and help you polish it. Whatever you decide to do, thanks for sharing your experience here :-) Bastiaan.
On Saturday, 13 May 2017 at 10:15:34 UTC, Bastiaan Veelo wrote: -------------------------------------------------------------- Okay, and NOW I understood what you are trying to say. First of all I thought you got mad at me. And I became sad. Since; I tell this really that I was so happy about the code in D, that I would want to share my happiness here with others and not expressing myself. Still I am a beginner and learner. Thanks anyway.
May 13 2017
parent Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Saturday, 13 May 2017 at 10:51:09 UTC, k-five wrote:
 Okay, and NOW I understood what you are trying to say.
 First of all I thought you got mad at me. And I became sad.
My sincere apologies! Always assume the best in people :-) I am glad you asked for clarification.
 [...] Still I am a beginner and learner.
I am too, and learners we are all.
 Thanks anyway.
Welcome.
May 13 2017
prev sibling parent reply Bastiaan Veelo <Bastiaan Veelo.net> writes:
On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:
 A full version that I just added to my gitgub: 
 https://github.com/k-five/dren
You may like getopt[1] for command line argument parsing. https://dlang.org/phobos/std_getopt.html
May 12 2017
parent Nicholas Wilson <iamthewilsonator hotmail.com> writes:
On Friday, 12 May 2017 at 21:26:01 UTC, Bastiaan Veelo wrote:
 On Friday, 12 May 2017 at 15:24:52 UTC, k-five wrote:
 A full version that I just added to my gitgub: 
 https://github.com/k-five/dren
You may like getopt[1] for command line argument parsing. https://dlang.org/phobos/std_getopt.html
see also https://blog.thecybershadow.net/2014/08/05/ae-utils-funopt/ https://github.com/CyberShadow/ae/blob/master/utils/funopt.d
May 12 2017