What Exactly Does += Do

Regex what exactly does [ ] do?

Its because of ASCII Tables

/[0-z] captures all ASCII values from 48 to 122

Sample Image

[A-Za-z0-9] does not

Sample Image

What exactly does % do in JavaScript?

It's the modulus operator.

This returns "the first operand modulo the second operand", which is the remainder when you substract (or add) as much of the second operand off/to of the first, to get as close as possible to 0.

Here's an example:

 5  % 2 ==  1  ( 5  = 2*2   +1)
6 % 2 == 0 ( 6 = 2*3 +0)
12 % 5 == 2 ( 12 = 5*2 +2)
-5 % 2 == -1 (-5 = 2*-2 -1)
-6 % 2 == 0 (-6 = 2*-3 -0)
-12 % 5 == -2 (-12 = 5*-2 -2)
// ^ That's the result of the modulo.

What exactly does .*? do in regex? .*?([a-m/]*).*

The ? here acts as a 'modifier' if I can call it like that and makes .* match the least possible match (termed 'lazy') until the next match in the pattern.

In fall/2005, the first .*? will match up to the first match in ([a-m/]*), which is just before f. Hence, .*? matches 0 characters so that ([a-m/]*) will match fall/ and since ([a-m/]*) cannot match anymore, the next part of the pattern .* matches what's left in the string, meaning 2005.

In contrast to .*([a-m/]*).*, you would have .* match as much as possible first (meaning the whole string) and try to go back to make the other terms match. Except that the problem is with the other quantifiers being able to match 0 characters as well, so that .* alone will match the whole string (termed 'greedy').


Maybe a different example will help.

.*ab

In:

aaababaaabab

Here, .* will match as much characters as possible and then try to match ab. Thus, .* will match aaababaaab and the remainder will be matched by ab.

.*?ab

In:

aaababaaabab

Here, .*? will match as little as possible until it can match ab in that regex. The first occurrence of ab is here:

aaababaaabab
^^

And so, .*? matches aa while ab will match ab.

What exactly DOES a Lambda do?

In your example lambda x: x+1 will take an argument x and return x+1, equivalent to def foo(x): return x+1, these are also called anonymous functions.

Lambdas are extremely useful while passing a function as an argument. Let's give you an example. Suppose you have a 2D list of tuples:

l = [('a',2),('b',3),('c',1)]

And you want to sort the list using the second item in each tuple. Manually it will take a lot of code to do this manually, but with lambdas you can pass a lambda function as a key to sort method or sorted() like this:

l.sort(key=lambda x: x[1])

And there are more examples!!!

What exactly does `return` do in AutoHotKey?

In AHK return is what you'd learn return to be from other programming languages. It's just that control flow, and in general the legacy syntax, in AHK is different from what you might be used to from other languages.

How is return different from exit?

If we're not trying to return a value from a function, and are just interested in the control flow, there is not much difference. But the difference is there.

You could indeed end an hotkey label with exit, and it would be the same as ending it with return.

From the documentation:

"If there is no caller to which to return, Return will do an Exit instead.".

I guess it's just convention to end hotkey labels with return.

So basically said, there is difference between the two, if return isn't about to do an exit.

Well when would this be?

For example:

MsgBox, 1
function()
MsgBox, 3
return ;this does an exit

function()
{
MsgBox, 2
return ;this does a return
}

The first return has no caller to which to return to, so it will do an exit.

The second return however does have a caller to return to do, so it will return there, and then execute the 3rd message box.

If we replaced the second return with an exit, the current thread would just be exited and the 3rd message box would never have been shown.

Here's the same exact example with legacy labels:

MsgBox, 1
gosub, label
MsgBox, 3
return ;this does an exit

label:
MsgBox, 2
return ;this does a return

I'm showing this, because they're very close to hotkey labels, and hotkey labels were something you were wondering a lot about.

In this specific example, if the line MsgBox, 3 didn't exist, replacing the second return with an exit would produce the same end result. But only because code execution is about to end anyway on the first return.



If return doesn't play like a closing bracket, then why does it appear in many places one expects a closing bracket?

In AHK v1, hotkeys and hotstrings work like labels, and labels are legacy. Labels don't care about { }s like modern functions do.

So we need something to stop the code execution. Return does that.

Consider the following example:

c::
MsgBox, % "Hotkey triggered!"
gosub, run_chrome
;code execution not ended

n::
MsgBox, % "Hotkey triggered!`nRunning notepad"
Run, notepad
;code execution not ended

run_chrome:
MsgBox, % "Running chrome"
run, chrome
WinWait, ahk_exe chrome.exe
WinMove, ahk_exe chrome.exe, , 500, 500
;code execution not ended

show_system_uptime:
MsgBox, % "System uptime: " A_TickCount " ms"
;code execution not ended

We're not ending code execution at any of the labels. Because of this, code execution will bleed into other parts of the code, in this case, into the other labels.

Try running the hotkeys and see how code execution bleeds into the other labels.

Because of this, the code execution needs to be ended somehow.

If using { } was supported by labels, it would indeed be the solution to keep the code execution where it needs to be.

And in fact AHK v2 hotkeys and hotstrings are no longer labels (they're only lookalikes). In v2 using { } is actually the correct way (the script wont even run if you don't use them).

See hotkeys from the AHK v2 documentation.



Related Topics



Leave a reply



Submit