#!/usr/bin/env raku
# List in Raku have many, many methods already: https://docs.raku.org/type/List
# But you can attach some more you see on youtube: https://www.youtube.com/watch?v=LvFTyvYM-ds
# by mixin in Roles: https://docs.raku.org/language/objects#Mixins_of_roles
# such that it is easy to re-use these enhancements
# by just putting your default Role enhancements in a module and use it together
# with any project specific enhancements anywhere.
use lib '.';
use My-Roles;
role Project-Specific-List-Enhancements does Generic-List-Enhancements {
method is-sorted-until( --> UInt) {
self.cache.&{$_ Z .skip}.map({.[0] <= .[1] || last}).sum
}
method adjacent-difference {
($_ Z- .skip given self.cache).flat does Project-Specific-List-Enhancements
}
};
my @a does Project-Specific-List-Enhancements = 1, 2, 3, 3, 4, 6, 5;
say @a;
say @a.is-sorted;
say @a.is-unsorted;
say @a.is-sorted-until;
say @a.adjacent-difference;
say @a.adjacent-difference.adjacent-difference;
say @a.adjacent-difference.adjacent-difference.is-sorted-until;
unit module My-Roles;
role Generic-List-Enhancements is export {
method is-sorted( --> Bool) {
?self.&{$_ Z<= .skip}.all
}
method is-unsorted( --> Bool) {
so one $_ Z> .skip with self
}
};