Jump to content

Towards approximating a 'yield' statement


zfisherdrums
 Share

Recommended Posts

Not the most useful thing as it is not feature complete, but a fun experiment nonetheless.

Edit: What does it do? Well, it attempts to approximate the yield statement found in other languages, i.e. Ruby and C#. The purpose of the yield statement is to give control to a specified function from within the called function's body. Some security limitations - as well as my own intelligence limitations - make this example more of a conceptual example than fully functional. But oh well.

Why did I place it here? Perhaps some poor soul will find themselves trapped under mounds of maintenance code, much of which relies on repetitious looping and iterating. Perhaps it will spark a better implementation. Who knows. That's the beauty of these forum thingys.

#include <WinAPI.au3>

Func Iterator( $loop, $yield )  
    Local $errorFlag = 0
    For $i in $loop
        if ( IsObj( $i ) ) Then
            $command = StringFormat( "%s ( $item )", $yield )
            ConsoleWrite( $command & @CRLF ) 
            Call( $yield, $i )
            If @error <> 0 then $errorFlag = @error
        Else
            $command = StringFormat( $yield, $i )           
            ConsoleWrite( $command & @CRLF ) 
            Execute( $command )
            If @error <> 0 then $errorFlag = @error
        EndIf       
    Next
    SetError( $errorFlag )
EndFunc


#region Example Functions
; The following are example functions that will be called by the Iterator function

Func ShowMeFromArray( $msg )
; Helo Werldz
    MsgBox(0, "ShowMeFromArray", $msg )
EndFunc


Func TwoToThePowerOf( $exponent )
; Display two to the power of a supplied exponent
    MsgBox(0, "TwoToThePowerOf", 2 ^ $exponent  )
EndFunc


Func ShowMeFromCollection( $item )  
; Some kind of window-releated activity is simulated here
; Because of AutoIts restrictions against window-operations
; proceeding from Call'ed, Execute'd statements - we'll use 
; the WinAPI include to achieve the desired result.
    MsgBox(0, "ShowMeFromCollection", "Press 'OK' to maximize " & $item.LocationName  ) 
    _WinAPI_ShowWindow( $item.HWND, @SW_MAXIMIZE )
    MsgBox(0, "ShowMeFromCollection", "Press 'OK' to minimize " & $item.LocationName  ) 
    _WinAPI_ShowWindow( $item.HWND, @SW_MINIMIZE )
    SetError( 1 )
EndFunc

#EndRegion


Dim $test = StringSplit( "1,2,3", "," )
Iterator( $test, 'ShowMeFromArray( "%s" )' )
Iterator( $test, 'TwoToThePowerOf( "%s" )' )
ConsoleWrite( @error & @CRLF)

$oShell = ObjCreate("shell.application")
$oShellWindows=$oShell.windows
Iterator( $oShellWindows, "ShowMeFromCollection" )
ConsoleWrite( @error & @CRLF)
Edited by zfisherdrums
Link to comment
Share on other sites

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
 Share

  • Recently Browsing   0 members

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