velocity-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Geir Magnusson Jr." <ge...@optonline.net>
Subject Re: Can I write a macro that can call itself?
Date Fri, 13 Jul 2001 02:40:26 GMT
There also is an example in the included examples...

Brian Lloyd-Newberry wrote:
> 
> --- Uday Kumar <uday@eself.com> wrote:
> > "Can I write a macro that can call itself?"
> 
>    Yes. Recursion is possible in velocity. Here is a short demo that
> you can run to try it out. It really is pretty harmless, although I
> named the macro #forkBomb in honor of the many recursive nightmares
> some of us have had.

I guess if you toss fork() into your recursion, you deserve it :)
 
>    On a seperate note those of you who run the code below will notice
> that there are line feeds that affect the presentation of the output.
> Is there a way to swallow a linefeed so that the macro code can be
> written on seperate lines?

No, not really. The idea is to keep it simple - what goes in is what
comes out, with a few exceptions...

geir
 
> -Brian
> 
> Brian S. Lloyd-Newberry
> newbeb@yahoo.com
> Vertical Learning Curve Solutions
> 
> Example below
> ---------------------------------------------------------------------
> 
> #macro( indent $level )#foreach( i in [1..$level]) #end#end
> 
> #macro( forkBomb $level $max $inc )
> ##indent($level)Before Level: $level
> #if( $level && $level >= 0 && $level <= $max )
> #set( $level = $level + $inc )
> #indent($level)Before Fork: $level
> #forkBomb($level $max $inc)
> #indent($level)After Fork: $level
> #else
> #indent($level)No more forking at level: $level
> #end
> ##indent($level)After Level: $level
> #end
> 
> Try with 0 as level and 10 max
> #forkBomb(0 10 1)
> 
> Try with 5 as level and 10 max
> #forkBomb(5 10 1)
> 
> Try with 15 as level and 10 max
> #forkBomb(15 10 1)
> 
> Try with 10 as level and 10 max going downward
> #forkBomb(10 10 -1)
> 
> Try with 'hello' as level and 10 max
> #forkBomb('hello' 10 1)
> 
> ## Try with 0 as level and 100 max
> ## #forkBomb(0 100)
> 
> __________________________________________________
> Do You Yahoo!?
> Get personalized email addresses from Yahoo! Mail
> http://personal.mail.yahoo.com/

-- 
Geir Magnusson Jr.                           geirm@optonline.net
System and Software Consulting
Developing for the web?  See http://jakarta.apache.org/velocity/
You have a genius for suggesting things I've come a cropper with!

Mime
View raw message