Skip to content

Commit

Permalink
- Added the Number.divides() method which returns true when the first…
Browse files Browse the repository at this point in the history
… arguments divides the second argument.

Example:
	6.divides(30);	# true

- Changed the Array.minmax() method to return a list instead of a new array.

Example:
	var(min, max) = [1,2,3,4].minmax;   # (1,4)
  • Loading branch information
trizen committed Aug 22, 2015
1 parent 6b360b3 commit 80899b1
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 25 deletions.
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -354,7 +354,7 @@ package Sidef::Types::Array::Array {

sub minmax {
my ($self) = @_;
$self->new($self->min, $self->max);
Sidef::Types::Array::List->new($self->min, $self->max);
}

sub sum {
Expand Down
45 changes: 24 additions & 21 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,6 @@ package Sidef::Types::Number::Number {
$self->new($self->get_value->copy->bdec);
}

sub lt {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($self->get_value < $num->get_value);
}

sub gt {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($self->get_value > $num->get_value);
}

sub and {
my ($self, $num) = @_;
$self->new($self->get_value->as_int->band($num->get_value->as_int));
Expand Down Expand Up @@ -134,6 +124,16 @@ package Sidef::Types::Number::Number {
__PACKAGE__->new($self->get_value->bacmp($num->get_value));
}

sub gt {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($self->get_value > $num->get_value);
}

sub lt {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($self->get_value < $num->get_value);
}

sub ge {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($self->get_value >= $num->get_value);
Expand Down Expand Up @@ -424,15 +424,6 @@ package Sidef::Types::Number::Number {
*isPos = \&is_positive;
*is_pos = \&is_positive;

sub is_inf {
my ($self) = @_;
Sidef::Types::Bool::Bool->new($self->get_value->is_inf);
}

*isInf = \&is_inf;
*is_infinite = \&is_inf;
*isInfinite = \&is_inf;

sub is_negative {
my ($self) = @_;
Sidef::Types::Bool::Bool->new($self->get_value->is_neg);
Expand All @@ -456,6 +447,15 @@ package Sidef::Types::Number::Number {

*isOdd = \&is_odd;

sub is_inf {
my ($self) = @_;
Sidef::Types::Bool::Bool->new($self->get_value->is_inf);
}

*isInf = \&is_inf;
*is_infinite = \&is_inf;
*isInfinite = \&is_inf;

sub is_integer {
my ($self) = @_;
Sidef::Types::Bool::Bool->new($self->get_value->is_int);
Expand Down Expand Up @@ -570,12 +570,16 @@ package Sidef::Types::Number::Number {

sub is_div {
my ($self, $num) = @_;

Sidef::Types::Bool::Bool->new($self->get_value % $num->get_value == 0);
}

*isDiv = \&is_div;

sub divides {
my ($self, $num) = @_;
Sidef::Types::Bool::Bool->new($num->get_value % $self->get_value == 0);
}

sub commify {
my ($self) = @_;

Expand Down Expand Up @@ -615,7 +619,6 @@ package Sidef::Types::Number::Number {

sub shift_right {
my ($self, $num, $base) = @_;

$self->new($self->get_value->copy->brsft($num->get_value, (defined($base) ? $base->get_value : ())));
}

Expand Down
1 change: 0 additions & 1 deletion lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ package Sidef::Types::String::String {

sub cmp {
my ($self, $string) = @_;

Sidef::Types::Number::Number->new($self->get_value cmp $string->get_value);
}

Expand Down
2 changes: 1 addition & 1 deletion scripts/Rosettacode/Interactive/Sparkline_in_unicode.sf
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ var bar = '▁'..'█';
loop {
print 'Numbers, please, separated by space/commas: ';
var numbers = read(String).trim.split(/[\s,]+/).map{.to_f};
var (min, max) = numbers.minmax...;
var (min, max) = numbers.minmax;
say "min: %5f; max: %5f"%[min, max];
var div = ((max - min) / bar.offset);
say (min == max ? bar.last*numbers.len : numbers.map{|num| bar[(num - min) / div]}.join);
Expand Down
2 changes: 1 addition & 1 deletion scripts/Rosettacode/Sorting_algorithms_Radix_sort.sf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
class Array {
method radix_sort(base=10) {
var arr = self.copy;
var rounds = (arr.minmax.map{.abs}.max -> log(base).floor + 1);
var rounds = ([arr.minmax].map{.abs}.max -> log(base).floor + 1);
0 .. rounds-1 each { |i|
var buckets = (2*base of {[]});
var base_i = base**i;
Expand Down

0 comments on commit 80899b1

Please sign in to comment.