Jump to content
Sign in to follow this  

Towards approximating a 'yield' statement

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
            $command = StringFormat( $yield, $i )           
            ConsoleWrite( $command & @CRLF ) 
            Execute( $command )
            If @error <> 0 then $errorFlag = @error
    SetError( $errorFlag )

#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 )

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

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 )


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

$oShell = ObjCreate("shell.application")
Iterator( $oShellWindows, "ShowMeFromCollection" )
ConsoleWrite( @error & @CRLF)
Edited by zfisherdrums
  • Like 1

Share this post

Link to post
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
Sign in to follow this