Skip to content

Instantly share code, notes, and snippets.

@marcoarthur
Last active April 28, 2024 16:49
Show Gist options
  • Save marcoarthur/5445a1dca652ed1e1ba2bf43234d3c74 to your computer and use it in GitHub Desktop.
Save marcoarthur/5445a1dca652ed1e1ba2bf43234d3c74 to your computer and use it in GitHub Desktop.
Generators and iterators
# JavaScript translation of a simple generator example, where pagination
# technique is necessary in order to process a big list of itens, possibly in
# async manner.
#
# The JS code was generated by ChatGPT
# ../../GPT/Questions/generators_and_iterators_js.md
# we translate this js example to a Perl version
use Mojo::Base -strict, -signatures, -async_await;
sub chunkGenerator($data, $chunkSize) {
return sub {
if (@$data) {
return [splice @$data,0, $chunkSize];
} else {
return;
}
};
}
my $largeDataset = [map { $_ + 1 } 1..10**3];
my $chunkSize = 100;
my $iterator = chunkGenerator($largeDataset, $chunkSize);
async sub processChunks {
while ( my $chunk = $iterator->() ){
my $processed_chunk = await processChunk($chunk);
say "RESULTS: @$processed_chunk";
}
}
async sub processChunk($chunk) {
say "Processing chunk...";
my $rand = sub { return int(rand(10))/10 };
my $processed_chunk = [ map {$_*$_} @$chunk ]; #just square it
Mojo::Promise->new->timer( $rand->() => $processed_chunk );
}
processChunks
->then( sub { say "Process completed" } )
->catch( sub ($err) { say "Error occured: $err" } )
->wait;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment