Re: Best way to test predicate against a range
Hello Jonathan, For example, there are two functions that I'd like to be have: all() and any(). That is, I want a function which checks a predicate against a range and returns whether all elements in that range satisfy the predicate, and I want a function that checks a predicate against a range and returns whether any element satisfies the predicate. Ideally, all() would shortcut if it found even one element which didn't satisfy the predicate, and any() would shortcut if it found even one that did. The trivial solution (no shortcuting) would be to map the predicate and reduce via 'and' or 'or'. -- ... IXOYE
Re: Best way to test predicate against a range
On Sun, Jun 27, 2010 at 08:07, BCS n...@anon.com wrote: Hello Jonathan, For example, there are two functions that I'd like to be have: all() and any(). That is, I want a function which checks a predicate against a range and returns whether all elements in that range satisfy the predicate, and I want a function that checks a predicate against a range and returns whether any element satisfies the predicate. Ideally, all() would shortcut if it found even one element which didn't satisfy the predicate, and any() would shortcut if it found even one that did. The trivial solution (no shortcuting) would be to map the predicate and reduce via 'and' or 'or'. Or do a new version of reduce that stops the reducing on a certain condition reduceWhile(alias reducer, alias predicate) {... reduce using reducer, as long as predicate holds } That would be a nice addition to std.algo. The question is: what should test the predicate? The last returned value, the entire result being created? As an asideJonathan, you may be interested in using some of the algorithms here: http://www.dsource.org/projects/dranges 'all' and 'some' are in the 'predicates' module. They accept predicates of any arity (even 0!), because I wanted to test for increasing numerical ranges, like this : auto isGrowing = all ! ab (range); Note that the fact of accepting variable-arity predicates represents 90% of the complexity, because a simple all is: import std.functional: unaryFun; import std.range: isInputRange; bool all(alias predicate, R)(R range) if (isInputRange!R) { foreach(elem; range) { if (!unaryFun!predicate(elem)) return false; } return true; } // warning : untested. usage: auto a = all ! a0 (range) or auto a = all ! foo (range) Philippe
Weird error on nested map
auto fn1 = ( string s ) { return s; }; auto fn2 = ( string s ) { return map!fn1( [] ); }; auto idirs = map!fn2( [] ); The above code gives the following errors: foo.d(114): Error: struct foo.main.Map!(fn2,string[]).Map inner struct Map cannot be a field foo.d(114): Error: struct foo.main.Map!(fn2,string[]).Map inner struct Map cannot be a field As far as I can see, this has to do with struct rules, but I can't see how to fix it. Should I file this in bugzilla? -- Simen
What are delimited string, heredoc and D token strings?
Hi all, The D2.0 lexical page describes delimited string and token string literals. Is there any example of how these are used and why, somewhere? Thanks -- Pierre
Re: What are delimited string, heredoc and D token strings?
Pierre Rouleau prouleau...@gmail.com wrote: Hi all, The D2.0 lexical page describes delimited string and token string literals. Is there any example of how these are used and why, somewhere? Token strings are added for the specific use case of string mixins[1]. They must contain valid D code. mixin( q{ a = b; } ); // Works. mixin( q{ this is nonsense, I tell you! } ); // Does not work. Editors may syntax-highlight token strings as if it were normal code. They often do, by virtue of having no idea that it is indeed a string. Delimited strings and heredoc strings exist for simplicity of adding long strings to the source. qEOS Heredoc strings allow you to add long string literals to your code. They also let you embed quotes 'of' `various` ´kinds´ without escaping them. EOS q(Delimited strings are much the same as heredoc strings, but somewhat more succinct, by not using a whole identifier for terminators. They are thus not as suited for long texts.) [1]: http://digitalmars.com/d/2.0/statement.html#MixinStatement also, http://digitalmars.com/d/2.0/expression.html#MixinExpression http://digitalmars.com/d/2.0/module.html#MixinDeclaration -- Simen
Re: Weird error on nested map
On Sun, Jun 27, 2010 at 10:11, Simen kjaeraas simen.kja...@gmail.comwrote: auto fn1 = ( string s ) { return s; }; auto fn2 = ( string s ) { return map!fn1( [] ); }; auto idirs = map!fn2( [] ); The above code gives the following errors: foo.d(114): Error: struct foo.main.Map!(fn2,string[]).Map inner struct Map cannot be a field foo.d(114): Error: struct foo.main.Map!(fn2,string[]).Map inner struct Map cannot be a field As far as I can see, this has to do with struct rules, but I can't see how to fix it. Should I file this in bugzilla? I've had this particular error dozens of time :-( Most of the time, it means the function you passed to (the outer) Map is not correct, be it a template that has trouble instantiating, a type mismatch or some frame pointer that got lost. In your case, I guess there is a bug somewhere concerning the passing around of delegates. If you get rid of delegates (which is not what you want, I know), it works: string fn1(string s) { return s;} auto fn2(string s) { return map!fn1([]);} void main() { auto idirs = map!fn2([]); // works. } I tried defining fn1 inside fn2, but it does not work. Too bad. Philippe
Re: What are delimited string, heredoc and D token strings?
On 27/06/10 9:52 AM, Simen kjaeraas wrote: Pierre Rouleau prouleau...@gmail.com wrote: Hi all, The D2.0 lexical page describes delimited string and token string literals. Is there any example of how these are used and why, somewhere? Token strings are added for the specific use case of string mixins[1]. They must contain valid D code. mixin( q{ a = b; } ); // Works. mixin( q{ this is nonsense, I tell you! } ); // Does not work. Editors may syntax-highlight token strings as if it were normal code. They often do, by virtue of having no idea that it is indeed a string. Delimited strings and heredoc strings exist for simplicity of adding long strings to the source. qEOS Heredoc strings allow you to add long string literals to your code. They also let you embed quotes 'of' `various` ´kinds´ without escaping them. EOS q(Delimited strings are much the same as heredoc strings, but somewhat more succinct, by not using a whole identifier for terminators. They are thus not as suited for long texts.) [1]: http://digitalmars.com/d/2.0/statement.html#MixinStatement also, http://digitalmars.com/d/2.0/expression.html#MixinExpression http://digitalmars.com/d/2.0/module.html#MixinDeclaration Thanks for this clear explanation, Simen. I was wondering if D had the equivalent of Python's triple quote string literals and it does: the delimited string serves the same purpose. Great! -- Pierre
auto functions not authorized inside main?
Is it defined somewhere that auto functions are not authorized inside main? void main() { auto fun(string s) { return s;} // this does not compile } error: main.d|6|found 's' when expecting ')'| main.d|6|semicolon expected, not ')'| main.d|6|found ')' instead of statement| main.d|7|unrecognized declaration| ||=== Build finished: 4 errors, 0 warnings ===| So it's not even parsed? I couldn't find a bugzilla entry for this and I cannot believe no one ever tried to put an auto fun inside main! Is that part of the spec? Philippe
Re: What are delimited string, heredoc and D token strings?
Pierre Rouleau: I was wondering if D had the equivalnt of Python's triple quote string literals and it does: the delimited string serves the same purpose. Great! But keep in mind that normal D string literals can span more than one line :-) Bye, bearophile
Re: auto functions not authorized inside main?
Philippe Sigaud: I couldn't find a bugzilla entry for this and I cannot believe no one ever tried to put an auto fun inside main! Maybe auto funcs are seen as instantiated templates, and templates can't be defined inside functions. Anyway, I think you can file this as enhancement request. Bye, bearophile
Re: Best way to test predicate against a range
Jonathan M Davis: Okay. The functions in std.algorithm are quite powerful, but sometimes you have to play around with them a bit to figure out how to do exactly what you're trying to do. Some of them need to be improved in their concept and API. Some of them are awkward to use or they are not the most handy. They are not battle-tested at all, the best group of functions is yet to be found. Bye, bearophile
Re: What are delimited string, heredoc and D token strings?
On 27/06/10 11:36 AM, bearophile wrote: But keep in mind that normal D string literals can span more than one line :-) In what sense? In the sense that adjacent strings are concatenated? If I want to create a string literal that embeds new line without explicitly placing a '\n' inside the string, I did not see any other way than using the delimited string (at least inside http://www.digitalmars.com/d/2.0/lex.html). What am I missing? -- Pierre
Why doesn't this work in D2?
Why doesn't the following code work in D2 (it works in D1)? void foo (T) (in T[] a, T b) { } void main () { asd.foo('s'); } The error I get is: main.d(10): Error: template main.foo(T) does not match any function template declaration main.d(10): Error: template main.foo(T) cannot deduce template function from argument types !()(string,char) It seems to be some problem with the b argument, if I change that to char it works. -- /Jacob Carlborg
Re: Why doesn't this work in D2?
On 06/27/2010 12:18 PM, Jacob Carlborg wrote: Why doesn't the following code work in D2 (it works in D1)? void foo (T) (in T[] a, T b) { } void main () { asd.foo('s'); } asd.foo(cast(immutable) 's');
Re: Why doesn't this work in D2?
Jacob Carlborg d...@me.com wrote: Why doesn't the following code work in D2 (it works in D1)? void foo (T) (in T[] a, T b) { } void main () { asd.foo('s'); } The error I get is: main.d(10): Error: template main.foo(T) does not match any function template declaration main.d(10): Error: template main.foo(T) cannot deduce template function from argument types !()(string,char) It seems to be some problem with the b argument, if I change that to char it works. In D2, strings are of type immutable(char)[], so your T would be immutable(char). However, 's' is a simple, unadorned char. Ways to fix this would include: void foo(T)(const T[] a, const T b){ ... } void foo(T,U)(const T[] a, U b) if (is(Unqual!T == Unqual!U)) { ... } -- Simen
Re: Why doesn't this work in D2?
On 2010-06-27 19:26, Simen kjaeraas wrote: Jacob Carlborg d...@me.com wrote: Why doesn't the following code work in D2 (it works in D1)? void foo (T) (in T[] a, T b) { } void main () { asd.foo('s'); } The error I get is: main.d(10): Error: template main.foo(T) does not match any function template declaration main.d(10): Error: template main.foo(T) cannot deduce template function from argument types !()(string,char) It seems to be some problem with the b argument, if I change that to char it works. In D2, strings are of type immutable(char)[], so your T would be immutable(char). However, 's' is a simple, unadorned char. Ways to fix this would include: void foo(T)(const T[] a, const T b){ ... } void foo(T,U)(const T[] a, U b) if (is(Unqual!T == Unqual!U)) { ... } That's annoying, specially since char is a value type. I would preferably have a solution for both D1 and D2. Can I use a template to cast/alias away the immutable part? -- /Jacob Carlborg
Re: Why doesn't this work in D2?
Hello Jacob, That's annoying, specially since char is a value type. I would preferably have a solution for both D1 and D2. Can I use a template to cast/alias away the immutable part? One solution would be to have templates strip off const/immutable from the top level of args. void F1(T)(T t) { pragam(msg,typeof(t).stringof); } string s1; immutable(char[]) s2 char[] s3 F1(s1); // immutable(char)[] // all as normal F1(s2); // immutable(char)[] // making a mutable copy of a immutable value is OK F1(s3); // char[] // all as normal void F2(T)(immutable T t) { pragam(msg,typeof(t).stringof); } F2(s1); // immutable(char[]) // making an immutable copy of a mutable reference to immutable data is ok F2(s2); // immutable(char[]) // all as normal F2(s3); // error, invalid conversion This solution would match the proposal that popped up a while ago to allow value assignment from const/immutable to mutable. -- ... IXOYE
A module comprehensive template-specialization
Hi list, I tried to write a traits class comparable to iterator_traits in C++ STL or graph_traits in Boost Graph Library in D 2.0, but failed to do so via template specialization which is put into different modules. Putting everything into one module interferes with extensibility. I tried the following: == Module a == | module a; | | template Base (T) | { | alias T Base; | } == Module b == | module b; | | import a; | | template Base(T: T*) | { | alias Base !(T) Base; | } == Main module == | | import a, b; | | int main(char[][] args) | { | alias Base !(int*) foo; | | return 0; | } The error message is: bug.d(8): Error: template instance ambiguous template declaration b.Base(T : T*) and a.Base(T) Can I handle this in another way (like making the template a conditional one)? best regards Matthias Walter