105 C++ Algorithms in One Line of Raku
— 焉知非鱼105 C++ Algorithms in One Line of Raku
- filter_map
Rust 中有一个 filter_map 函数, Raku 也可以有:
my $text = "1\nfrond .25 289\n3.1415 estuary\n";
$text.split(/\s+/).map( {$_ ~~ /\d/ ?? $_*$_ !! Empty });
@a.map(&fn1).grep(&fn2);
@a.map({&fn2 ?? .&fn1 !! Empty});
- zip-tail
@a Z @a[1...*];
@a Z @a.skip;
@a-longer-name Z @a-longer-name.skip;
@a.&{$_ Z .skip};
@a.rotor(2 => -1);
- binary filter
@a «&&» @b;
@a = <foo bar baz buz>;
@b = [True, False, Flase, True];
(@b «&&» @a).grep(* != False)
- find
&first: p # or :v, :kv
&min
&max
&minmax
- &sort
- &reduce
- &produce
- &map
# remove
@a.grep: !&pred
# count
+@a.grep: &pred
# replace
@a.map: {&pred($_) ?? $new $_}
# find_end, lower_bound, upper_bound
@a.first(&pred, :end); # with :p etc for key
# partition
@a.classify(+(&pred)){1, 0}
- Permutations
@a == @b
# next permutation
@a.permutations[0]
# prev permutation
@a.permutations[*-1]
#zip-tail (again)
@a.&{$_ Z .skip}
# is sorted
@a.&{$_ Z< .skip}.all
# is sorted until
@a.&{$_ Z .skip}.map({.[0] < .[1] || last}).sum
# adjacent difference
@a.&{$_ Z- .skip}
@a.&{.skip Z- $_}
# adjacent find
first({[==] $_}, @a.&{$_ Z .skip})[0]
- mismatch, aka zip_find
first(* != False, (@b Z!=== @a) «&&» @a)
- is partitioned
(@a.&{$_ Z .skip}.map({ ?(&pred(.[0]) & !&pred(.[1])) }) «&&» @a).first(?*)
- inner product
[+] @a Zx @b
#exclusive scan
(0, |@a).produce(&[+])[^(*-1)]
# transfrom reduce
@a.map(&fn).reduce(&fn2)
# transfrom inclusive scan
@a.map(&fn).produce(&fn2)