Skip to content

Commit

Permalink
- Added the Array .segment_by { ... } method.
Browse files Browse the repository at this point in the history
Example:

	# Segment the array after each prime
	say @(1..prime(5)).segment_by { .is_prime }

Output:

	[[1, 2], [3], [4, 5], [6, 7], [8, 9, 10, 11]]
  • Loading branch information
trizen committed Oct 29, 2019
1 parent 31d1e6d commit 968c96f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
17 changes: 17 additions & 0 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -419,9 +419,26 @@ package Sidef::Types::Array::Array {
$prev_i = $i + 1;
}

if ($prev_i <= $end) {
CORE::push(@parts, bless [@{$self}[$prev_i .. $end]]);
}

bless \@parts;
}

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

my @indices;
foreach my $i (0 .. $#$self) {
if ($block->run($self->[$i])) {
CORE::push(@indices, $i);
}
}

$self->segment(@indices);
}

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

Expand Down
7 changes: 3 additions & 4 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -8221,17 +8221,16 @@ package Sidef::Types::Number::Number {
Math::GMPz::Rmpz_sqrt($t, $n);
Math::GMPz::Rmpz_fits_ulong_p($t) || goto &nan; # too large

my $L = 0;
my $sqrt = Math::GMPz::Rmpz_get_ui($t);

my $L = 0;

foreach my $k (1 .. $sqrt) {
if ($k * $k < ULONG_MAX) {
Math::GMPz::Rmpz_div_ui($t, $n, $k * $k);
Math::GMPz::Rmpz_tdiv_q_ui($t, $n, $k * $k);
}
else {
Math::GMPz::Rmpz_ui_pow_ui($t, $k, 2);
Math::GMPz::Rmpz_div($t, $n, $t);
Math::GMPz::Rmpz_tdiv_q($t, $n, $t);
}
$L += Math::GMPz::Rmpz_get_si(${mertens($t)}); # most of the time is spent here
}
Expand Down

0 comments on commit 968c96f

Please sign in to comment.