For-loops | Range object

Discuss the future of the AutoHotkey language
User avatar
Flipeador
Posts: 1004
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

For-loops | Range object

20 Apr 2018, 20:10

I think we should consider the implementation of a Range object to use with For-loops.
Basically it would work exactly like Coco's Range Function.
Since it seems that Switch-case will be implemented as an alternative to if-else-if, why not implement a Range object as an alternative to Loop.
Points in favor:
:wave:
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
SirRFI
Posts: 404
Joined: 25 Nov 2015, 16:52

Re: For-loops | Range object

21 Apr 2018, 03:36

Isn't that basically a for loop? (Didn't mean AHK's for loop, which is rather for-each)
JavaScript sample (can use it in browser's console):

Code: [Select all] [Download] GeSHi © Codebox Plus

var arr = ['one', 'two', 'three', 'four'];
for (i=0; i<arr.length; i++)
console.log(arr[i]);
Use [code=autohotkey] forum tag to share your code.
Click on (Accept this answer) on top-right part of the post if it has answered your question / solved your problem.
User avatar
nnnik
Posts: 3194
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: For-loops | Range object

21 Apr 2018, 05:13

Yeah it is - except that it is more modern.
The outdated for loop you suggested would be slower when operating on arrays than the normal AutoHotkey for loop - and by a whole exponent.
Recommends AHK Studio
User avatar
jeeswg
Posts: 4959
Joined: 19 Dec 2016, 01:58
Location: UK

Re: For-loops | Range object

21 Apr 2018, 08:25

Range object. Sounds good. (I'm wondering if any other functions for generating objects would be useful.)
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: For-loops | Range object

21 Apr 2018, 21:34

What's stopping you from making it yourself?

"Performance" is overrated. If you want performance iterating over a range of integers, allocating an enumerator object (built-in or not) is not the way to get it.

nnnik wrote:The outdated for loop you suggested would be slower when operating on arrays than the normal AutoHotkey for loop - and by a whole exponent.
For(;;) would perform similarly to Loop Count, which is not much slower than for-in on an array.

For(;;) is not outdated. It is more general purpose and lower overhead than for-in. (It is also more obscure/harder to explain to new/non programmers than Loop Count.)

Flipeador wrote:You can even avoid creating a temporary variable in some cases.
I guess you mean that you can avoid having to assign i := A_Index when using nested loops. With for temp_variable in range(...), you cannot avoid creating a temporary variable (and on top of that, a temporary object).
User avatar
Flipeador
Posts: 1004
Joined: 15 Nov 2014, 21:31
GitHub: Flipeador
Location: Argentina
Contact:

Re: For-loops | Range object

21 Apr 2018, 21:55

lexikos wrote:What's stopping you from making it yourself?

What do you mean? Make my own version of AHK or use a function?
If you mean the first, then in that case I would not have created this topic. It's just a suggestion to improve AHK and from which we can all take advantage. I understand if you do not think it necessary. You have more experience and every update that you make seems very serious and correct, I will not discuss it much. In case I have a better and stronger point in favor, I will let you know. :)

I guess you mean that you can avoid having to assign i := A_Index when using nested loops. With for temp_variable in range(...), you cannot avoid creating a temporary variable (and on top of that, a temporary object).

Yes ... it would be the same as in this case:

Code: [Select all] [Download] (Untitled.ahk)GeSHi © Codebox Plus

Loop 8
{
n := (A_Index - 1) * 4
NumPut(0xFFFFFF, &STRUCT + n, "UInt")

; do more stuff with n ...
}


Regards!
Windws 1♂ Pro 64-Bits I make scripts for AHKv2 (my v2 compiler) & WIN_7+ Spanish Argentina SublimeText 3 & AHKv2 My GDI+ Library
User avatar
nnnik
Posts: 3194
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: For-loops | Range object

22 Apr 2018, 01:45

lexikos the loop itself is not slower but inside the loop you will have to perform look-ups inside the loop body in order to iterate over the array.
The for loop used the arrays iterator to access it.
A single look-up has a complexity of O( log(n) ) .
The iterator takes a constant time to get the next Element O( 1 ).
Both loops get every value inside the array, meaning they repeat the action of getting the arrays value.
Ignoring all other code the complexity of iterating over an array is O( n * log(n)) for the normal loop and O(n) for the for loop.
Only if the array has a single element it seems like the normal loop is faster. ( tested out in practice )
Recommends AHK Studio
lexikos
Posts: 6129
Joined: 30 Sep 2013, 04:07
GitHub: Lexikos

Re: For-loops | Range object

22 Apr 2018, 02:28

I was comparing a for loop which stores each value in a variable to a Loop Count which stores each value in a variable, and my conclusion was backed by benchmarks in v1. Your theory does not cover all of the factors, such as the overhead of creating the enumerator object and invoking it via dynamic dispatch on each iteration. Even if both had O(n) complexity, they would not be equivalent.

I never said the normal loop would be faster; I said that it would not be much slower. The performance is close enough to be irrelevant (or more to the point, the time that either completes in is small enough to be irrelevant for most scripts). There are more important factors, such as for-in being potentially more readable, or Loop Count being able to adjust for removed elements.

Actually, there is a bigger difference in performance on v2. Both methods are faster on v2, but especially for-in.

Flipeador, I mean write a script.
User avatar
nnnik
Posts: 3194
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: For-loops | Range object

22 Apr 2018, 03:49

That's true I never talked about variables - when working on arrays for in is the way to go.
The question is if the single use case of Loop, count warrants a whole new type of Loop.
But as far as I can tell t that will have to wait for v3.
The for loop actually really seems outdated to me.
At least I don't see common uses for it in modern languages (and no C++ is not a modern language to me, I was thinking about Python and Java)
Recommends AHK Studio
User avatar
jeeswg
Posts: 4959
Joined: 19 Dec 2016, 01:58
Location: UK

Re: For-loops | Range object

24 Apr 2018, 03:49

- I had a look at various traditional for loop possibilities here:
traditional for loop: for i = a to b (step c) possibilities - AutoHotkey Community
https://autohotkey.com/boards/viewtopic.php?f=5&t=43022

- I believe that the Range object would be a nice solution:

Code: [Select all] [Expand] [Download] GeSHi © Codebox Plus


- Re. performance, in theory, 'for loop Range' lines could be optimised at the script start.

- It's a bit like switch-case statements that I didn't personally want, or ObjCount which I did, simple functionality that isn't strictly necessary but that a lot of people would benefit from or consider standard in some form, and that can make scripts easier to read and maintain.

- An advantage of Range is that it more directly/succinctly expresses the idea that you want to express: start, stop, step. All other alternatives are essentially workarounds which will vary from user to user and which obscure the intention of the programmer.
User avatar
nnnik
Posts: 3194
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: For-loops | Range object

24 Apr 2018, 08:43

I think for index in range with only one variable seems better.
Recommends AHK Studio
User avatar
jeeswg
Posts: 4959
Joined: 19 Dec 2016, 01:58
Location: UK

Re: For-loops | Range object

24 Apr 2018, 10:53

- Yes nnnik, it looks nice.
- But what about it being inconsistent with every other time you use a for loop? :?:
- And inconsistent with any custom Range functions/variants.
- Also, sometimes it can be useful to specify both the equivalent of A_Index (as a key), and the value obtained from Range() (as a value).
- What about a symbol, that if used for the key, prevents a variable being created for it:
for ~, vValue in oArray
I don't urgently need this functionality, but I suppose it could improve performance. And it avoids an unnecessary extra variable needing to be defined as local in a custom function.
User avatar
nnnik
Posts: 3194
Joined: 30 Sep 2013, 01:01
Location: Germany

Re: For-loops | Range object

24 Apr 2018, 15:38

I think allowing for custom enumerators which take a variable amount of input variables would be a nice addition for AHK v2
Recommends AHK Studio

Return to “AutoHotkey v2 Development”

Who is online

Users browsing this forum: No registered users and 3 guests