Enable while & until?

Propose new features and changes
iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Enable while & until?

22 Jan 2024, 17:22

@lexikos Have you considered enabling until for use with while loops, or is that too confusing?
User avatar
lmstearn
Posts: 695
Joined: 11 Aug 2016, 02:32
Contact:

Re: Enable while & until?

22 Jan 2024, 22:44

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.
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
User avatar
V2User
Posts: 195
Joined: 30 Apr 2021, 04:04

Re: Enable while & until?

23 Jan 2024, 07:09

I agree.

Also refer to this post:
viewtopic.php?f=37&t=125199&p=556337#p556337
iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Re: Enable while & until?

23 Jan 2024, 20:29

@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:
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
}
into:

Code: Select all

while c := Ord(RegExReplace(s, "^(.)", "$1")) ; head
   ... do something here ...
until not (s := RegExReplace(s, "^" Chr(c))) ; tail
It looks nicer I suppose, but it seems to be a zero-cost change to enable until for while loops.
User avatar
lmstearn
Posts: 695
Joined: 11 Aug 2016, 02:32
Contact:

Re: Enable while & until?

24 Jan 2024, 07:56

It looks very much like a attractive case for the inclusion, we can also look at it this way:

Code: Select all

While Some_Condition_to_Continue_Iterating
{
}
Until Some_Condition_Not_To_Proceed_Further
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.

Code: Select all

WhileUntil Some_Condition_to_Continue_Iterating, Some_Condition_Not_To_Proceed_Further
{
}
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

Code: Select all

_enum := Expression
try _enum := _enum.__Enum(2)
while _enum(&Value1, &Value2)
{
    ...
}
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:
  • 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?
:arrow: itros "ylbbub eht tuO kaerB" a ni kcuts m'I pleH
iseahound
Posts: 1446
Joined: 13 Aug 2016, 21:04
Contact:

Re: Enable while & until?

25 Jan 2024, 12:08

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.
lexikos
Posts: 9592
Joined: 30 Sep 2013, 04:07
Contact:

Re: Enable while & until?

15 Feb 2024, 20:21

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.
Marium0505
Posts: 40
Joined: 11 May 2020, 20:45

Re: Enable while & until?

15 Feb 2024, 20:59

Why not just use Loop and 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.
User avatar
boiler
Posts: 16978
Joined: 21 Dec 2014, 02:44

Re: Enable while & until?

20 Feb 2024, 04:32

Marium0505 wrote: Why not just use Loop and 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).

Return to “Wish List”

Who is online

Users browsing this forum: No registered users and 119 guests