Check whether a range is empty

Steven Schveighoffer schveiguy at gmail.com
Sun Jul 15 12:18:27 UTC 2018


On 7/15/18 7:45 AM, vino.B wrote:
> On Saturday, 14 July 2018 at 17:20:52 UTC, Ali Çehreli wrote:
>> First, please show us code that demonstrates the issue.
>>
>> On 07/14/2018 07:47 AM, vino.B wrote:
>>
>> >    The reason it never prints the text "Empty" is that the
>> out of the
>> > "r" is just an empty array.
>> >
>> > OUTPUT:
>> > []
>> > []
>>
>> If that's the output of r, then r is not empty but has two elements 
>> and those elements are likely arrays. If they are in fact arrays, them 
>> being empty does not change r: it still has two elements.
>>
>> If you want to treat the range as empty when all its elements are 
>> empty, then perhaps your problem needs std.algorithm.joiner. The 
>> following program demonstrates your issue with the first assert and 
>> the fix with the second assert:
>>
>> import std.algorithm;
>> import std.range;
>>
>> void main() {
>>     int[][] r = [ [], [] ];
>>     assert(!r.empty);
>>     auto joined_r = r.joiner;
>>     assert(joined_r.empty);
>> }
>>
>> joiner joins elements that are ranges themselves. For example, 
>> joiner([ [1], [2] ]) is equal to [ 1, 2 ].
>>
>> Ali
> 
> HI Ali,
> 
>   Thank you very much, but unfortunately the above solution did not work 
> as the variable PFResult contains the output from the 
> workerLocalStorgage which is prited as PFResult.toRange , but was able 
> to find a solution as below
> 
> void ptThreadManager(alias coRoutine, T...)(Array!string Dirlst, T params)
> {
>        alias scRType = typeof(coRoutine(string.init, T.init));
>        auto PFresult = taskPool.workerLocalStorage!scRType();
>        PFresult.get ~= coRoutine(FFs, params); }
>        int a = 0;
>        if (!(PFresult.toRange).empty) {
>        foreach(i; chain(PFresult.toRange)) { writeln(i[]); a = a +1;} }

I still don't know why you are using chain here as it equates to the 
identity function in this instance:
https://github.com/dlang/phobos/blob/3d41721141f31059ca6e77dec2da390fad737955/std/range/package.d#L900

>        if(a == 0) { writeln("No files");

So I'm assuming from your assertion that this works, that the range is 
not empty, but yields no elements when it's iterated? Seems like a bug 
to me.

-Steve


More information about the Digitalmars-d-learn mailing list