Skip to content

Commit

Permalink
- Added basic support for the gather/take construct
Browse files Browse the repository at this point in the history
Example:
	var arr = gather {
  	  3.times {
        	take(_**2);
	    };

	    3.times {
	        take(_**3);
	    }
	};
	say arr;   # prints: [1, 4, 9, 1, 8, 27]

Recursion is not currently supported inside the block of gather, so the following example is not working correctly:

Example:
	func fib(n) {
	    var arr = gather {
	        take(n <= 1 ? n : fib(n-1)+fib(n-2));
	    };

	    return arr[0];
	}

	say fib(12);		# prints 4 instead of 144
  • Loading branch information
trizen committed Aug 13, 2015
1 parent ad1cbe6 commit b4e5de6
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 1 deletion.
1 change: 1 addition & 0 deletions MANIFEST
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ lib/Sidef/Types/Block/For.pm
lib/Sidef/Types/Block/For.pod
lib/Sidef/Types/Block/Fork.pm
lib/Sidef/Types/Block/Fork.pod
lib/Sidef/Types/Block/Gather.pm
lib/Sidef/Types/Block/Given.pm
lib/Sidef/Types/Block/Given.pod
lib/Sidef/Types/Block/Next.pm
Expand Down
3 changes: 3 additions & 0 deletions META.json
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@
"Sidef::Types::Block::Fork" : {
"file" : "lib/Sidef/Types/Block/Fork.pm"
},
"Sidef::Types::Block::Gather" : {
"file" : "lib/Sidef/Types/Block/Gather.pm"
},
"Sidef::Types::Block::Given" : {
"file" : "lib/Sidef/Types/Block/Given.pm"
},
Expand Down
2 changes: 2 additions & 0 deletions META.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ provides:
file: lib/Sidef/Types/Block/For.pm
Sidef::Types::Block::Fork:
file: lib/Sidef/Types/Block/Fork.pm
Sidef::Types::Block::Gather:
file: lib/Sidef/Types/Block/Gather.pm
Sidef::Types::Block::Given:
file: lib/Sidef/Types/Block/Given.pm
Sidef::Types::Block::Next:
Expand Down
21 changes: 20 additions & 1 deletion lib/Sidef/Parser.pm
Original file line number Diff line number Diff line change
Expand Up @@ -189,14 +189,18 @@ package Sidef::Parser {
for foreach
if while
given switch
gather
continue
import
include
eval
read
die
warn
assert assert_eq assert_ne
assert
assert_eq
assert_ne
my
var
Expand Down Expand Up @@ -1253,6 +1257,21 @@ package Sidef::Parser {
return $obj;
}

if (/\Ggather\h*(?=\{)/gc) {
my $obj = Sidef::Types::Block::Gather->new();

local $self->{current_gather} = $obj;

my $block = $self->parse_block(code => $opt{code});
$obj->{block} = $block;

return scalar {$self->{class} => [{self => $obj, call => [{method => 'gather'}]}]};
}

if (exists($self->{current_gather}) and /\G(?=take\b)/) {
return $self->{current_gather}, 1;
}

# Inside a class context
if (exists $self->{current_class}) {

Expand Down
25 changes: 25 additions & 0 deletions lib/Sidef/Types/Block/Gather.pm
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Sidef::Types::Block::Gather {

use 5.014;

sub new {
my (undef, $block) = @_;
bless {block => $block}, __PACKAGE__;
}

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

local $self->{values} = [];

sub take {
my ($self, @args) = @_;
push @{$self->{values}}, @args;
}

$self->{block}->run;
Sidef::Types::Array::Array->new(@{$self->{values}});
}
}

1;

0 comments on commit b4e5de6

Please sign in to comment.