Snippet ForEach

Bribe

vJass errors are legion
Reaction score
67
JASS:
library ForEach
    
    //=======================================================================
    // This library provides high-level functions at your normal high-level
    // efficiency loss. Zinc has a built-in syntax which is very close to
    // this, but Zinc also lacks a ton of features only vJass can provide.
    //
    // The premise is this:
    //
    // For each (from 0 to the specified exit-point), do (function).
    // For range (start, end), do (function).
    // For range (start, end, stride), do (function).
    //
    // Start may be higher than end.  The stride will reverse itself in that
    // case. The stride defaults to 1 (i = i + 1).
    //
    // cJass, now deprecated due to lack of proper care, had the "define"
    // syntax which allowed optional parameters. Because of the lack of such
    // functionality, ForEach is split into three functions:
    //
    // ForEach - takes an integer for how many times the loop should iterate.
    // ForRange - takes a start and end parameter
    // ForRangeEx - takes a start, end and stride parameter
    //
    // The last parameter will always be the function to be iterated through.
    //
    // The functionality of ForEach is this: you give it a function, the
    // function must take an integer and return a boolean. A true boolean
    // continues the loop as normal, a false boolean exits the loop ahead of
    // schedule. You can decide which one you are more comfortable with by
    // adjusting the constant "EXITWHEN".
    //
    // If you are iterating through a stack and you need to increment or
    // decrement the current and end position, use:
    //
    // ForEach_RaiseStack()
    // ForEach_LowerStack()
    //
    // Alternatively, you may set ForEach_A and ForEach_B directly.  A is the
    // running index, B is the end-position.
    //
    // A ForEach call may be nested within another ForEach call.
    
    
    // Syntax:
    //
    // function theLoop takes integer i returns boolean
    //     call TriggerExecute(myTriggerArray<i>)
    //     return true
    // endfunction
    // ...
    // call ForEach(10, theLoop)
    //
    // # This runs the function &quot;theLoop&quot; exactly 10 times (0..9).
    
    
    globals
        // If the looping function returns this value, the loop ends:
        public constant boolean EXITWHEN = false
        
        public integer A = 0
        public integer B = 0
    endglobals
    
    
    function ForEach_LowerStack takes nothing returns nothing
        set A = A - 1
        set B = B - 1
    endfunction
    
    function ForEach_RaiseStack takes nothing returns nothing
        set A = A + 1
        set B = B + 1
    endfunction
    
    
    function interface ForEachFunc takes integer i returns boolean
    
    
    function ForRangeEx takes integer start, integer end, integer stride, ForEachFunc func returns nothing
        local integer a = A
        local integer b = B
        set A = start
        set B = end
        if (start &gt; end) then
            set stride = -(stride)
        endif
        loop
            exitwhen ((A == B) or (func.evaluate(A) == EXITWHEN))
            set A = A + stride
        endloop
        set A = a
        set B = b
    endfunction
    
    
    function ForRange takes integer start, integer end, ForEachFunc func returns nothing
        call ForRangeEx(start, end, 1, func)
    endfunction
    
    
    function ForEach takes integer end, ForEachFunc func returns nothing
        call ForRangeEx(0, end, 1, func)
    endfunction
    
    
endlibrary
</i>


JASS:
library Example initializer init requires ForEach
    
    globals
        group array Groups
    endglobals
    
    private function clear takes integer i returns nothing
        call GroupClear(Groups<i>)
        return true
    endfunction
    
    function ClearUnitGroups takes nothing returns nothing
        call ForEach(bj_MAX_PLAYER_SLOTS, clear)
    endfunction
    
    private function fill takes integer i returns nothing
        call GroupEnumUnitsOfPlayer(Groups<i>, Player(i), null)
        return true
    endfunction
    
    function FillUnitGroups takes nothing returns nothing
        call ForEach(bj_MAX_PLAYER_SLOTS, fill)
    endfunction
    
endlibrary

</i></i>


 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
># This runs the function "theLoop" 10 times.
11 : )
 

Sgqvur

FullOfUltimateTruthsAndEt ernalPrinciples, i.e shi
Reaction score
62
> In this case, use TriggerEvaluate instead.
It's the default thing.

The fix could be to move the exitwhen above the static if.
 

Bribe

vJass errors are legion
Reaction score
67
Thank you for the feedback. I have adjusted the functionality to work more like the mainstream "for each" iterator so that a call to ForEach(10, func) will run "func" 10 times, from 0..9.
 

Darthfett

Aerospace/Cybersecurity Software Engineer
Reaction score
615
Seems a bit overkill to use a system for such a simple task, but I'm sure there are a few times where it could become useful, for someone who wants their code structured their own way.

I think I would be more interested in a wrapper for ForGroup or GroupEnum callbacks, so that we don't have to always create globals or worry about the efficiency of using GetEnumUnit()/GetFilterUnit() in each situation.
 
General chit-chat
Help Users
  • No one is chatting at the moment.

      The Helper Discord

      Staff online

      Members online

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top