Course of Raku / Functional, concurrent, reactive, and web programming / Functional programming / Iterators / Exercises / Sum with pull-one
Solution: Sum with pull-one
Here is a possible solution to the task.
Code
my $it = (3, 7, 5).iterator;
my $sum = 0;
loop {
my $v := $it.pull-one;
last if $v =:= IterationEnd;
$sum += $v;
}
say $sum;🦋 You can find the source code in the file sum-with-pull-one.raku.
Output
15Comments
.iteratorgives the pull-based view of the list, and eachpull-onereturns the next number.The value is bound with
:=, not assigned, so$v =:= IterationEndcan spot the end correctly — an assigned=would compare the container instead of the value. The loop adds3,7, and5, then meetsIterationEndand stops, leaving15.