in reply to reduce like iterators

Hello younger me :)

> and List::MoreUtils doesn't seem to provide anything better

This changed in the meantime, slide was added, but there are still issues.

The worst one is that it only returns scalars instead of lists like map does, which means we have to add an extra grep to filter undefined values :/

use v5.12; use warnings; use List::MoreUtils qw/slide/; use Data::Dump; my @x= split',', q(a,a,a,a,b,c,c,a,a,d,e,e,e,e); dd grep defined, slide { $a ne $b ? $b : () } "", @x;

Cheers Rolf
(addicted to the Perl Programming Language :)
see Wikisyntax for the Monastery

Replies are listed 'Best First'.
Re^2: reduce like iterators (BUG in List::MoreUtils::slide)
by LanX (Saint) on Jan 29, 2024 at 22:38 UTC
    > The worst one is that it only returns scalars instead of lists like map does, which means we have to add an extra grep to filter undefined values :/

    That's most likely a bug in the XS implementation of slide.

    I looked into the Pure Perl implementation in List::MoreUtils::PP, and it looked fine, it's literally using map

    And a test reveals, that it really works like it should.

    use v5.12; use warnings; use Data::Dumper; BEGIN { $ENV{LIST_MOREUTILS_PP} = 1 }; # enforce PP version, comment f +or XS use List::MoreUtils qw/slide/; my @x= split',', q(a,a,a,a,b,c,c,a,a,d,e,e,e,e); print Dumper slide { $a ne $b ? $b : () } "", @x;

      PP version

      $VAR1 = 'a'; $VAR2 = 'b'; $VAR3 = 'c'; $VAR4 = 'a'; $VAR5 = 'd'; $VAR6 = 'e';

      XS version

      $VAR1 = 'a'; $VAR2 = undef; $VAR3 = undef; $VAR4 = undef; $VAR5 = 'b'; $VAR6 = 'c'; $VAR7 = undef; $VAR8 = 'a'; $VAR9 = undef; $VAR10 = 'd'; $VAR11 = 'e'; $VAR12 = undef; $VAR13 = undef; $VAR14 = undef;

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    see Wikisyntax for the Monastery

      > That's most likely a bug in the XS implementation of slide.

      The word "bug" should be a link to the actual bug report.

      map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
        I already reported it in private, it's complicated....

        Cheers Rolf
        (addicted to the Perl Programming Language :)
        see Wikisyntax for the Monastery