daily pastebin goal
11%
SHARE
TWEET

[Perl 6] Using some higher-order functions.

cwchen Nov 28th, 2017 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  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";
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top