Jump to page: 1 2
Thread overview
Check whether a range is empty
Jul 13, 2018
vino.B
Jul 13, 2018
vino.B
Jul 14, 2018
vino.B
Jul 14, 2018
vino.B
Jul 14, 2018
Ali Çehreli
Jul 15, 2018
vino.B
Jul 15, 2018
vino.B
Jul 17, 2018
Gary Willoughby
Jul 17, 2018
Alex
July 13, 2018
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"); }


From,
Vino.B


July 13, 2018
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?

-Steve
July 13, 2018
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?
>
> -Steve

Hi Steve,

 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"); }

From,
Vino.B
July 13, 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
July 14, 2018
On Friday, 13 July 2018 at 19:45:03 UTC, Steven Schveighoffer wrote:
> On 7/13/18 3:29 PM, vino.B wrote:
>>  [...]
>
>
> 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

Hi Steve,

 i Tried your method no luck, it just prints blank lines.

From,
Vino.B
July 14, 2018
On Saturday, 14 July 2018 at 14:28:52 UTC, vino.B wrote:
> On Friday, 13 July 2018 at 19:45:03 UTC, Steven Schveighoffer wrote:
>> On 7/13/18 3:29 PM, vino.B wrote:
>>>  [...]
>>
>>
>> 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
>
> Hi Steve,
>
>  i Tried your method no luck, it just prints blank lines.
>
> From,
> Vino.B

Hi Steve,

  The reason it never prints the text "Empty" is that the out of the "r" is just an empty array.

OUTPUT:
[]
[]

From,
Vino.B
July 14, 2018
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

July 15, 2018
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;} }
      if(a == 0) { writeln("No files");

}

From,
Vino.B


July 15, 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
July 15, 2018
On Sunday, 15 July 2018 at 12:18:27 UTC, Steven Schveighoffer wrote:
> On 7/15/18 7:45 AM, vino.B wrote:
>> [...]
>
> 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
>
>>  [...]
>
> 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

Hi Steve,

 Initially i thought the using "chain" it will merge several ranges for arrays into single range of array, but it doesn't merge into single range of array so i have removed the same from my code.


From,
Vino.B
« First   ‹ Prev
1 2