diff options
Diffstat (limited to 'challenge-001/alexander-pankoff')
| -rw-r--r-- | challenge-001/alexander-pankoff/perl/lib/My/List/Util.pm | 45 | ||||
| -rw-r--r-- | challenge-001/alexander-pankoff/perl/lib/My/String/Util.pm | 32 |
2 files changed, 77 insertions, 0 deletions
diff --git a/challenge-001/alexander-pankoff/perl/lib/My/List/Util.pm b/challenge-001/alexander-pankoff/perl/lib/My/List/Util.pm new file mode 100644 index 0000000000..81be4560e7 --- /dev/null +++ b/challenge-001/alexander-pankoff/perl/lib/My/List/Util.pm @@ -0,0 +1,45 @@ +package My::List::Util; + +use strict; +use warnings; +use feature qw'signatures'; +no warnings qw'experimental::signatures'; + +use List::Util qw(any min); + +use Exporter qw(import); + +our @EXPORT_OK = qw(flatten zip zip_with chunks_of without); + +# flatten : [[a]] -> [a] +sub flatten(@xs) { + return map { @{$_} } @xs; +} + +# zip : [a] -> [b] -> [[a,b]] +sub zip ( $as, $bs ) { + return map { [ $as->[$_], $bs->[$_] ] } 0 .. min( $#{$as}, $#{$bs} ); +} + +# zip_with : (a -> b -> c) -> [a] -> [b] -> [c] +sub zip_with ( $fn, $as, $bs ) { + return map { $fn->( $as->[$_], $bs->[$_] ) } 0 .. min( $#{$as}, $#{$bs} ); +} + +# splits a list into chunks of size $size +# chunks_of : Int -> [a] -> [[a]] +sub chunks_of ( $size, @xs ) { + return if !@xs; + my $chunk = [ @xs[ 0 .. min( ( $size - 1 ), $#xs ) ] ]; + return ( $chunk, chunks_of( $size, @xs[ $size .. $#xs ] ) ); +} + +# returns a list with all elements from a set are not in $bs. +# (Elements compared with string equality) +# without : [a] -> [a] -> [a] +sub without ( $as, $bs ) { + grep { + my $a = $_; + !any { $_ eq $a } @$bs + } @$as; +} diff --git a/challenge-001/alexander-pankoff/perl/lib/My/String/Util.pm b/challenge-001/alexander-pankoff/perl/lib/My/String/Util.pm new file mode 100644 index 0000000000..8f305a242c --- /dev/null +++ b/challenge-001/alexander-pankoff/perl/lib/My/String/Util.pm @@ -0,0 +1,32 @@ +package My::String::Util; + +use strict; +use warnings; +use feature qw'signatures'; +no warnings qw'experimental::signatures'; + +use Exporter qw(import); + +our @EXPORT_OK = qw(explode implode contains str_map); + +# explode : String -> [Char] +sub explode($str) { + return split( m//, $str ); +} + +# implode : [Char] -> String +sub implode(@chars) { + return join( '', @chars ); +} + +# contains : String -> Char -> Bool +sub contains ( $str, $char ) { + return $str =~ m/\Q$char\E/; +} + +# str_map: (Char -> Char) -> String -> String +sub str_map ( $fn, $str ) { + return implode( map { $fn->($_) } explode($str) ); +} + +1; |
