> CUSTOM FUNCTION DIRECT TO OBJECT
(and custom class direct to object)
- something like this that can handle a complete function cf. fat arrow functions:- the same may be potentially useful for classes oClass := class ...Code: Select all
oFunc := NewFunc(arg1, arg2, arg3) { return arg1 arg2 arg3 }
- otherwise you can get clashes with existing functions/classes for simple temporary code
I've since seen that this can also be very useful for functions for filtering lines (keeping/omitting lines), and modifying(/not modifying) lines.
For permanent functions, I'd rather store them in one place. For temporary functions, I'd rather keep the code together with other relevant code, not have proper named functions dotted about the place in random locations, and not pollute the global namespace, hence the advantage of storing them direct to object.
Here are some examples for filtering/modifying/sorting:
Code: Select all
q:: ;filter/modify text using a filter/modifier function - keep/modify if contain vowels
vText := "
(
abcde
fghij
klmno
pqrst
uvwxy
z
)"
oFilter := Func("MyFilter")
MyFilter(vText)
{
return !!RegExMatch(vText, "i)[aeiou]")
}
oModifier := Func("MyModifier")
MyModifier(vText)
{
return (RegExMatch(vText, "i)[aeiou]") ? "`t" : "") vText
}
MsgBox, % LineFilter(vText, oFilter)
MsgBox, % LineModifier(vText, oModifier)
return
LineFilter(vText, oFunc)
{
local
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2*2)
Loop, Parse, vText, `n, `r
{
if %oFunc%(A_LoopField)
vOutput .= A_LoopField "`r`n"
}
return SubStr(vOutput, 1, -2)
}
LineModifier(vText, oFunc)
{
local
vOutput := ""
VarSetCapacity(vOutput, StrLen(vText)*2+2*2)
Loop, Parse, vText, `n, `r
vOutput .= %oFunc%(A_LoopField) "`r`n"
return SubStr(vOutput, 1, -2)
}
;==================================================
w:: ;sort using a custom function
vText := "
(
Item 100
Item 20
Item 3
)"
;the Sort function would be defined here
;direct to an object (to not interfere with the global namespace)
Sort, vText
MsgBox, % vText
Sort, vText, F SortStrCmpLogical
MsgBox, % vText
return
SortStrCmpLogical(vTextA, vTextB, vOffset) ;for use with AHK's Sort command
{
local
vRet := DllCall("shlwapi\StrCmpLogicalW", "WStr",vTextA, "WStr",vTextB)
if (vRet = 0)
return -vOffset
else
return vRet
}
;==================================================
Here is some example code for doing something similar in JavaScript. You can save the code to an htm file, and open the htm file, to run it.
Code: Select all
<script>
//5+5=10
//5-5=0
//5*5=25
//5/5=1
//5**5=3125
for(i = 1; i <= 1; i++) //loop 1 time
{
var myfunc = function(num1, num2)
{
return num1 + num2;
}
confirm(myfunc(5, 5));
var myfunc = function(num1, num2)
{
return num1 - num2;
}
confirm(myfunc(5, 5));
var myfunc = function(num1, num2)
{
return num1 * num2;
}
confirm(myfunc(5, 5));
var myfunc = function(num1, num2)
{
return num1 / num2;
}
confirm(myfunc(5, 5));
var myfunc = function(num1, num2)
{
return Math.pow(num1, num2);
}
confirm(myfunc(5, 5));
}
</script>
Thanks for reading.