 |
Home:
Fun With Perl:
Perl Quizzes - Learn Perl the Fun Way:
Re: [BillKSmith] Cool Perl:
Edit Log
|
|

Zhris
Enthusiast
Sep 24, 2013, 8:53 AM
Views: 341131
|
Re: [BillKSmith] Cool Perl
|
|
|
I admit that I didn't write the original snippet, but rather found it deep in the web a while back. I never took the time to fully understand how it worked, but have a considerably clearer idea since reading your rework. The original writer must have had headaches whilst designing the "curve" calculations, unless of course it is based on a widely established algorithm of some sort:
my $x = 24 - $row - $col / 3; return $x**2 - 20 * $x + ( $col**2 ) / 3; I'm interested in your coderef approach. Initially, it appears you forgot to refactor this part. However I think it may be more efficient since it only needs to interpolate $row once per row (25 times) and not per row/col (24 * 31 times)? My "refactored" version below will help to explain what I mean:
use strict; use warnings; use Readonly; Readonly::Scalar my $AT => q(@); Readonly::Scalar my $SPACE => q( ); for my $row (0..24) { my $line; $line .= select_curve($row, $_) < 0 ? $AT : $SPACE for (0..30) ; print scalar reverse($line), $line, "\n"; } sub select_curve { my ($row, $col) = @_; my $x = 24 - $row - $col / 3; return $x**2 - 20 * $x + $col**2 / 3; } Chris
(This post was edited by Zhris on Sep 24, 2013, 8:59 AM)
|
|
Edit Log:
|
Post edited by Zhris
(Enthusiast) on Sep 24, 2013, 8:55 AM
|
Post edited by Zhris
(Enthusiast) on Sep 24, 2013, 8:56 AM
|
Post edited by Zhris
(Enthusiast) on Sep 24, 2013, 8:57 AM
|
Post edited by Zhris
(Enthusiast) on Sep 24, 2013, 8:59 AM
|
|
|  |