Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 04/22/2014 in all areas

  1. Function Reference _AdlibEnhance.au3 Adlib function with support for parameters, pause and resume using Call Back! Sintax: _Adlib_Register( "Function" [, "Params" [, Time [, RepeatCount ]]] ) _Adlib_Pause( "Function" ) _Adlib_Resume( "Function" ) _Adlib_SetTimer( "Function" [, Time ] ) _Adlib_UnRegister( "Function" ) Supports: ; You can call functions with parameters and native functions also! Downloads: Version: 0.10 _AdlibEnhance_(RedirectLink).html Note: Usage example is included! Sample: Fixes: Regards, João Carlos.
    1 point
  2. If you are coming from the realm of C# 3.0 then lamda expressions should be pretty familiar to you as it's basic syntactic sugar for creating a function with a conditional return. Since functions in AutoIt are first class objects, the following code should make some sense. It passes an array of integers to a function called IsTrueForAll() that checks whether the function object matches the return condition of that function (you passed). So for example a function called IsGreaterThanOrEqualTo10() checks if all values are greater than or equal to 10 (TRUE). Similarly, IsLessThan10() checks if all values are less than 10 (FALSE). This example should be nothing new to those of you who use v3.3.10.0+ and have an understanding of Call(). #include <MsgBoxConstants.au3> Local $aArray[] = [10, 100, 99, 67] If IsTrueForAll($aArray, IsGreaterThanOrEqualTo10) Then MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf If IsTrueForAll($aArray, IsLessThan10) Then MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf Func IsTrueForAll($aArray, $hFunc) ; Loop through the array and check the function passed with a single param matches the condition. For $i = 0 To UBound($aArray) - 1 If Not $hFunc($aArray[$i]) Then Return False EndIf Next Return True EndFunc ;==>IsTrueForAll Func IsGreaterThanOrEqualTo10($x) Return $x >= 10 EndFunc ;==>IsGreaterThanOrEqualTo10 Func IsLessThan10($x) Return $x < 10 EndFunc ;==>IsLessThan10 But, we could easily just forget about writing the functions (less typing is always nice) and let the compiler or pre-processor do all the work for us. It would simply use the lambda expression and create the function for us with whatever we specified. Like so... #include <MsgBoxConstants.au3> Local $aArray[] = [10, 100, 99, 67] If IsTrueForAll($aArray, $x => $x >= 10) Then ; $x is the parameter, => tells us it's a lambda expression and then the condition we are checking. MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf If IsTrueForAll($aArray, $x => $x < 10) Then ; $x is the parameter, => tells us it's a lambda expression and then the condition we are checking. MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf Func IsTrueForAll($aArray, $hFunc) ; Loop through the array and check the function passed with a single param matches the condition. For $i = 0 To UBound($aArray) - 1 If Not $hFunc($aArray[$i]) Then Return False EndIf Next Return True EndFunc ;==>IsTrueForAll...which would create the following code with anonymous functions (they're anonymous as we don't care about them in our main script)... #include <MsgBoxConstants.au3> Local $aArray[] = [10, 100, 99, 67] If IsTrueForAll($aArray, D3F7B1B92177415CA70C7FFC35C2649C) Then MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf If IsTrueForAll($aArray, DA06B548ABF4045AA32F805E6651004) Then MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.') Else MsgBox($MB_SYSTEMMODAL, '', 'Condition was False') EndIf Func IsTrueForAll($aArray, $hFunc) For $i = 0 To UBound($aArray) - 1 If Not $hFunc($aArray[$i]) Then Return False EndIf Next Return True EndFunc ;==>IsTrueForAll Func D3F7B1B92177415CA70C7FFC35C2649C($x) Return $x >= 10 EndFunc ;==>D3F7B1B92177415CA70C7FFC35C2649C Func DA06B548ABF4045AA32F805E6651004($x) Return $x < 10 EndFunc ;==>DA06B548ABF4045AA32F805E6651004 Example of parsing a lambda expression and replacing in the chosen script: #include <WinAPICom.au3> ; Script read from a file. Local $sScript = "#include <MsgBoxConstants.au3>" & @CRLF $sScript &= "" & @CRLF $sScript &= "Local $aArray[] = [10, 100, 99, 67]" & @CRLF $sScript &= "If IsTrueForAll($aArray, $x => $x >= 10) Then" & @CRLF ; Lambda expression. $sScript &= " MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.')" & @CRLF $sScript &= "Else" & @CRLF $sScript &= " MsgBox($MB_SYSTEMMODAL, '', 'Condition was False')" & @CRLF $sScript &= "EndIf" & @CRLF $sScript &= "" & @CRLF $sScript &= "If IsTrueForAll($aArray, $x => $x < 10) Then" & @CRLF ; Lambda expression. $sScript &= " MsgBox($MB_SYSTEMMODAL, '', 'Condition was True.')" & @CRLF $sScript &= "Else" & @CRLF $sScript &= " MsgBox($MB_SYSTEMMODAL, '', 'Condition was False')" & @CRLF $sScript &= "EndIf" & @CRLF $sScript &= "" & @CRLF $sScript &= "Func IsTrueForAll($aArray, $hFunc) ; Loop through the array and check the function passed with a single param matches the condition." & @CRLF $sScript &= " For $i = 0 To UBound($aArray) - 1" & @CRLF $sScript &= " If Not $hFunc($aArray[$i]) Then" & @CRLF $sScript &= " Return False" & @CRLF $sScript &= " EndIf" & @CRLF $sScript &= " Next" & @CRLF $sScript &= " Return True" & @CRLF $sScript &= "EndFunc ;==>IsTrueForAll" & @CRLF ReplaceLambdaInScript($sScript, '$x => $x >= 10') ; Parse the lambda expression and replace it in the above script. ReplaceLambdaInScript($sScript, '$x => $x < 10') ; Parse the lambda expression and replace it in the above script. ConsoleWrite($sScript & @CRLF) ; This is the new script with the lamda expression convert to an anonymous method! ClipPut($sScript) Func CreateLambdaMethod($sExpression) ; Currently no error checking of whether parameters match. Local $bIsReturn = False, _ ; Is the return condition. $sFunction = StringRegExpReplace(_WinAPI_CreateGUID(), '[}{-]', ''), $sParam = '', $sReturn = '' $sFunction = StringRegExpReplace($sFunction, '^\d+', '') ; Remove digits at the beginning of the function name. For $i = 1 To StringLen($sExpression) $sChr = StringMid($sExpression, $i, 1) If $bIsReturn Then $sReturn &= $sChr ElseIf $sChr & StringMid($sExpression, $i + 1, 1) == '=>' Then ; Check for => $i += 1 $bIsReturn = True ElseIf Not $bIsReturn Then $sParam &= $sChr EndIf Next If Not $bIsReturn Then Return "" ; Something went wrong as the => was not found. $sParam = '(' & StringRegExpReplace($sParam, '^\h*|\h*$', '') & ')' $sReturn = @TAB & 'Return ' & $sReturn Return 'Func ' & $sFunction & $sParam & @CRLF & $sReturn & @CRLF & 'EndFunc ;==>' & $sFunction & @CRLF EndFunc ;==>CreateLambdaMethod Func ReplaceLambdaInScript(ByRef $sScript, $sLambda) Local $sFunction = CreateLambdaMethod($sLambda) Local $sFunctionName = StringRegExp($sFunction, 'Func (\w*)\(', 3) If @error Then Return False $sFunctionName = $sFunctionName[0] $sScript = StringReplace($sScript, $sLambda, $sFunctionName, 1) $sScript &= @CRLF & $sFunction Return True EndFunc ;==>ReplaceLambdaInScriptIt was a simple idea I had this morning whilst having breakfast.
    1 point
  3. But your function is nice little example where young programmer could learn and benefit from using C++ features (I'm not talking about standard library, only the core language). You don't need large project to justify using this and that. I was hoping you would at least try. This is really great small example where one can learn how to define class with custom constructor, deatructor, operaror overloading, stack allocation, automatic memory managment. And all that in 10 lines. But ok, you learn C++ on large projects.
    1 point
  4. trancexx You know, I've always been a fan of Classes, I saw the power of OOP to handle lots of things when the project start growing, both in college lessons and personal experiences. I'm agree with you about using a classes for preventing the memory leak (Handling it in a nicer way). Personally, when the project wouldn't get that big, usually when it's simple enough, I won't even think about using classes, I believe OO concepts makes little projects so complicated, however they are the key to make huge projects simpler and more understandable.
    1 point
  5. The solution depends on how you define a "sentence". You need to search for ". " for "sentecnes" within a paragraph or for ".P" where "P" is the control character for a new paragraph.
    1 point
×
×
  • Create New...