Skip to content

Commit

Permalink
- Number.range() is no longer inclusive when no argument is provided …
Browse files Browse the repository at this point in the history
…for the upper limit.

Example:
	10.range()    # returns a range from 0 to 9 (inclusive)

Number.range(n) has NOT been affected.

Example:
	0.range(10)   # returns a range from 0 to 10 (inclusive)
  • Loading branch information
trizen committed Dec 17, 2015
1 parent e7c3756 commit 6bc3074
Show file tree
Hide file tree
Showing 10 changed files with 21 additions and 17 deletions.
2 changes: 2 additions & 0 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,8 @@ package Sidef::Types::Array::Array {
$self->new(@new_array);
}

*flat = \&flatten;

sub exists {
my ($self, $index) = @_;
Sidef::Types::Bool::Bool->new(exists $self->[$index->get_value]);
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ package Sidef::Types::Number::Number {

defined($to)
? $self->to($to, $step)
: $from->to($self);
: $from->to($self->dec);
}

sub sqrt {
Expand Down
6 changes: 3 additions & 3 deletions scripts/Expensive/long_multiplication.sf
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ func long_multiplication(String a, String b) -> String {
var mem = 0;
var map = y.len.of { Array.new };

ylen.range.each { |j|
xlen.range.each { |i|
y.range.each { |j|
x.range.each { |i|
var n = (x[i]*y[j] + mem);
var(d, m) = n.divmod(10);
if (i == xlen) {
Expand All @@ -48,7 +48,7 @@ func long_multiplication(String a, String b) -> String {
var mrange = (0 .. map.end);
var end = (xlen + ylen + 1);

end.range.each { |i|
(end+1).range.each { |i|
var n = (mrange.map {|j| map[j][i] }.sum + mem);
if (i == end) {
n != 0 && result.append(n);
Expand Down
2 changes: 1 addition & 1 deletion scripts/Games/bulls_and_cows_player.sf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func read_score(guess) {
func score_correct(a, b, bulls, cows) {
var (exact, loose) = (0, 0);

3.range.each { |i|
4.range.each { |i|
a[i] == b[i] ? ++exact
: (b.contains(a[i]) && ++loose)
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/Graphical/ulam_spiral.sf
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ var white = %s'Imager::Color'.new('#FFFFFF');
var img = %s'Imager'.new(xsize => n, ysize => n, channels => 1);
img.box(filled => 1, color => white);

(n-1).range.each { |y|
(n-1).range.each { |x|
n.range.each { |y|
n.range.each { |x|
var v = cell(n, x, y, start);
%S'ntheory'.is_prime(v) && (
img.setpixel(x => x, y => y, color => black)
Expand Down
2 changes: 1 addition & 1 deletion scripts/Project Euler/0001-multiples_of_3_and_5.sf
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@

# https://projecteuler.net/problem=1

say +(1000-1).range.grep{ (_ %% 3) || (_ %% 5) }.sum;
say 1000.range.grep{ (_ %% 3) || (_ %% 5) }.sum;
12 changes: 7 additions & 5 deletions scripts/Tests/cramers_method.sf
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ var free_terms = [4, -6, 5];

func diag_multiply_sum (matrix, left) {

var range = matrix[0].range; # range from 0 to len(matrix[0])-1
var r1 = matrix[0].range; # range from 0 to len(matrix[0])-1
var products = [];

(left ? range.reverse : range).each { |i|
(left ? r1.reverse : r1).each { |i|

var x = 0;
var nums = [];
var rangex = CORE::range(i, matrix[0].end+i); # range from i to len(matrix[0])-1+i
var r2 = i.to(matrix[0].end+i); # range from i to len(matrix[0])-1+i

(left ? rangex.reverse : rangex).each { |j|
(left ? r2.reverse : r2).each { |j|
nums.push(matrix[j][x++]);
};

Expand All @@ -39,7 +39,7 @@ func diag_multiply_sum (matrix, left) {
}

func make_det_matrix (matrix) {
matrix + [matrix.@[range(matrix.end-1)]];
matrix + [matrix[matrix.end.range...]];
}

func calculate_delta (matrix, free_terms, position) {
Expand Down Expand Up @@ -97,3 +97,5 @@ indices.each {
var i = _[1];
"%s == %3d/%-3d == %3s\n".printf(_[0], xyz[i], delta, xyz[i] / delta);
}

assert_eq(xyz, [6,3,9]);
2 changes: 1 addition & 1 deletion scripts/Tests/run_length_encoding_3.sf
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ func encode(str) {
func decode(str) {
var chars = str.chars;
var r = '';
(chars.len/2 - 1 -> int).range.each { |i|
(chars.len/2 -> int).range.each { |i|
r += (chars[2*i + 1] * chars[2*i].ord);
}
return r;
Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/subtractive_generator.sf
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class SubRandom(seed, state=[]) {
53.times {
s.append((s[-2] - s[-1]) % mod);
}
state = range(s).map {|i| s[(34 + 34*i) % 55] };
state = s.range.map {|i| s[(34 + 34*i) % 55] };
range(55, 219).each { self.subrand };
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/aks_test_for_primes.sf
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ func coef(n, e) {
}

func binpoly(p) {
join(" ", coef(1, p), range(p-1).reverse.map {|i|
join(" ", coef(1, p), p.range.reverse.map {|i|
join(" ", %w(+ -)[(p-i)&1], coef(binomial(p, i), i));
}...);
}

say "expansions of (x-1)^p:";
range(9).each { |i| say binpoly(i) };
10.range.each { |i| say binpoly(i) };
say "Primes to 80: [#{(2..80).grep { binprime(_) }}]";

0 comments on commit 6bc3074

Please sign in to comment.