http://www.perlmonks.org?node_id=1071000


in reply to Re^4: index of the minimum element of the array
in thread index of the minimum element of the array

I'm passing a block to reduce.

Replies are listed 'Best First'.
Re^6: index of the minimum element of the array
by ikegami (Patriarch) on Jan 17, 2014 at 18:44 UTC
    Impossible. There's no such data type. You are passing a sub reference to reduce.
    reduce BLOCK LIST
    is compiled as
    &reduce(sub BLOCK, LIST)

      Fair enough. However, the sub is called with inherited @_ and it happens to work.

      Same thing with map:

      sub foo { map { print $_[$_] } 0 .. $#_ }
      Can I not rely on this behavior?

      Update. example code:

      use List::Util 'reduce'; sub minindex { reduce { $_[$a] < $_[$b] ? $a : $b } 0 .. $#_ } my @g = (55, 88, 33, 6, 234, 234, 52, 6, 1324, 22, 1234); print minindex @g; # $g[7] == 6
      This is perl 5, version 12, subversion 3 (v5.12.3) built for x86_64-linux-thread-multi
      List::Util $VERSION = "1.22";

      Update2. One final observation:
      I also see coderef passed to the sub when run under debugger. But not when run normally.

        For reduce, no. It's not documented. It's a side-effect of the implementation you tested.

        For map, yes. map is a not a subroutine; it's an operator. It's not constrained to the syntax and convention of subroutine calls Its argumentoperand is not a sub.

        I also see coderef passed to the sub when run under debugger. But not when run normally.

        It's easy to see by using return inside the block. It's also easy to see using -MO=Concise.

        $ perl -MList::Util=reduce -MO=Concise,-exec -e'reduce { "..." } @a' ... 4 <0> pushmark sRM 5 <$> anoncode[CV ] lRM 6 <1> refgen KM/1 7 <#> gv[*a] s 8 <1> rv2av[t4] lKM/1 9 <#> gv[*reduce] s a <1> entersub[t5] vKS/TARG,1 ... {15} eric@dev-eric [~/work/p4/src/main/CC] $ perl -MList::Util=reduce -MO=Concise,-exec -e'&reduce(sub { "..." }, + @a)' ... 4 <0> pushmark sRM 5 <$> anoncode[CV ] lRM 6 <1> refgen lKM/1 7 <#> gv[*a] s 8 <1> rv2av[t4] lKM/1 9 <#> gv[*reduce] s a <1> entersub[t5] vKS/TARG,AMPER,1 ...
        map and grep are very different beasts, cause the block is NOT handled like an anonymous sub but more like a do-block.

        DB<117> sub tst { map {$_[$_]+1} 0..3 } DB<118> tst 1..10 => (2, 3, 4, 5)

        But your code with reduce didn't work for me, it always returned 0 cause thats the smallest index you passed. 1 cause ... ¹

        Could you show us an example and tell us which versions you use?

        Cheers Rolf

        ( addicted to the Perl Programming Language)

        ¹) @_=(BLOCKREF, indices)

        DB<148> @a= map { int rand 100 } 1..5 => (32, 4, 90, 79, 70) DB<149> sub minindex { reduce { print "@_\t$a:$_[$a],$b:$_[$b]\n"; $ +_[$a] < $_[$b] ? $a : $b } 0 .. $#_ } DB<150> minindex @a CODE(0xa353ea8) 0 1 2 3 4 0:CODE(0xa353ea8),1:0 CODE(0xa353ea8) 0 1 2 3 4 1:0,2:1 CODE(0xa353ea8) 0 1 2 3 4 1:0,3:2 CODE(0xa353ea8) 0 1 2 3 4 1:0,4:3 => 1
        re update:

        thats the interesting part: )

        DB<102> $List::Util::VERSION => "1.22"

        and shouldn't your code return 3 (the first 6) ?

        update

        the result is correct, your code produces the last min.

        update

        you have the same version of List::Util? strange!

        perl -v This is perl, v5.10.0 built for i486-linux-gnu-thread-multi

        Cheers Rolf

        ( addicted to the Perl Programming Language)

Re^6: index of the minimum element of the array
by LanX (Saint) on Jan 17, 2014 at 18:40 UTC
    Ikegami is right, thats not working!

    Better pass the list as a closed over array (here @x)

    DB<109> @a= map { int rand 100 } 1..10 => (96, 10, 99, 9, 43, 8, 20, 85, 42, 26) DB<110> sub minindex { my @x=@_; reduce { $x[$a] < $x[$b] ? $a : $b } 0 .. $#_ } DB<111> print minindex @a 5

    update

    or even better like ikegami now suggested as ref to avoid overhead

    DB<114> @a= map { int rand 100 } 1..10 => (22, 15, 27, 44, 70, 85, +74, 93, 96, 14) DB<115> sub minindex { my $x=\@_; reduce { $x->[$a] < $x->[$b] ? $a : $b } 0 .. $#_ } DB<116> print minindex @a 9

    Cheers Rolf

    ( addicted to the Perl Programming Language)

      Don't copy @_, take a reference to it.