Skip to content

Commit

Permalink
- Added auto-conversion support for blessed objects inside the Perl.t…
Browse files Browse the repository at this point in the history
…o_sidef() method.
  • Loading branch information
trizen committed Sep 9, 2015
1 parent 22e3cbd commit 43e1f00
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 27 deletions.
7 changes: 1 addition & 6 deletions lib/Sidef/Module/OO.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ package Sidef::Module::OO {
return Sidef::Types::Array::List->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
}

my $result = $results[0];
if (ref($result) && eval { $result->can('can') }) {
return $self->__NEW__($result);
}

Sidef::Perl::Perl->to_sidef($result);
Sidef::Perl::Perl->to_sidef($results[0]);
}
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Sidef/Perl/Perl.pm
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ package Sidef::Perl::Perl {
return Sidef::Types::String::String->new($val);
}

# Return an OO object when $val is blessed
state $x = require Scalar::Util;
if (defined Scalar::Util::blessed($val)) {
return Sidef::Module::OO->__NEW__($val);
}

$val;
};

Expand Down
37 changes: 16 additions & 21 deletions scripts/Rosettacode/Graphical/Ulam_spiral_[for_primes].sf
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,32 @@
## http://rosettacode.org/wiki/Ulam_spiral_(for_primes)
#

var Imager = require('Imager');
var ntheory = frequire('ntheory');
 
var n = (ARGV ? ARGV.shift.to_i : 512);
var start = (ARGV ? ARGV.shift.to_i : 1);
var file = "ulam.png";
 
require('Imager');
require('ntheory');

var (n=512, start=1, file='ulam.png') = ARGV»to_i»()...;

func cell(n, x, y, start) {
y -= (n >> 1);
x -= (n-1 >> 1);
var l = 2*(x.abs > y.abs ? x.abs : y.abs);
var d = (y > x  ? (l*3 + x + y) : (l - x - y));
var l = 2*(x.abs > y.abs ? x.abs : y.abs);
var d = (y > x ? (l*3 + x + y) : (l - x - y));
(l-1)**2 + d + start - 1;
}
 
var Imager::Color = 'Imager::Color'.to_caller;
 
var black = Imager::Color.new('#000000');
var white = Imager::Color.new('#FFFFFF');
 
var img = Imager.new(xsize => n, ysize => n, channels => 1);

var black = %s'Imager::Color'.new('#000000');
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|
var v = cell(n, x, y, start);
ntheory.is_prime(v) && (
%S'ntheory'.is_prime(v) && (
img.setpixel(x => x, y => y, color => black)
);
}
}
 
img.write(file => file)
|| die "Cannot write `#{file}': #{img.errstr}";

img.write(file => file);

0 comments on commit 43e1f00

Please sign in to comment.