digitalmars.D.learn - Check whether a range is empty
- vino.B (7/7) Jul 13 2018 Hi All,
- Steven Schveighoffer (6/14) Jul 13 2018 Without knowing what PFResutl is, let me simplify a bit:
- vino.B (13/28) Jul 13 2018 Hi Steve,
- Steven Schveighoffer (14/44) Jul 13 2018 Well, empty is how you detect whether any range is empty, and as far as
- vino.B (6/23) Jul 14 2018 Hi Steve,
- vino.B (9/39) Jul 14 2018 Hi Steve,
- =?UTF-8?Q?Ali_=c3=87ehreli?= (20/25) Jul 14 2018 If that's the output of r, then r is not empty but has two elements and
- vino.B (20/48) Jul 15 2018 HI Ali,
- Steven Schveighoffer (8/61) Jul 15 2018 I still don't know why you are using chain here as it equates to the
- vino.B (9/19) Jul 15 2018 Hi Steve,
- Steven Schveighoffer (4/19) Jul 17 2018 Ahh, I think you want joiner:
- Gary Willoughby (7/14) Jul 17 2018 I thought every range at the lowest level has an `empty`
- Alex (8/14) Jul 17 2018 Yeah, but it seems, that PFResutl is a range of ranges, and the
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
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
On Friday, 13 July 2018 at 19:05:20 UTC, Steven Schveighoffer wrote:On 7/13/18 2:37 PM, vino.B wrote: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.BHi 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
On 7/13/18 3:29 PM, vino.B wrote:On Friday, 13 July 2018 at 19:05:20 UTC, Steven Schveighoffer 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. -SteveOn 7/13/18 2:37 PM, vino.B wrote: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"); }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?
Jul 13 2018
On Friday, 13 July 2018 at 19:45:03 UTC, Steven Schveighoffer wrote:On 7/13/18 3:29 PM, vino.B wrote:Hi Steve, i Tried your method no luck, it just prints blank lines. From, Vino.B[...]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 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: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.BOn 7/13/18 3:29 PM, vino.B wrote:Hi Steve, i Tried your method no luck, it just prints blank lines. From, Vino.B[...]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 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
Jul 14 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: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.BThe reason it never prints the text "Empty" is that theout 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 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: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#L900First, please show us code that demonstrates the issue. On 07/14/2018 07:47 AM, vino.B wrote: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;} }The reason it never prints the text "Empty" is that theout 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 ]. Aliif(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
On Sunday, 15 July 2018 at 12:18:27 UTC, Steven Schveighoffer wrote:On 7/15/18 7:45 AM, vino.B wrote: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[...]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
Jul 15 2018
On 7/15/18 8:56 AM, vino.B wrote:On Sunday, 15 July 2018 at 12:18:27 UTC, Steven Schveighoffer wrote:Ahh, I think you want joiner: https://dlang.org/phobos/std_algorithm_iteration.html#joiner -SteveOn 7/15/18 7:45 AM, vino.B wrote: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.[...]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
Jul 17 2018
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.BI 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
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