Skip to content

Commit

Permalink
- Added the Array.grep_kv{} method
Browse files Browse the repository at this point in the history
- Renamed Array.each_with_index{} to Array.each_kv{}
- Renamed Array.map_with_index{} to Array.map_kv{}
- Added the Hash.each_kv{} alias for Hash.each{}
- Minor optimization inside Hash.sort_by{} -- no longer converts twice a key into a String object.
  • Loading branch information
trizen committed Dec 18, 2015
1 parent dad0453 commit 0cc0dcf
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 71 deletions.
2 changes: 1 addition & 1 deletion bin/sidef
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ else {

my $header =
"\nuse lib ("
. join(', ', map { qq{"\Q$_\E"} } @INC) . ");\n"
. join(', ', map { qq{"\Q$_\E"} } @INC) . ");\n\n"
. "use Sidef;\n\n"
. "binmode(STDIN, ':utf8');\n"
. "binmode(STDOUT, ':utf8');\n"
Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Object/Object.pod
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ Return the

=cut

=head2 bless

Object.bless() -> I<Obj>

Return the

=cut

=head2 class

Object.class() -> I<Obj>
Expand Down
31 changes: 21 additions & 10 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -703,7 +703,9 @@ package Sidef::Types::Array::Array {
$self;
}

sub each_with_index {
*each_key = \&each_index;

sub each_kv {
my ($self, $code) = @_;

foreach my $i (0 .. $#{$self}) {
Expand All @@ -715,16 +717,14 @@ package Sidef::Types::Array::Array {
$self;
}

*each_kv = \&each_with_index;

sub map {
my ($self, $code) = @_;
$self->new(map { $code->run($_) } @{$self});
}

*collect = \&map;

sub map_with_index {
sub map_kv {
my ($self, $code) = @_;

my @arr;
Expand All @@ -735,9 +735,7 @@ package Sidef::Types::Array::Array {
$self->new(@arr);
}

*map_kv = \&map_with_index;
*collect_kv = \&map_with_index;
*collect_with_index = \&map_with_index;
*collect_kv = \&map_kv;

sub flat_map {
my ($self, $code) = @_;
Expand All @@ -752,6 +750,20 @@ package Sidef::Types::Array::Array {
*filter = \&grep;
*select = \&grep;

sub grep_kv {
my ($self, $code) = @_;

my @arr;
foreach my $i (0 .. $#{$self}) {
push(@arr, $self->[$i]) if $code->run(Sidef::Types::Number::Number->new($i), $self->[$i]);
}

$self->new(@arr);
}

*filter_kv = \&grep_kv;
*select_kv = \&grep_kv;

sub group_by {
my ($self, $code) = @_;

Expand Down Expand Up @@ -1579,9 +1591,8 @@ package Sidef::Types::Array::Array {

*remove_last_if = \&delete_last_if;

sub to_list {
@{$_[0]};
}
sub to_list { @{$_[0]} }
*as_list = \&to_list;

sub dump {
my ($self) = @_;
Expand Down
68 changes: 45 additions & 23 deletions lib/Sidef/Types/Array/Array.pod
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ I<Obj> B<...> I<Obj> -> I<Obj>

Return the

Aliases: I<to_list>
Aliases: I<as_list>, I<to_list>

=cut

Expand Down Expand Up @@ -347,17 +347,27 @@ Return the

=cut

=head2 each_index
=head2 each_key

Array.each_index() -> I<Obj>
Array.each_key() -> I<Obj>

Return the

Aliases: I<each_index>

=cut

=head2 each_kv

Array.each_kv() -> I<Obj>

Return the

=cut

=head2 each_with_index
=head2 each_slice

Array.each_with_index() -> I<Obj>
Array.each_slice() -> I<Obj>

Return the

Expand Down Expand Up @@ -409,17 +419,19 @@ Return the

=cut

=head2 flat_map
=head2 flat

Array.flat_map() -> I<Obj>
Array.flat() -> I<Obj>

Return the

Aliases: I<flatten>

=cut

=head2 flatten
=head2 flat_map

Array.flatten() -> I<Obj>
Array.flat_map() -> I<Obj>

Return the

Expand Down Expand Up @@ -465,6 +477,16 @@ Aliases: I<filter>, I<select>

=cut

=head2 grep_kv

Array.grep_kv() -> I<Obj>

Return the

Aliases: I<filter_kv>, I<select_kv>

=cut

=head2 group_by

Array.group_by() -> I<Obj>
Expand Down Expand Up @@ -615,22 +637,22 @@ Aliases: I<collect>

=cut

=head2 map_operator
=head2 map_kv

Array.map_operator() -> I<Obj>
Array.map_kv() -> I<Obj>

Return the

Aliases: I<collect_kv>

=cut

=head2 map_with_index
=head2 map_operator

Array.map_with_index() -> I<Obj>
Array.map_operator() -> I<Obj>

Return the

Aliases: I<collect_with_index>

=cut

=head2 max
Expand Down Expand Up @@ -699,6 +721,14 @@ Return the

=cut

=head2 pairmap

Array.pairmap() -> I<Obj>

Return the

=cut

=head2 pairs

Array.pairs() -> I<Obj>
Expand Down Expand Up @@ -799,14 +829,6 @@ Return the

=cut

=head2 reduce_pairs

Array.reduce_pairs() -> I<Obj>

Return the

=cut

=head2 resize

Array.resize() -> I<Obj>
Expand Down
35 changes: 21 additions & 14 deletions lib/Sidef/Types/Hash/Hash.pm
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ package Sidef::Types::Hash::Hash {
push @list, $key, $val;
}

while (my ($key, $val) = each %{$obj}) {
while (my ($key, $val) = each %$obj) {
push @list, $key, $val;
}

Expand Down Expand Up @@ -281,21 +281,20 @@ package Sidef::Types::Hash::Hash {
Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($key), $value);
}

*each_kv = \&each;
*each_pair = \&each;

sub sort_by {
my ($self, $code) = @_;

my @array;
foreach my $key (CORE::keys %$self) {
push @array, [$key, $code->run(Sidef::Types::String::String->new($key), $self->{$key})];
my $str = Sidef::Types::String::String->new($key);
push @array, [$key, $str, $code->run($str, $self->{$key})];
}

Sidef::Types::Array::Array->new(
map {
Sidef::Types::Array::Array->new(Sidef::Types::String::String->new($_->[0]), $self->{$_->[0]})
} (sort { $a->[1] cmp $b->[1] } @array)
);
Sidef::Types::Array::Array->new(map { Sidef::Types::Array::Array->new($_->[1], $self->{$_->[0]}) }
(sort { $a->[2] cmp $b->[2] } @array));
}

sub to_a {
Expand All @@ -312,7 +311,7 @@ package Sidef::Types::Hash::Hash {

sub exists {
my ($self, $key) = @_;
Sidef::Types::Bool::Bool->new(exists $self->{$key});
Sidef::Types::Bool::Bool->new(CORE::exists $self->{$key});
}

*has_key = \&exists;
Expand All @@ -325,8 +324,8 @@ package Sidef::Types::Hash::Hash {
my ($self) = @_;

my $new_hash = $self->new();
@{$new_hash}{map { $_->get_value } CORE::values %$self} =
(map { Sidef::Types::String::String->new($_) } CORE::keys %$self);
@{$new_hash}{CORE::values %$self} =
(map { Sidef::Types::String::String->new($_) } CORE::keys %$self);

$new_hash;
}
Expand All @@ -338,6 +337,13 @@ package Sidef::Types::Hash::Hash {
Storable::dclone($self);
}

sub to_list {
my ($self) = @_;
map { (Sidef::Types::String::String->new($_) => $self->{$_}) } keys %$self;
}

*as_list = \&to_list;

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

Expand Down Expand Up @@ -373,10 +379,11 @@ package Sidef::Types::Hash::Hash {
{
no strict 'refs';

*{__PACKAGE__ . '::' . '+'} = \&concat;
*{__PACKAGE__ . '::' . '=='} = \&eq;
*{__PACKAGE__ . '::' . '!='} = \&ne;
*{__PACKAGE__ . '::' . ':'} = \&new;
*{__PACKAGE__ . '::' . '+'} = \&concat;
*{__PACKAGE__ . '::' . '=='} = \&eq;
*{__PACKAGE__ . '::' . '!='} = \&ne;
*{__PACKAGE__ . '::' . ':'} = \&new;
*{__PACKAGE__ . '::' . '...'} = \&to_list;
}
};

Expand Down
12 changes: 11 additions & 1 deletion lib/Sidef/Types/Hash/Hash.pod
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ Aliases: I<merge>, I<concat>

=cut

=head2 ...

I<Obj> B<...> I<Obj> -> I<Obj>

Return the

Aliases: I<as_list>, I<to_list>

=cut

=head2 :

I<Obj> B<:> I<Obj> -> I<Obj>
Expand Down Expand Up @@ -130,7 +140,7 @@ Hash.each() -> I<Obj>

Return the

Aliases: I<each_pair>
Aliases: I<each_kv>, I<each_pair>

=cut

Expand Down
16 changes: 0 additions & 16 deletions lib/Sidef/Types/Number/Complex.pod
Original file line number Diff line number Diff line change
Expand Up @@ -444,14 +444,6 @@ Aliases: I<as_int>

=cut

=head2 is_even

Complex.is_even() -> I<Obj>

Return the

=cut

=head2 is_inf

Complex.is_inf() -> I<Obj>
Expand Down Expand Up @@ -492,14 +484,6 @@ Aliases: I<is_negative>

=cut

=head2 is_odd

Complex.is_odd() -> I<Obj>

Return the

=cut

=head2 is_one

Complex.is_one() -> I<Obj>
Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/greatest_subsequential_sum.sf
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

func maxsubseq(*a) {
var (start, end, sum, maxsum) = (-1, -1, 0, 0);
a.each_with_index { |i, x|
a.each_kv { |i, x|
sum += x;
if (maxsum < sum) {
maxsum = sum;
Expand Down
2 changes: 1 addition & 1 deletion scripts/Tests/read_a_configuration_file.sf
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ say "favouritefruit = #{favouritefruit}";
say "needspeeling = #{needspeeling}";
say "seedsremoved = #{seedsremoved}";

otherfamily.each_with_index {|i, name|
otherfamily.each_kv {|i, name|
say "otherfamily(#{i+1}) = #{name}";
};

Expand Down
Loading

0 comments on commit 0cc0dcf

Please sign in to comment.