Skip to content

Commit

Permalink
- Added the Array.each_index{} method.
Browse files Browse the repository at this point in the history
modified:   scripts/sierpinski_sidef.sf
  • Loading branch information
trizen committed Jun 20, 2015
1 parent 513af71 commit e950204
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
12 changes: 12 additions & 0 deletions lib/Sidef/Types/Array/Array.pm
Original file line number Diff line number Diff line change
Expand Up @@ -487,6 +487,18 @@ package Sidef::Types::Array::Array {
*for = \&each;
*foreach = \&each;

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

foreach my $i (0 .. $#{$self}) {
if (defined(my $res = $code->_run_code(Sidef::Types::Number::Number->new($i)))) {
return $res;
}
}

$self;
}

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

Expand Down
8 changes: 8 additions & 0 deletions lib/Sidef/Types/Array/Array.pod
Original file line number Diff line number Diff line change
Expand Up @@ -442,6 +442,14 @@ Returns an object of type: C<Sidef::Types::String::String>

=cut

=head2 each_index

Array.each_index() -> I<Obj>

Return the

=cut

=head2 each_with_index

Array.each_with_index() -> I<Obj>
Expand Down
8 changes: 4 additions & 4 deletions scripts/sierpinski_sidef.sf
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,10 @@ class Sierpinski {
# *
method letter_i(n) {
var c = ['*'];
{
c = (c.map{|x| x*n } +
c.map{|x| x*n } +
c.map{|x| x*n }
{ |i|
c = (c.map{|x| x*i } +
c.map{|x| x*i } +
c.map{|x| x*i }
);
} * n;
return c;
Expand Down

0 comments on commit e950204

Please sign in to comment.