Obvious, but passing arguments to this will cause it to break.
One other mod: (j/1000) could be replaced with (j>999) which is still non conditional, but faster.
One other mod: (j/1000) could be replaced with (j>999) which is still non conditional, but faster.
#include <stdio.h>
#include <stdlib.h>
void pr(int j) {
printf("%d\n", j);
(pr + (exit - pr)*(j/1000))(j+1);
}
int main(int argc, char** argv) {
pr(1);
return 0;
} #include <stdio.h>
#include <stdlib.h>
int pk( int i )
{
static int (*v[])( int ) = { pk, abs };
printf( "%d\n", i );
return v[ i/1000 ]( i+1 );
}
int main( void )
{
pk( 1 );
return 0;
} use POSIX;
sub pk
{
print "$_[0]\n";
return ( \&pk, \&fabs )[ $_[0] / 1000 ]( $_[0] + 1 );
}
pk( 1 ); perl -e '($pk=sub { print "$_[0]\n"; ($pk, sub{}) [$_[0]/1000] ($_[0]+1) }) -> (1)' $,="\n"; print 1..1000
I don't know if it can be made shorter. My previous example was (also for the readers who don't know that) to demonstrate the "C inspired" features in Perl and almost 1-1 mapping to the C solution, minus declarations.