Loop function

Idea created by chivalry on Feb 21, 2017
    Active
    Score6

    We have loops in scripts and recursive loops in custom functions. I'd love to see a new built-in function that allows calculations to loop without recursion. As an example to how this might work:

     

    Loop ( initialization ; whileCondition ; code )

     

    For example, the following would return 55:

     

    Loop (

    Let ( [ $i = 0 ; $sum = 0 ] ; "" ) ;

    $i <= 10 ;

    Let ( [ $i = $i + 1 ; $sum = $sum + $i ] ; $sum )

    )

     

    No, I haven't worked out all the details. Maybe `Let` variables are remembered between two `Let` calls within a `Loop` call so that local variables aren't required, for example. I'm just trying to give an idea of how it might work. The idea above is simply that the variable assignment portion of the second `Let` is executed until the `whileCondition` returns `True`, and only then is the return portion of the second `Let` used to evaluate the `Loop`'s return value.

     

    Basically, I'd like to see this custom function become built-in (or see a non-recursive version without the recursion limit here).

     

    // devp.Loop ( _count ; _statements ; _result_name )

    // Purpose: Executes _statements _count times and returns the variable named in _result_name.

    // Parameters: _count:       Repetition count, 0 < _count < 10,000 (recursion limit)

    //             _statements:  One or more statements to be repeated

    //             _result_name: The variable name to return as the final result

    // Version:    1.0 written 17-02-23 by Charles Ross

    // Example:    Let ( [ $c=0; $ret=0 ]; devp.Loop ( 10 ; "$c=$c+1; $ret=$ret+$c" ; "$ret" )  ) = 55

     

    Case (

      _count < 0 ; "?" ;

      _count = 0 ; Evaluate ( _value_name ) ;

     

     

      Let (

        [

          _ = Evaluate ( "Let ( [ " & _statements & " ] ; \"\" )" )

        ] ;

     

        devp.Loop ( _count - 1 ; _statements ; _value_name )

      )

    )