Quick actions

cmd+k|ctrl+k

Navigation

Languages

roles-and-lists-on-steroids

Snippet info

Language

Raku

Visibility

public

Author

221a86612c7785cb6094f25b034cba9994736288

Created

2025-12-03T09:59:53.635213Z

Updated

2025-12-03T10:02:17.453986Z

#!/usr/bin/env raku

# Lists in Raku have many usefull methods already: https://docs.raku.org/type/List
# But you can attach some more usefull stuff 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 custom enhancements
# by just putting the Role definition in a module and use it anywhere.

role My-Mighty-List { 
    method is-sorted( --> Bool) {
        ?self.&{$_ Z<= .skip}.all
    } 
    method is-sorted-until( --> UInt) {
        self.&{$_ Z .skip}.map({.[0] <= .[1] || last}).sum
    } 
    method adjacent-difference {
        self.&{$_ Z- .skip}
    }
};

my @a does My-Mighty-List = 1, 2, 3, 3, 4, 6, 5;

say @a;
say @a.is-sorted;
say @a.is-sorted-until;
say @a.adjacent-difference;
INFO