cwchen icon

[Perl 6] Laziness in iterators.

cwchen | PRO | 11/29/17 02:18:18 AM UTC | 0 ⭐ | 12172 👁️ | Never ⏰ | []
Perl 6 |

0 B

|

None

|

0 👍

/

0 👎

sub filter($f, @arr) {
    gather {
        for @arr -> $e {
            take $e if $f($e);
        }
    }
}
 
my @even = filter({ $_ mod 2 == 0 }, 1..Inf)[0..4];
@even ~~ (2, 4, 6, 8, 10) or die "Wrong array {@even}";

Comments

  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎

    
        
  •  icon
    01/01/70 12:00:00 AM UTC
    Plain Text |

    0 B

    |

    👍

    /

    👎