Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # The average/starting dev in the team never needs to see
- # this code, and thus doesn't need to learn anything about the
- # meta-programming going on. Too much use of meta-programming
- # in any complex codebase can lead to problems...
- # ... but also solutions.
- role Foldable {
- method left-fold(&f) {
- self.reduce(&f) but Foldable
- }
- method parallel-map(&m) {
- self.hyper.map(&m) but Foldable
- }
- method push(*@a) {
- (self, @a) but Foldable
- }
- }
- # A CLI app that can be easily adapted to your
- # folding needs. If you know what you are doing,
- # they could easily be the same file.
- sub MAIN() {
- my $fold-this = [1 .. 10] but Foldable;
- my sub adder($a, $b) {
- # anonymous state variable eases recursive type thinking
- $a + $b + $++
- }
- my $folded = $fold-this.parallel-map({ $_ + 1 }).left-fold(&adder);
- {
- use Test;
- ok { $folded == 101 }, "The adder works as expected";
- ok { $folded ~~ Foldable }, '$folded is ready to fold again';
- ok { $folded.push(1..5).left-fold(&adder) == 115 }, 'Add some more friends and fold again';
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment