2 correcting syntax
source | link

Combining for loops

Suppose you have code of the following form:

for($pre1; $cond1; $post1) for($pre2; $cond2; $post2) $code;

this can generally be re-rolled in the following form:

for($pre1; $cond2 • $post2 || $cond1 • $pre2 • $post1$post1; ) $code;

where represents a generic combining operator. This usually results in an byte count reduction, but will likely require some creativity. $cond2 will need to be written so that it fails the first time through. $post1 should also fail to execute the first time, although it may be easier to refactor beforehand so that $post1 is not present.

If you're working with three or more nested loops, you can also combine two first, and then combine that to another, and so on. I find that it has generally been easier to combine from the inside outwards.


As an example, consider the following solution to the H-carpet fractal (97 bytes):

for(;$i<$n=3**$argn;$i+=print"$s\n")for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

This can be reformulated in the following way:

for(;($i+=$e&&print"$s\n")<$n=3**$argn;)for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$e&&print prevents print on first iteration, and also does not increment $i.

and finally (93 bytes):

for(;$H>$e*=3or$e=($i+=$e&&print"$s\n")<${$s=H}=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$H>$e*=3 will fail the first time as both variables are undefined.

Combining for loops

Suppose you have code of the following form:

for($pre1; $cond1; $post1) for($pre2; $cond2; $post2) $code;

this can generally be re-rolled in the following form:

for($pre1; $cond2 • $post2 || $cond1 • $pre2 • $post1) $code;

where represents a generic combining operator. This usually results in an byte count reduction, but will likely require some creativity. $cond2 will need to be written so that it fails the first time through. $post1 should also fail to execute the first time, although it may be easier to refactor beforehand so that $post1 is not present.

If you're working with three or more nested loops, you can also combine two first, and then combine that to another, and so on. I find that it has generally been easier to combine from the inside outwards.


As an example, consider the following solution to the H-carpet fractal (97 bytes):

for(;$i<$n=3**$argn;$i+=print"$s\n")for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

This can be reformulated in the following way:

for(;($i+=$e&&print"$s\n")<$n=3**$argn;)for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$e&&print prevents print on first iteration, and also does not increment $i.

and finally (93 bytes):

for(;$H>$e*=3or$e=($i+=$e&&print"$s\n")<${$s=H}=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$H>$e*=3 will fail the first time as both variables are undefined.

Combining for loops

Suppose you have code of the following form:

for($pre1; $cond1; $post1) for($pre2; $cond2; $post2) $code;

this can generally be re-rolled in the following form:

for($pre1; $cond2 • $post2 || $cond1 • $pre2 • $post1; ) $code;

where represents a generic combining operator. This usually results in an byte count reduction, but will likely require some creativity. $cond2 will need to be written so that it fails the first time through. $post1 should also fail to execute the first time, although it may be easier to refactor beforehand so that $post1 is not present.

If you're working with three or more nested loops, you can also combine two first, and then combine that to another, and so on. I find that it has generally been easier to combine from the inside outwards.


As an example, consider the following solution to the H-carpet fractal (97 bytes):

for(;$i<$n=3**$argn;$i+=print"$s\n")for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

This can be reformulated in the following way:

for(;($i+=$e&&print"$s\n")<$n=3**$argn;)for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$e&&print prevents print on first iteration, and also does not increment $i.

and finally (93 bytes):

for(;$H>$e*=3or$e=($i+=$e&&print"$s\n")<${$s=H}=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$H>$e*=3 will fail the first time as both variables are undefined.

1
source | link

Combining for loops

Suppose you have code of the following form:

for($pre1; $cond1; $post1) for($pre2; $cond2; $post2) $code;

this can generally be re-rolled in the following form:

for($pre1; $cond2 • $post2 || $cond1 • $pre2 • $post1) $code;

where represents a generic combining operator. This usually results in an byte count reduction, but will likely require some creativity. $cond2 will need to be written so that it fails the first time through. $post1 should also fail to execute the first time, although it may be easier to refactor beforehand so that $post1 is not present.

If you're working with three or more nested loops, you can also combine two first, and then combine that to another, and so on. I find that it has generally been easier to combine from the inside outwards.


As an example, consider the following solution to the H-carpet fractal (97 bytes):

for(;$i<$n=3**$argn;$i+=print"$s\n")for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

This can be reformulated in the following way:

for(;($i+=$e&&print"$s\n")<$n=3**$argn;)for($s=H,$e=1;$e<$n;$e*=3)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$e&&print prevents print on first iteration, and also does not increment $i.

and finally (93 bytes):

for(;$H>$e*=3or$e=($i+=$e&&print"$s\n")<${$s=H}=3**$argn;)$s.=str_pad($i/$e%3&1?$s:'',$e).$s;

$H>$e*=3 will fail the first time as both variables are undefined.