www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - std.algorithm.among

reply "bearophile" <bearophileHUGS lycos.com> writes:
The idea of not making std.algorithm.among!() a predicate was not 
so good:


void main() {
     import std.stdio, std.algorithm;
     auto s = "hello how\nare you";
     s.until!(c => c.among!('\n', '\r')).writeln;
}


(A normal workaround is to use !!c.among!).

Bye,
bearophile
Jul 13 2014
next sibling parent "sigod" <sigod.mail gmail.com> writes:
On Sunday, 13 July 2014 at 11:18:05 UTC, bearophile wrote:
 The idea of not making std.algorithm.among!() a predicate was 
 not so good:


 void main() {
     import std.stdio, std.algorithm;
     auto s = "hello how\nare you";
     s.until!(c => c.among!('\n', '\r')).writeln;
 }


 (A normal workaround is to use !!c.among!).

 Bye,
 bearophile
``` s.until!(among!('\n', '\r')).writeln; // Error: cannot implicitly convert expression (among(front(this._input))) of type uint to bool ``` :-(
Jul 13 2014
prev sibling next sibling parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/13/2014 01:18 PM, bearophile wrote:
 The idea of not making std.algorithm.among!() a predicate was not so good:
 ...
Agreed.
 void main() {
      import std.stdio, std.algorithm;
      auto s = "hello how\nare you";
      s.until!(c => c.among!('\n', '\r')).writeln;
 }


 (A normal workaround is to use !!c.among!).

 Bye,
 bearophile
It works with filter, so I think it should just work with until as well.
Jul 13 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 It works with filter, so I think it should just work with until 
 as well.
So do you suggest me to open a bug report where I ask "among" to return a bool, or do you suggest to ask for an enhancement of "until", or what? Bye, bearophile
Jul 13 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/13/2014 03:09 PM, bearophile wrote:
 Timon Gehr:

 It works with filter, so I think it should just work with until as well.
So do you suggest me to open a bug report where I ask "among" to return a bool, or do you suggest to ask for an enhancement of "until", or what? Bye, bearophile
I am saying the following code implementing 'until' in std.algorithm is at fault: private bool predSatisfied() // <-- don't say bool here { static if (is(Sentinel == void)) return unaryFun!pred(_input.front); // or cast here else return startsWith!pred(_input, _sentinel); // and here }
Jul 13 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Timon Gehr:

 I am saying the following code implementing 'until' in 
 std.algorithm is at fault:

     private bool predSatisfied() // <-- don't say bool here
     {
         static if (is(Sentinel == void))
             return unaryFun!pred(_input.front); // or cast here
         else
             return startsWith!pred(_input, _sentinel); // and 
 here
     }
https://issues.dlang.org/show_bug.cgi?id=13124 Bye, bearophile
Jul 13 2014
prev sibling parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 13 July 2014 at 11:18:05 UTC, bearophile wrote:
 The idea of not making std.algorithm.among!() a predicate was 
 not so good:


 void main() {
     import std.stdio, std.algorithm;
     auto s = "hello how\nare you";
     s.until!(c => c.among!('\n', '\r')).writeln;
 }


 (A normal workaround is to use !!c.among!).

 Bye,
 bearophile
That's weird, I always assumed this worked. Was it always the case that numeric types can't be implicitly casted to bool?
Jul 13 2014
parent reply Timon Gehr <timon.gehr gmx.ch> writes:
On 07/13/2014 08:51 PM, Meta wrote:
 That's weird, I always assumed this worked. Was it always the case that
 numeric types can't be implicitly casted to bool?
Yes, unless their range fits into [0,2).
Jul 13 2014
parent reply "Meta" <jared771 gmail.com> writes:
On Sunday, 13 July 2014 at 19:06:29 UTC, Timon Gehr wrote:
 On 07/13/2014 08:51 PM, Meta wrote:
 That's weird, I always assumed this worked. Was it always the 
 case that
 numeric types can't be implicitly casted to bool?
Yes, unless their range fits into [0,2).
It seems that not even that is the case. void main() { uint n = 0; //Error bool b = n; }
Jul 13 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Meta:

 It seems that not even that is the case.

 void main()
 {
 	uint n = 0;
         //Error
 	bool b = n;
 }
D doesn't carry the range of mutable variables across different expressions. So write (with the 2.066beta3): void main() { const uint x1 = 0; const uint x2 = 1; bool b1 = x1; bool b2 = x2; } Bye, bearophile
Jul 13 2014
parent reply "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= writes:
On Sunday, 13 July 2014 at 20:55:25 UTC, bearophile wrote:
 D doesn't carry the range of mutable variables across different 
 expressions.
What is the reasoning behind this kind of special-casing?
Jul 13 2014
parent "bearophile" <bearophileHUGS lycos.com> writes:
Ola Fosheim Grøstad:

 On Sunday, 13 July 2014 at 20:55:25 UTC, bearophile wrote:
 D doesn't carry the range of mutable variables across 
 different expressions.
What is the reasoning behind this kind of special-casing?
Compiler simplicity and to avoid flow analysis. The value range is kept across const expressions since 2.066. Bye, bearophile
Jul 14 2014