www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Minimum value in a range

reply Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
next sibling parent reply simendsjo <simendsjo gmail.com> writes:
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
next sibling parent simendsjo <simendsjo gmail.com> writes:
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
prev sibling parent Timon Gehr <timon.gehr gmx.ch> writes:
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
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
On 8/2/11, simendsjo <simendsjo gmail.com> wrote:
      assert(reduce!"min(a,b)"([1,2,3]) == 1);

Thanks!
Aug 02 2011
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling next sibling parent Andrej Mitrovic <andrej.mitrovich gmail.com> writes:
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
prev sibling next sibling parent bearophile <bearophileHUGS lycos.com> writes:
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
prev sibling parent reply Kai Meyer <kai unixlords.com> writes:
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
parent reply bearophile <bearophileHUGS lycos.com> writes:
Kai Meyer:

 Looking at std.algorithm, I think what you really want is minCount:
 http://www.d-programming-language.org/phobos/std_algorithm.html#minCount

It's a bad design. Bye, bearophile
Aug 04 2011
parent reply Kai Meyer <kai unixlords.com> writes:
On 08/04/2011 07:54 PM, bearophile wrote:
 Kai Meyer:

 Looking at std.algorithm, I think what you really want is minCount:
 http://www.d-programming-language.org/phobos/std_algorithm.html#minCount

It's a bad design. Bye, bearophile

minCount is, or the usage of minCount in his particular problem?
Aug 05 2011
parent bearophile <bearophileHUGS lycos.com> writes:
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