Permalink
Browse files

Int.base

  • Loading branch information...
1 parent afce340 commit adcf1edf7e95f0f695e928543efa12010309f639 @moritz moritz committed Jul 22, 2011
Showing with 16 additions and 2 deletions.
  1. +14 −0 src/core/Int.pm
  2. +2 −2 t/spectest.data
View
@@ -36,6 +36,20 @@ my class Int {
method pred(Int:D:) { self - 1 }
method sqrt() { self.Num.sqrt }
+
+ method base(Cool $base) {
+ fail("base must be between 2 and 36, got $base") unless 2 <= $base <= 36;
+ my Int $b = $base.Int;
+ my @conversion = 0..9, 'A' .. 'Z';
+ my @res;
+ my $n = self.abs;
+ repeat {
+ push @res, @conversion[$n % $b];
+ $n div= $b;
+ } while $n > 0;
+ push @res, '-' if self < 0;
+ join '', @res.reverse;
+ }
}
multi prefix:<++>(Int:D \$a is rw) { # XXX
View
@@ -506,12 +506,12 @@ S32-list/map_function_return_values.t
# S32-list/minmax.t # err: Could not find sub &min
S32-list/pick.t
# S32-list/reduce.t # err: Could not find sub &reduce
-# S32-list/reverse.t # err: Could not find sub &reverse
+S32-list/reverse.t
S32-list/roll.t
# S32-list/sort.t # err: Could not find sub &sort
# S32-list/uniq.t # err: Method 'uniq' not found for invocant of class 'Array'
S32-num/abs.t
-# S32-num/base.t # err: Method 'base' not found for invocant of class 'Int'
+S32-num/base.t
S32-num/complex.t
# S32-num/cool-num.t # err: Method 'abs' not found for invocant of class 'Str'
S32-num/exp.t # err: Too many positional parameters passed; got 2 but expected 1

0 comments on commit adcf1ed

Please sign in to comment.