Skip to content

Commit

Permalink
- Added the Array .run_length method.
Browse files Browse the repository at this point in the history
Optionally, the method also takes a "by-block".

Example:

	say [1,1,1,2,3,3].run_length		#=> [[1, 3], [2, 1], [3, 2]]
	say %w(a a b C c c).run_length { .lc }	#=> [['a', 2], ['b', 1], ['C', 3]]
  • Loading branch information
trizen committed Dec 20, 2019
1 parent f9298eb commit b5a7558
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 6 deletions.
35 changes: 34 additions & 1 deletion lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -1713,6 +1713,39 @@ package Sidef::Types::Array::Array {

*stack_by = \&stack;

sub run_length {
my ($self, $block) = @_;

$block //= Sidef::Types::Block::Block::IDENTITY;
@$self || return bless [];

my @result = bless [$self->[0], 1];
my $prev_value = $block->run($self->[0]);

foreach my $i (1 .. $#{$self}) {

my $item = $self->[$i];
my $curr_value = $block->run($item);

if ($curr_value eq $prev_value) {
++$result[-1][1];
}
else {
CORE::push(@result, bless [$item, 1]);
}

$prev_value = $curr_value;
}

foreach my $pair (@result) {
$pair->[1] = Sidef::Types::Number::Number->_set_uint($pair->[1]);
}

bless \@result;
}

*run_length_by = \&run_length;

sub match {
my ($self, $regex) = @_;

Expand Down Expand Up @@ -2395,7 +2428,7 @@ package Sidef::Types::Array::Array {
return (
$len > 0
? bless([map { $self->[CORE::rand($len)] } 1 .. $amount], ref($self))
: bless([], ref($self))
: bless([], ref($self))
);
}

Expand Down
10 changes: 10 additions & 0 deletions lib/Sidef/Types/Array/Array.pod
Original file line number Diff line number Diff line change
Expand Up @@ -1602,6 +1602,16 @@ Aliases: I<rscalar_operator>

=cut

=head2 run_length

Array.run_length() -> I<Obj>

Return the

Aliases: I<run_length_by>

=cut

=head2 sadd

Array.sadd() -> I<Obj>
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Types/Glob/File.pm
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,8 @@ package Sidef::Types::Glob::File {
sub readlink {
ref($_[0]) || shift(@_);
my ($self) = @_;
my $link = "$self";
my $class = (-d $link) ? 'Sidef::Types::Glob::Dir' : __PACKAGE__;
my $link = "$self";
my $class = (-d $link) ? 'Sidef::Types::Glob::Dir' : __PACKAGE__;
$class->new(CORE::readlink($link));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Regex/Match.pm
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ package Sidef::Types::Regex::Match {
else {
$hash{pos} = CORE::int($hash{pos} // 0);
$hash{pos} = 0 if ($hash{pos} < 0);
@captures = substr($hash{string}, $hash{pos}) =~ $hash{regex}{regex};
@captures = substr($hash{string}, $hash{pos}) =~ $hash{regex}{regex};

$hash{matched} = (@captures != 0);
$hash{match_pos} = $hash{matched} ? [$-[0] + $hash{pos}, $+[0] + $hash{pos}] : [];
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ package Sidef::Types::String::String {
sub numbers {
my ($self) = @_;
Sidef::Types::Array::Array->new(
[map { Sidef::Types::Number::Number->new($_) }
[map { Sidef::Types::Number::Number->new($_) }
grep { Scalar::Util::looks_like_number($_) } CORE::split(' ', $$self)
]
);
Expand All @@ -707,7 +707,7 @@ package Sidef::Types::String::String {
sub integers {
my ($self) = @_;
Sidef::Types::Array::Array->new(
[map { Sidef::Types::Number::Number->new($_)->int }
[map { Sidef::Types::Number::Number->new($_)->int }
grep { Scalar::Util::looks_like_number($_) } CORE::split(' ', $$self)
]
);
Expand Down

0 comments on commit b5a7558

Please sign in to comment.