Skip to content

Instantly share code, notes, and snippets.

@masak
Created January 29, 2013 23:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save masak/4668987 to your computer and use it in GitHub Desktop.
Save masak/4668987 to your computer and use it in GitHub Desktop.
Sieve of Eratosthenes, using only lambdas
$_ = sub ($_) {
state $value = 1;
when 'value' { $value }
when 'next' { ++$value; &?ROUTINE }
when 'filter' {
sub (&substream, $factor) {
sub ($_) {
state $value = 0;
when 'value' { $value }
when 'next' { $value = &substream('next')('value') until $value % $factor; &?ROUTINE }
when 'filter' { &substream('filter') }
}
}
}
};
loop {
say .('next')('value');
$_ = .('filter')($_, .('value'));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment