Advertisement
cwchen

[Perl 6] Using some higher-order functions.

Nov 28th, 2017
3,491
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.42 KB | None | 0 0
  1. # grep
  2. (1, 2, 3, 4, 5).grep({ $_ mod 2 != 0 }) ~~ (1, 3, 5) or die "Wrong list";
  3.  
  4. # map
  5. (1, 2, 3, 4, 5).map({ $_ ** 2 }) ~~ (1, 4, 9, 16, 25) or die "Wrong list";
  6.  
  7. # reduce
  8. (1, 2, 3, 4, 5).reduce({ $^a + $^b }) == 15 or die "Wrong value";
  9.  
  10. # sort
  11. (4, 2, 5, 1, 3).sort({ $^b <=> $^a }) ~~ (5, 4, 3, 2, 1) or die "Wrong value";
  12.  
  13. # zip
  14. zip((1, 2, 3), ("a", "b", "c")) ~~ ((1, "a"), (2, "b"), (3, "c")) or die "Wrong list";
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement