www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Check whether a range is empty

reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 13 2018
next sibling parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jul 13 2018
parent reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 13 2018
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jul 13 2018
parent reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 14 2018
parent reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 14 2018
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
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
Jul 14 2018
parent reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 15 2018
parent reply Steven Schveighoffer <schveiguy gmail.com> writes:
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
Jul 15 2018
parent reply vino.B <bheeman.vino hotmail.com> writes:
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
Jul 15 2018
parent Steven Schveighoffer <schveiguy gmail.com> writes:
On 7/15/18 8:56 AM, vino.B wrote:
 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/ ange/package.d#L900
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.
Ahh, I think you want joiner: https://dlang.org/phobos/std_algorithm_iteration.html#joiner -Steve
Jul 17 2018
prev sibling parent reply Gary Willoughby <dev nomad.uk.net> writes:
On Friday, 13 July 2018 at 18:37:35 UTC, 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"); }


 From,
 Vino.B
I thought every range at the lowest level has an `empty` property. So, in this case, it would be: if (PFResutl.toRange.empty) { writeln("Empty"); }
Jul 17 2018
parent Alex <sascha.orlov gmail.com> writes:
On Tuesday, 17 July 2018 at 13:59:45 UTC, Gary Willoughby wrote:
 I thought every range at the lowest level has an `empty` 
 property. So, in this case, it would be:

 if (PFResutl.toRange.empty)
 {
     writeln("Empty");
 }
Yeah, but it seems, that PFResutl is a range of ranges, and the OP has the case, where both of the contained ranges are empty. However, this does not correspond to empty of the governing range. So [[], []].empty is false whereas [[], []].joiner.empty is true.
Jul 17 2018