Guest User

Untitled

a guest
Jul 14th, 2019
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.06 KB | None | 0 0
  1. # The average/starting dev in the team never needs to see
  2. # this code, and thus doesn't need to learn anything about the
  3. # meta-programming going on. Too much use of meta-programming
  4. # in any complex codebase can lead to problems...
  5. # ... but also solutions.
  6.  
  7. role Foldable {
  8. method left-fold(&f) {
  9. self.reduce(&f) but Foldable
  10. }
  11. method parallel-map(&m) {
  12. self.hyper.map(&m) but Foldable
  13. }
  14. method push(*@a) {
  15. (self, @a) but Foldable
  16. }
  17. }
  18.  
  19. # A CLI app that can be easily adapted to your
  20. # folding needs. If you know what you are doing,
  21. # they could easily be the same file.
  22. sub MAIN() {
  23. my $fold-this = [1 .. 10] but Foldable;
  24. my sub adder($a, $b) {
  25. # anonymous state variable eases recursive type thinking
  26. $a + $b + $++
  27. }
  28. my $folded = $fold-this.parallel-map({ $_ + 1 }).left-fold(&adder);
  29. {
  30. use Test;
  31. ok { $folded == 101 }, "The adder works as expected";
  32. ok { $folded ~~ Foldable }, '$folded is ready to fold again';
  33. ok { $folded.push(1..5).left-fold(&adder) == 115 }, 'Add some more friends and fold again';
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment