-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Added basic support for the gather/take construct
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
Showing
5 changed files
with
51 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |