daily pastebin goal
18%
SHARE
TWEET

[Perl 6] Higher-order function demo.

cwchen Nov 28th, 2017 74 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. sub filter($f, @arr) {
  2.     my @out;
  3.    
  4.     for @arr -> $e {
  5.         if $f($e) {
  6.             @out.push($e)
  7.         }
  8.     }
  9.    
  10.     @out;
  11. }
  12.  
  13. my @even = filter { $_ mod 2 == 0 }, 1..10;
  14. @even ~~ (2, 4, 6, 8, 10) or die "Wrong array {@even}";
RAW Paste Data
Top