Skip to content

Commit

Permalink
- Methods that can return one or more values, now return a list, inst…
Browse files Browse the repository at this point in the history
…ead of an array.

This methods are: String.unpack(), Array.get(), Hash.get(), Caller.*, Func.*, Pipe.command, Socket::*.

- Number.divmod() now returns a list with two elements, instead of an array.

Example:
	var (d,m) = 17.divmod(5);  # returns: (3, 2)

- Fixed a minor bug in Range for numbers between 2**31 and 2**63; now they are unpacked correctly when they are Math::Big* objects.
- Method Math.map() is now faster.
- Pair.first() and Pair.second() no longer take arguments.
  • Loading branch information
trizen committed Jul 15, 2015
1 parent 814ae15 commit d0fdf0f
Show file tree
Hide file tree
Showing 16 changed files with 42 additions and 44 deletions.
4 changes: 3 additions & 1 deletion lib/Sidef/Math/Math.pm
Original file line number Diff line number Diff line change
Expand Up @@ -231,10 +231,12 @@ package Sidef::Math::Math {

return $array if $step == 0;

my @values;
for (my $i = $from ; $i < $to ; $i += $step) {
$array->push(Sidef::Types::Number::Number->new($i));
push @values, Sidef::Types::Number::Number->new($i);
}

$array->push(@values);
$array;
}

Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Module/Caller.pm
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ package Sidef::Module::Caller {
}

if (@results > 1) {
return Sidef::Types::Array::Array->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
return Sidef::Types::Array::List->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
}

my $result = $results[0];
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Module/Func.pm
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ package Sidef::Module::Func {
}

if (@results > 1) {
return Sidef::Types::Array::Array->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
return Sidef::Types::Array::List->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
}

Sidef::Perl::Perl->to_sidef($results[0]);
Expand Down
9 changes: 7 additions & 2 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -447,8 +447,13 @@ package Sidef::Types::Array::Array {
}

sub get {
my ($self, $index) = @_;
exists($self->[$index->get_value]) ? $self->[$index->get_value]->get_value : ();
my ($self, @indices) = @_;

if (@indices > 1) {
return Sidef::Types::Array::List->new(map { exists($self->[$_]) ? $self->[$_]->get_value : undef } @indices);
}

@indices && exists($self->[$indices[0]]) ? $self->[$indices[0]]->get_value : ();
}

*item = \&get;
Expand Down
6 changes: 1 addition & 5 deletions lib/Sidef/Types/Array/Pair.pm
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,7 @@ package Sidef::Types::Array::Pair {
no strict 'refs';
foreach my $pair ([first => 0], [second => 1]) {
*{__PACKAGE__ . '::' . $pair->[0]} = sub {
my ($self, $arg) = @_;
if (@_ > 1) {
$self->[$pair->[1]] = $arg;
}
$self->[$pair->[1]];
$_[0]->[$pair->[1]];
};
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/Sidef/Types/Array/Range.pm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,10 @@ package Sidef::Types::Array::Range {

if ($self->{direction} eq 'up') {
if ($step == 1 and not $limit > (-1 >> 1) and not $from > (-1 >> 1)) {

# Unpack limit
$limit = $limit->bstr if ref($limit);

foreach my $i ($from .. $limit) {
if (defined(my $res = $code->_run_code(Sidef::Types::Number::Number->new($i)))) {
return $res;
Expand Down
5 changes: 1 addition & 4 deletions lib/Sidef/Types/Glob/Pipe.pm
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,7 @@ package Sidef::Types::Glob::Pipe {

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

$#{$self} == 0
? $self->[0]
: Sidef::Types::Array::Array->new(@{$self});
@{$self} > 1 ? Sidef::Types::Array::List->new(@{$self}) : $self->[0];
}

sub open {
Expand Down
9 changes: 5 additions & 4 deletions lib/Sidef/Types/Glob/Socket.pm
Original file line number Diff line number Diff line change
Expand Up @@ -156,10 +156,11 @@ package Sidef::Types::Glob::Socket {
my $result = $func->(map { $_->get_value } @args);
@results = $result;
}
if (@results > 1) {
return Sidef::Types::Array::Array->new(map { Sidef::Perl::Perl->to_sidef($_) } @results);
}
return Sidef::Perl::Perl->to_sidef($results[0]);
return (
@results > 1
? Sidef::Types::Array::List->new(map { Sidef::Perl::Perl->to_sidef($_) } @results)
: Sidef::Perl::Perl->to_sidef($results[0])
);
}

warn qq{[WARN] Inexistent Socket method "$name"!\n};
Expand Down
13 changes: 4 additions & 9 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,6 @@ package Sidef::Types::Hash::Hash {
if (ref($pairs[0]) eq 'Sidef::Types::Block::Code') {
return $pairs[0]->to_hash;
}

# Default value only for: Hash.new(obj);
#if (ref($pairs[0]) ne 'Sidef::Types::Array::Pair') {
# $self->default(shift @pairs);
# return $self;
#}
}

# Add hash key/value pairs
Expand Down Expand Up @@ -76,11 +70,12 @@ package Sidef::Types::Hash::Hash {
sub get {
my ($self, @keys) = @_;

if ($#keys == 0) {
return $self->{data}{$keys[0]};
if (@keys > 1) {
return Sidef::Types::Array::List->new(map { exists($self->{data}{$_}) ? $self->{data}{$_}->get_value : undef }
@keys);
}

Sidef::Types::Array::Array->new(map { defined($_) ? $_->get_value : $_ } @{$self->{data}}{@keys});
@keys && exists($self->{data}{$keys[0]}) ? $self->{data}{$keys[0]}->get_value : ();
}

sub length {
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 @@ -167,7 +167,7 @@ package Sidef::Types::Number::Number {

sub divmod {
my ($self, $num) = @_;
Sidef::Types::Array::Array->new($self->div($num)->int, $self->mod($num));
Sidef::Types::Array::List->new($self->div($num)->int, $self->mod($num));
}

sub factorial {
Expand Down
8 changes: 3 additions & 5 deletions lib/Sidef/Types/String/String.pm
Original file line number Diff line number Diff line change
Expand Up @@ -719,11 +719,9 @@ package Sidef::Types::String::String {
*tr = \&translit;

sub unpack {
my ($self, $argv) = @_;
my @parts = CORE::unpack($self->get_value, $argv->get_value);
$#parts == 0
? __PACKAGE__->new($parts[0])
: Sidef::Types::Array::Array->new(map { __PACKAGE__->new($_) } @parts);
my ($self, $arg) = @_;
my @values = map { __PACKAGE__->new($_) } CORE::unpack($self->get_value, $arg->get_value);
@values > 1 ? Sidef::Types::Array::List->new(@values) : $values[0];
}

sub pack {
Expand Down
4 changes: 2 additions & 2 deletions lib/Sidef/Variable/Struct.pm
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@ package Sidef::Variable::Struct {
sub DESTROY { }

sub AUTOLOAD {
my ($self, $argv) = @_;
my ($self, $arg) = @_;

my ($name) = ($AUTOLOAD =~ /^.*[^:]::(.*)$/);

# Variable autovification
if (not exists $self->{$name}) {
return $self->{$name} = Sidef::Variable::Variable->new(name => '', type => 'var', value => $argv);
return $self->{$name} = Sidef::Variable::Variable->new(name => '', type => 'var', value => $arg);
}

$self->{$name};
Expand Down
2 changes: 1 addition & 1 deletion lib/Sidef/Variable/Variable.pm
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ package Sidef::Variable::Variable {

sub _stack_depth {
my ($self) = @_;
exists($self->{stack}) ? $#{$self->{stack}} + 1 : -1;
Sidef::Types::Number::Number->new(exists($self->{stack}) ? $#{$self->{stack}} + 1 : -1);
}

sub _stack_vals {
Expand Down
4 changes: 2 additions & 2 deletions scripts/Expensive/long_multiplication.sf
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func long_multiplication(a is String, b is String) -> String {
ylen.range.each { |j|
xlen.range.each { |i|
var n = (x[i]*y[j] + mem);
var(d, m) = n.divmod(10)...;
var(d, m) = n.divmod(10);
if (i == xlen) {
map[j].append(m, d);
mem = 0;
Expand All @@ -54,7 +54,7 @@ func long_multiplication(a is String, b is String) -> String {
n != 0 && result.append(n);
}
else {
n.divmod(10) » (\mem, \result[result.end+1]);
[n.divmod(10)] » (\mem, \result[result.end+1]);
}
}

Expand Down
4 changes: 2 additions & 2 deletions scripts/Interactive/socket.sf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

# A very basic, low-level, web-server.

var port = 8080;
var port = 8081;
var protocol = Socket.getprotobyname( "tcp" );

var sock = (Socket.open(Socket::PF_INET, Socket::SOCK_STREAM, protocol) || die "couldn't open a socket: #{$!}");
Expand All @@ -19,7 +19,7 @@ sock.bind(Socket.sockaddr_in(port, Socket::INADDR_ANY)) || die "couldn't bind so
sock.listen(Socket::SOMAXCONN) || die "couldn't listen to port #{port}: #{$!}";
# start listening for incoming connections

while(var client = sock.accept){
while (var client = sock.accept) {
client.print ("HTTP/1.1 200 OK\r\n" +
"Content-Type: text/html; charset=UTF-8\r\n\r\n" +
"<html><head><title>Goodbye, world!</title></head>" +
Expand Down
8 changes: 4 additions & 4 deletions scripts/objects_init.sf
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ array.join(", ").say;
## Pipe object
#

var pipe_h = (Pipe.new("ls -x").open_r);
var pipe_h = (Pipe.new('ls', '-x').open_r);

var line;
while (line = (pipe_h.readline) != nil) {
"'%s': %s".printf(pipe_h.parent.command, line); break;
pipe_h.each { |line|
"'%s': %s".printf(join(' ', pipe_h.parent.command), line);
break;
}

0 comments on commit d0fdf0f

Please sign in to comment.