Jump to content

Recommended Posts

Posted

They both do the same thing, though the second version is better practice. Call() should only be used when required e.g. 

#include <Array.au3>

Example()

Func Example()
    Local $aArray = [0, 1, 2, 3, 4, 5, 10, 90]
    Local $aGtrThan10 = ArrayFindAll($aArray, GreaterThan10)
    _ArrayDisplay($aGtrThan10)

    Local $aLessThan4 = ArrayFindAll($aArray, LessThan4)
    _ArrayDisplay($aLessThan4)
EndFunc   ;==>Example

Func ArrayFindAll(ByRef $aArray, $hDelegate) ; This logic doesn't change. It's the delegate or first class object that does.
    Local $aReturn[0], $iIndex = 0
    For $i = 0 To UBound($aArray) - 1
        ConsoleWrite($i & @CRLF)
        If Call($hDelegate, $aArray[$i]) Then
            ReDim $aReturn[$iIndex + 1]
            $aReturn[$iIndex] = $aArray[$i]
            $iIndex += 1
        EndIf
    Next
    Return $aReturn
EndFunc   ;==>ArrayFindAll

Func GreaterThan10($iValue)
    Return $iValue > 10
EndFunc   ;==>GreaterThan10

Func LessThan4($iValue)
    Return $iValue < 4
EndFunc   ;==>LessThan4

But then I prefer this approach since functions are regarded as first class objects...

#include <Array.au3>

Example()

Func Example()
    Local $aArray = [0, 1, 2, 3, 4, 5, 10, 90]
    Local $aGtrThan10 = ArrayFindAll($aArray, GreaterThan10)
    _ArrayDisplay($aGtrThan10)

    Local $aLessThan4 = ArrayFindAll($aArray, LessThan4)
    _ArrayDisplay($aLessThan4)
EndFunc   ;==>Example

Func ArrayFindAll(ByRef $aArray, $hDelegate) ; This logic doesn't change. It's the delegate or first class object that does.
    Local $aReturn[0], $iIndex = 0
    For $i = 0 To UBound($aArray) - 1
        ConsoleWrite($i & @CRLF)
        If $hDelegate($aArray[$i]) Then ; Call($hDelegate, $aArray[$i])
            ReDim $aReturn[$iIndex + 1]
            $aReturn[$iIndex] = $aArray[$i]
            $iIndex += 1
        EndIf
    Next
    Return $aReturn
EndFunc   ;==>ArrayFindAll

Func GreaterThan10($iValue)
    Return $iValue > 10
EndFunc   ;==>GreaterThan10

Func LessThan4($iValue)
    Return $iValue < 4
EndFunc   ;==>LessThan4

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Posted

Thanks.

  On 8/5/2014 at 9:24 PM, guinness said:

 

They both do the same thing, though the second version is better practice. Call() should only be used when required e.g. 

Sorry if i posted it in a wrong place..

  On 8/5/2014 at 9:08 PM, Jos said:

Please post in the proper forum.

Moved..

Jos

Posted

Any other comments or questions? It was pretty impressive that example don't you think ?! -_0.

UDF List:

  Reveal hidden contents

Updated: 22/04/2018

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...