Jump to page: 1 2
Thread overview
Minimum value in a range
Aug 02, 2011
Andrej Mitrovic
Aug 02, 2011
simendsjo
Aug 02, 2011
Andrej Mitrovic
Aug 02, 2011
Andrej Mitrovic
Aug 04, 2011
Timon Gehr
Aug 02, 2011
Andrej Mitrovic
Aug 02, 2011
simendsjo
Aug 02, 2011
bearophile
Aug 04, 2011
Kai Meyer
Aug 05, 2011
bearophile
Aug 05, 2011
Kai Meyer
Aug 05, 2011
bearophile
August 02, 2011
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. :)
August 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);
}

August 02, 2011
On 8/2/11, simendsjo <simendsjo@gmail.com> wrote:
>      assert(reduce!"min(a,b)"([1,2,3]) == 1);

Thanks!
August 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]);
}
August 02, 2011
Oh just realized I can use the much simpler:

auto x = reduce!(min)([1, 2, 3]);

Coffee is starting to kick in!
August 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!
August 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
August 04, 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

August 04, 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
August 05, 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#minCount

It's a bad design.

Bye,
bearophile
« First   ‹ Prev
1 2