I'm looking at filtering ranges, and getting a little confused. In D, I can write this code:
import std.stdio;
import std.range;
import std.algorithm;
auto filterNums(int[] vals)
{
int limit = 3;
return filter!(n => n >limit)(vals);
}
int main()
{
int[] nums = [1,2,3,4,5];
auto flt = filterNums(nums);
foreach(n;flt)
{
writeln(n);
}
return 0;
}
which gives the expected output of:
4
5
But this doesn't seem to be terribly safe code. If the filter is lazy evaluating, how does it know that limit is 3, once the local variable goes out of scope? Is my code just lucky that nothing else has over-ridden limit in memory? Also is the passed variable nums a reference, or a copied value: if a value, is the filter making its own copy?
Or perhaps I am not using filter in the correct way?