Thread overview | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
May 12, 2017 As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to cym13 | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to drug | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 Re: As many thanks As possible to who crates D and UFCS feature | ||||
---|---|---|---|---|
| ||||
Posted in reply to k-five | 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 |
Copyright © 1999-2021 by the D Language Foundation