Elisions
Less typing means faster programming, and simple syntactic changes can save a lot of keystrokes. The newest Freya syntax allows you to drop the do and then keywords when they precede a statement starting with another keyword.
In this fragment, both the for and the while statements have dropped their do's:
method GetPrimes(Max: Integer): BitsArray;
begin
Result := new BitsArray(2, Max);
for i in 2..Max div 2 : not Result[i]
begin
var j := i + i;
while j <= Max
begin
Result[j] := true;
j += i;
end;
end;
Result.Invert;
end;
Note that we have also dropped the for var combination that used to mark local variable type inference in previous versions.
There is another interesting detail in the above code:
for i in 2..Max div 2 : not Result[i]
First, there's a seemingly innocent variation of the classical for/to numerical loop: we have disguised it as if we were iterating over a numerical range. We still cannot eliminate the classical statement, since this "iterator" does not substitute the downto loop.
However, the real purpose of this change has to do with the Boolean expression after the semicolon. It acts as a filter, and it can be used both with virtual numerical ranges and with real iterators.
At the end, an iterating filter translate as a nested if statement... but you have saved some typing, and your code is a little more expressive, since you don't have to look for a matching else before understanding the purpose of that nested if.
0 Comments:
Post a Comment
<< Home