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>


 
># This runs the function "theLoop" 10 times.
11 : )
 
> In this case, use TriggerEvaluate instead.
It's the default thing.

The fix could be to move the exitwhen above the static if.
 
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.
 
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.
  • V-SNES V-SNES:
    Happy Friday!
    +1
  • The Helper The Helper:
    News portal has been retired. Main page of site goes to Headline News forum now
  • The Helper The Helper:
    I am working on getting access to the old news portal under a different URL for those that would rather use that for news before we get a different news view.
  • Ghan Ghan:
    Easily done
    +1
  • The Helper The Helper:
    https://www.thehelper.net/pages/news/ is a link to the old news portal - i will integrate it into the interface somewhere when i figure it out
  • Ghan Ghan:
    Need to try something
  • Ghan Ghan:
    Hopefully this won't cause problems.
  • Ghan Ghan:
    Hmm
  • Ghan Ghan:
    I have converted the Headline News forum to an Article type forum. It will now show the top 20 threads with more detail of each thread.
  • Ghan Ghan:
    See how we like that.
  • The Helper The Helper:
    I do not see a way to go past the 1st page of posts on the forum though
  • The Helper The Helper:
    It is OK though for the main page to open up on the forum in the view it was before. As long as the portal has its own URL so it can be viewed that way I do want to try it as a regular forum view for a while
  • Ghan Ghan:
    Yeah I'm not sure what the deal is with the pagination.
  • Ghan Ghan:
    It SHOULD be there so I think it might just be an artifact of having an older style.
  • Ghan Ghan:
    I switched it to a "Standard" article forum. This will show the thread list like normal, but the threads themselves will have the first post set up above the rest of the "comments"
  • The Helper The Helper:
    I don't really get that article forum but I think it is because I have never really seen it used on a multi post thread
  • Ghan Ghan:
    RpNation makes more use of it right now as an example: https://www.rpnation.com/news/
  • The Helper The Helper:
  • The Helper The Helper:
    What do you think Tom?
  • tom_mai78101 tom_mai78101:
    I will have to get used to this.
  • tom_mai78101 tom_mai78101:
    The latest news feed looks good

      The Helper Discord

      Members online

      No members online now.

      Affiliates

      Hive Workshop NUON Dome World Editor Tutorials

      Network Sponsors

      Apex Steel Pipe - Buys and sells Steel Pipe.
      Top