Enable while & until?
Enable while & until?
@lexikos Have you considered enabling until for use with while loops, or is that too confusing?
Re: Enable while & until?
Have you an example to illustrate how it could be effective?
For While "the expression is evaluated once before each iteration", for Until, "The expression is evaluated once after each iteration", so double the cost.
For While "the expression is evaluated once before each iteration", for Until, "The expression is evaluated once after each iteration", so double the cost.
itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Re: Enable while & until?
@lmstearn It seems strange to ask for a while-until construction at first, but I started thinking in terms of head and tail. Generally, the head is the first few elements of some iterable like a list or a string, and the tail stands for "the rest".
For example, in this function which makes aesthetic text into AESTHETIC TEXT:
into:
It looks nicer I suppose, but it seems to be a zero-cost change to enable until for while loops.
For example, in this function which makes aesthetic text into AESTHETIC TEXT:
Aesthetic Function
I could move the deconstruction of the string outside the loop
Code: Select all
while c := Ord(RegExReplace(s, "^(.)", "$1")) { ; head
... do something here ...
s := RegExReplace(s, "^" Chr(c)) ; tail
}
Code: Select all
while c := Ord(RegExReplace(s, "^(.)", "$1")) ; head
... do something here ...
until not (s := RegExReplace(s, "^" Chr(c))) ; tail
Re: Enable while & until?
It looks very much like a attractive case for the inclusion, we can also look at it this way:
To reduce the impact of cost, ensure there is no overlapping in header or tail conditions, if you like - i.e. at each iteration the two evaluated conditions are always independent of each other.
In fact, one might even gain more real estate with a new WhileUntil statement.
Given a string like aesthetic text which can always be placed into an array, and consequently your suggested conversion above performed through a For, each, we know it breaks down to something like
Again this has a cost, and to the degree it exceeds or falls short of the other always has a solution, and is best measured in unit tests.
We may be further convinced it's a must for the language given the following considerations:
Code: Select all
While Some_Condition_to_Continue_Iterating
{
}
Until Some_Condition_Not_To_Proceed_Further
In fact, one might even gain more real estate with a new WhileUntil statement.
Code: Select all
WhileUntil Some_Condition_to_Continue_Iterating, Some_Condition_Not_To_Proceed_Further
{
}
Code: Select all
_enum := Expression
try _enum := _enum.__Enum(2)
while _enum(&Value1, &Value2)
{
...
}
We may be further convinced it's a must for the language given the following considerations:
- The reasons why it was never implemented or suggested for V1.
- Other languages I personally have come across don't have it - if they exist, that's a plus for the wish.
- Simplicity. - Will reading programs that contain While/Until loops be more or less confusing than before, or more difficult for users to debug?
itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
Re: Enable while & until?
I'm not really sure about the examples you posted, but my original suggestion is a zero-cost solution, assuming while/for/loop all share the same underlying code. I'm not looking to extend the language beyond it's existing merits.
Re: Enable while & until?
Some handling for the Until statement and evaluation of the condition is duplicated across each type of loop, so there would be a small cost to supporting While..Until.
Until cannot belong to While, and changing that now would cause issues with nested Loop/While.
Until cannot belong to While, and changing that now would cause issues with nested Loop/While.
-
- Posts: 48
- Joined: 11 May 2020, 20:45
Re: Enable while & until?
Why not just use Loop and Until?
Examples from Until:
Examples from Until:
Code: Select all
Loop
x *= 2
Until x > y
Code: Select all
Loop Read, A_ScriptFullPath
lines .= A_LoopReadLine . "`n"
Until A_Index=5 ; Read the first five lines.
Until can be used with any Loop or For.
Re: Enable while & until?
The point of the request is to be able to have a condition that is checked at the beginning of each loop iteration and another condition that is checked at the end of each loop iteration. There might be a condition for which you don’t want to execute the loop even once unless it’s true (While), and another condition that you want to check only after the body of the loop has executed (Until).Marium0505 wrote: ↑ Why not just use Loop and Until?
Who is online
Users browsing this forum: No registered users and 18 guests