Check whether a range is empty

Steven Schveighoffer schveiguy at gmail.com
Fri Jul 13 19:45:03 UTC 2018


On 7/13/18 3:29 PM, vino.B wrote:
> On Friday, 13 July 2018 at 19:05:20 UTC, Steven Schveighoffer wrote:
>> On 7/13/18 2:37 PM, vino.B wrote:
>>> Hi All,
>>>
>>>    How do i check whether a range is empty. eg. 
>>> (!PFResutl.toRange).empty. I tired the below, but it is no printing 
>>> Empty if the range is empty it just prints blank line.
>>>
>>> if (!(!PFResutl.toRange).empty) { writeln("Empty"); }
>>>
>>
>> Without knowing what PFResutl is, let me simplify a bit:
>>
>> if( ! (expr).empty) { writeln("Empty"); }
>>
>> That exclamation point means "not". So you are first checking if the 
>> range is NOT empty, and if so, printing "Empty". Is that what you meant?
>>
> 
>   Sorry there was a typo mistake, so the PFResult contain the results of 
> "taskPool.workerLocalStorage" which i print using 
> writeln(PFResult.toRange) so the requirement is that if the rage is 
> empty it has to print "Empty" else it should print the result.
> 
> 
> Eg:
> 
>   if (!(PFresult.toRange).empty) {
>   foreach(i; chain(PFresult.toRange)) { writeln(i[]); }
>   } else { writeln("Empty"); }


Well, empty is how you detect whether any range is empty, and as far as 
ranges are concerned, your code is correctly checking for empty.

A couple comments:

1. Why are you using chain with a single parameter? That just returns 
its parameter.
2. You may want to try grabbing the range ONCE. i.e.:

auto r = PFresult.toRange;
if(!r.empty)
{
    foreach(i; r) ...
}

I'm not familiar with how taskPool works, so I don't know the real answer.

-Steve


More information about the Digitalmars-d-learn mailing list