digitalmars.D.learn - Minimum value in a range
- Andrej Mitrovic (7/7) Aug 02 2011 import std.algorithm;
- simendsjo (6/13) Aug 02 2011 import std.algorithm;
- Andrej Mitrovic (2/3) Aug 02 2011 Thanks!
- Andrej Mitrovic (17/17) Aug 02 2011 Does anyone know why putting this alias in module scope errors out?:
- Timon Gehr (17/34) Aug 04 2011 I believe it is because the function literal is typed as a template dele...
- Andrej Mitrovic (3/3) Aug 02 2011 Oh just realized I can use the much simpler:
- simendsjo (3/6) Aug 02 2011 Of course. I even had a lot of coffee running through my veins, so shame...
- bearophile (5/14) Aug 02 2011 See:
- Kai Meyer (3/10) Aug 04 2011 Looking at std.algorithm, I think what you really want is minCount:
- bearophile (4/6) Aug 04 2011 It's a bad design.
- Kai Meyer (2/8) Aug 05 2011 minCount is, or the usage of minCount in his particular problem?
- bearophile (6/7) Aug 05 2011 Your usage seems OK. What I meant is that I don't like the design of min...
import std.algorithm; void main() { auto x = min([1, 2, 3]); // x would be 1 } min() isn't equipped to do this on a single range. What can I use instead? I haven't had my coffee yet. :)
Aug 02 2011
On 02.08.2011 14:03, Andrej Mitrovic wrote:import std.algorithm; void main() { auto x = min([1, 2, 3]); // x would be 1 } min() isn't equipped to do this on a single range. What can I use instead? I haven't had my coffee yet. :)import std.algorithm; void main() { assert(min(1, 2, 3) == 1); assert(reduce!"min(a,b)"([1,2,3]) == 1); }
Aug 02 2011
On 8/2/11, simendsjo <simendsjo gmail.com> wrote:assert(reduce!"min(a,b)"([1,2,3]) == 1);Thanks!
Aug 02 2011
Does anyone know why putting this alias in module scope errors out?: import std.algorithm; alias reduce!((a, b){ return 1; }) foo; void main() { foo([1, 2, 3]); } Error: delegate test.__dgliteral1!(int,int).__dgliteral1 is a nested function and cannot be accessed from reduce But this will work: import std.algorithm; void main() { alias reduce!((a, b){ return 1; }) foo; foo([1, 2, 3]); }
Aug 02 2011
Andrei Mitrovic wrote:Does anyone know why putting this alias in module scope errors out?: import std.algorithm; alias reduce!((a, b){ return 1; }) foo; void main() { foo([1, 2, 3]); } Error: delegate test.__dgliteral1!(int,int).__dgliteral1 is a nested function and cannot be accessed from reduce But this will work: import std.algorithm; void main() { alias reduce!((a, b){ return 1; }) foo; foo([1, 2, 3]); }I believe it is because the function literal is typed as a template delegate and the compiler cannot figure out where to get the context pointer from (because at module level, there is no need for a context pointer). If you declare the alias inside the main function, the 'reduce' function will be instantiated as a local function and therefore it has the context pointer. The result is that you pass around some unnecessary context pointers (without inlining). AFAIK Andrei wants this fixed. You can explicitly tell the compiler to type the function literal as a function pointer: alias reduce!(function (a, b){ return 1; }) foo; void main() { foo([1, 2, 3]); } works. -Timon
Aug 04 2011
Oh just realized I can use the much simpler: auto x = reduce!(min)([1, 2, 3]); Coffee is starting to kick in!
Aug 02 2011
On 02.08.2011 14:42, Andrej Mitrovic wrote:Oh just realized I can use the much simpler: auto x = reduce!(min)([1, 2, 3]); Coffee is starting to kick in!Of course. I even had a lot of coffee running through my veins, so shame on me!
Aug 02 2011
Andrej Mitrovic:import std.algorithm; void main() { auto x = min([1, 2, 3]); // x would be 1 } min() isn't equipped to do this on a single range. What can I use instead? I haven't had my coffee yet. :)See: http://d.puremagic.com/issues/show_bug.cgi?id=4705 Bye, bearophile
Aug 02 2011
On 08/02/2011 06:03 AM, Andrej Mitrovic wrote:import std.algorithm; void main() { auto x = min([1, 2, 3]); // x would be 1 } min() isn't equipped to do this on a single range. What can I use instead? I haven't had my coffee yet. :)Looking at std.algorithm, I think what you really want is minCount: http://www.d-programming-language.org/phobos/std_algorithm.html#minCount
Aug 04 2011
Kai Meyer:Looking at std.algorithm, I think what you really want is minCount: http://www.d-programming-language.org/phobos/std_algorithm.html#minCountIt's a bad design. Bye, bearophile
Aug 04 2011
On 08/04/2011 07:54 PM, bearophile wrote:Kai Meyer:minCount is, or the usage of minCount in his particular problem?Looking at std.algorithm, I think what you really want is minCount: http://www.d-programming-language.org/phobos/std_algorithm.html#minCountIt's a bad design. Bye, bearophile
Aug 05 2011
Kai Meyer:minCount is, or the usage of minCount in his particular problem?Your usage seems OK. What I meant is that I don't like the design of min/max/minCount. I have expressed my ideas here: http://d.puremagic.com/issues/show_bug.cgi?id=4705 I'd like to know what Andrei thinks about it. Then, I am probably able to implement those ideas myself. Bye, bearophile
Aug 05 2011