Help required on Array appender

Nicholas Wilson via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sat Sep 2 05:54:48 PDT 2017


On Saturday, 2 September 2017 at 10:15:04 UTC, Vino.B wrote:
> Hi All,
>
>  Can you please guide me how can i use array appender for the 
> below piece of code
>
> string[][] cleanFiles (string FFs, string Step) {
> auto dFiles = dirEntries(FFs, SpanMode.shallow).filter!(a => 
> a.isFile).map!(a => tuple(a.name , a.timeCreated)).array;
> 	foreach (d; dFiles) {
> 		if (Step == "dryrun")
> 		{
>                     Subdata ~=  [d[0], d[1].toSimpleString[0 .. 
> 20]];
>              }
> 		else if (Step == "run")
>              {
> 			remove(d[0]);
> 			if (!d[0].exists)
> 				Subdata ~=  [d[0], d[1].toSimpleString[0 .. 20]];
> 		}
> 	}
> 	return Subdata;
> }
>
> From,
> Vino.B

If you're wanting to use appender just make an appender and 
replace the ~= to calls to appender.put(data);

if you're trying to make it faster, consider that Step could be a 
bool, your return type could be string[2][], the `if 
(!d[0].exists)` is redundant since `remove` will throw if it 
fails. That leaves you with

string[2][] cleanFiles(string FFs, bool dryrun)
{
     auto dFiles = dirEntries(FFs, SpanMode.shallow)
                   .filter!(a => a.isFile)
                   .map!(a =>[a.name , 
a.timeCreated.toSimpleString[0 .. 20])
                   .array;
     if (! dryrun)
         dFiles.each!(f => f[0].remove);
}


More information about the Digitalmars-d-learn mailing list