diff --git a/Changes b/Changes index 9fd3b2d..acab912 100644 --- a/Changes +++ b/Changes @@ -1,7 +1,13 @@ Revision history for e ================= -1.31 - 2024-10- +1.32 - 2024-10-30 +================= + +Added commands: sum, uniq. + +================= +1.31 - 2024-10-13 ================= Improved Build.PL. diff --git a/README.md b/README.md index e82f19e..49c8625 100644 --- a/README.md +++ b/README.md @@ -26,7 +26,7 @@ ⠹⡽⣾⣿⠹⣿⣆⣾⢯⣿⣿ ⡞ ⠻⣿⣿⣿⠁ ⢠⣿⢏ ⡀ ⡟ ⢀⣴⣿⠃⢁⡼⠁ ⠈ ⠈⠛ ⢻⣿⣧⢸⢟⠶⢾⡇ ⣸⡿⠁ ⢠⣾⡟⢼ ⣷ ⡇ ⣰⠋⠙⠁ ⠈⣿⣻⣾⣦⣇⢸⣇⣀⣶⡿⠁⣀⣀⣾⢿⡇⢸ ⣟⡦⣧⣶⠏ unleashed - ⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.31 + ⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.32 ⢻⡄ ⠐⠚⠋⣠⡾⣧⣿⠁⠙⢳⣽⡟ ⠈⠳⢦⣤⣤⣀⣤⡶⠛ ⠈⢿⡆ ⢿⡇ ⠈ ⠈⠓ ⠈ @@ -442,7 +442,7 @@ Turn string into a [Mojo::File](https://metacpan.org/pod/Mojo%3A%3AFile) object. $ perl -Me -e 'say r j f("hello.json")->slurp' -## Math Help +## List Support ### max @@ -458,6 +458,22 @@ Get the smallest number in a list. $ perl -Me -e 'say max 2,4,1,3' 1 +### sum + +Adds a list of numbers. + + $ perl -Me -e 'say sum 1..10' + 55 + +### uniq + +Get the unique values in a list. + + $ perl -Me -e 'say for uniq 2,4,4,6' + 2 + 4 + 6 + ## Output ### say diff --git a/lib/e.pm b/lib/e.pm index 4544eee..f3bd699 100644 --- a/lib/e.pm +++ b/lib/e.pm @@ -30,7 +30,7 @@ package e; ⠹⡽⣾⣿⠹⣿⣆⣾⢯⣿⣿ ⡞ ⠻⣿⣿⣿⠁ ⢠⣿⢏ ⡀ ⡟ ⢀⣴⣿⠃⢁⡼⠁ ⠈ ⠈⠛ ⢻⣿⣧⢸⢟⠶⢾⡇ ⣸⡿⠁ ⢠⣾⡟⢼ ⣷ ⡇ ⣰⠋⠙⠁ ⠈⣿⣻⣾⣦⣇⢸⣇⣀⣶⡿⠁⣀⣀⣾⢿⡇⢸ ⣟⡦⣧⣶⠏ unleashed - ⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.31 + ⠸⢿⡍⠛⠻⠿⠿⠿⠋⣠⡾⢋⣾⣏⣸⣷⡸⣇⢰⠟⠛⠻⡄ v1.32 ⢻⡄ ⠐⠚⠋⣠⡾⣧⣿⠁⠙⢳⣽⡟ ⠈⠳⢦⣤⣤⣀⣤⡶⠛ ⠈⢿⡆ ⢿⡇ ⠈ ⠈⠓ ⠈ @@ -45,7 +45,7 @@ use 5.006; use strict; use warnings; -our $VERSION = '1.31'; +our $VERSION = '1.32'; =head1 SYNOPSIS @@ -468,7 +468,7 @@ Turn string into a L object. =cut -=head2 Math Help +=head2 List Support =head3 max @@ -484,6 +484,22 @@ Get the smallest number in a list. $ perl -Me -e 'say max 2,4,1,3' 1 +=head3 sum + +Adds a list of numbers. + + $ perl -Me -e 'say sum 1..10' + 55 + +=head3 uniq + +Get the unique values in a list. + + $ perl -Me -e 'say for uniq 2,4,4,6' + 2 + 4 + 6 + =cut =head2 Output @@ -942,7 +958,7 @@ sub import { }, ###################################### - # Math Help + # List Support ###################################### max => sub { @@ -961,6 +977,29 @@ sub import { List::Util::min( @_ ); }, + sum => sub { + if ( !$imported{$caller}{"List::Util"}++ ) { + require List::Util; + } + + List::Util::sum( @_ ); + }, + + uniq => sub { + if ( !$imported{$caller}{"List::Util"}++ ) { + require List::Util; + } + + # Since uniq is missing in some recent versions. + if ( List::Util->can( "uniq" ) ) { + List::Util::uniq( @_ ); + } + else { + my %h; + grep { !$h{$_}++ } @_; + } + }, + ###################################### # Output ###################################### diff --git a/t/01-simple.t b/t/01-simple.t index 1983a27..aabdcf5 100644 --- a/t/01-simple.t +++ b/t/01-simple.t @@ -254,18 +254,25 @@ is "f - basename"; ###################################### -# Math Help +# List Support ###################################### is - max(10,20,9,15), - 20, - "max - sanity check"; + max( 10, 20, 9, 15 ), 20, + "max - sanity check"; is - min(10,20,9,15), - 9, - "min - sanity check"; + min( 10, 20, 9, 15 ), 9, + "min - sanity check"; + +is + sum( 1 .. 10 ), 55, + "sum - sanity check"; + +is_deeply + [ uniq 2, 4, 4, 6 ], + [ 2, 4, 6 ], + "uniq - sanity check"; ###################################### # Output