пятница, 4 марта 2016 г.

#870. Emulation of for..to




Test ForToTest
 
 ARRAY operator to
   ^@ IN aFrom
   ^ IN aTo
  @ ( 
   OBJECT IN aLambda
   INTEGER VAR I
   I := ( aFrom DO )
   Dec I
   ( aTo DO I ) - LOOP ( Inc I I aLambda DO ) 
  ) FunctorToIterator >>> Result
 ; // 1to
  
 for ( 1 to 10 ) .
 // - prints numbers from 1 to 10
 '' .
 for ( 1 to 20 ) .
 // - prints numbers from 1 to 20
 '' .
 for ( 0 to 20 ) .
 // - prints numbers from 0 to 20
 '' .
 for ( -1 to 20 ) .
 // - prints numbers from -1 to 20
 '' .
 for ( -1 to 21 ) .
 // - prints numbers from -1 to 21
 '' .
 0 for ( 1 to 10 ) + .
 // - adds up numbers from 1 to 10 and prints the result
 '' .
 for ( 1 to 10 Reverted ) .
 // - prints numbers from 10 to 1
  
/*{ 
 ARRAY VAR L
  
 1to 10 >>> L
  
 @ . L ITERATE
 // - prints numbers from 1 to 10
 '' .
 @ . 1to 20 ITERATE
 // - prints numbers from 1 to 20
 '' .
 0 @ + L ITERATE .
 // - adds up numbers from 1 to 10 and prints the result
 '' .
 @ . L Reverted ITERATE
 // - prints numbers from 10 to 1}*/
; // ForToTest
 
ForToTest
Sure, it is not difficult to add step by writing I := I + Step in Inc I.

As for me, I managed to do it “more concisely” than gurus do.

Комментариев нет:

Отправить комментарий