=item calculate_area
The calculate_area function calculates the area of the
points passed in. You can pass the points in a number of
different ways, but, regardless, you'll get the area back.
=over 4
=item *
You can pass in a series of points. The function will
assume that the shape described is a closed-shape, that is,
the first point is automatically repeated as the end point.
You can pass in the points as perl6 pairs, e.g.:
$area = calculate_area( $x0 => $y0, $x1 => $y1 );
or as two lists of x's and y's:
$area = calculate_area( @x, @y );
or as a list of Point objects
$area = calculate_area( @points );
=item *
You can pass in a Shape object.
$area = calculate_area( $shape );
=item *
You can pass in an equation, and a beginning x-value and
an ending x-value, and the area will be that under the
curve. The area may be negative in this case.
$area = calculate_area( $equation, $x0, $x1 );
=back
Returns: the area of the shape, in square units.
=cut
To me, the subroutine is incredibly unambiguous. It calculates area. It's a nice DWIMmy function that can handle any type of input. However, if I were to maintain this type of DWIMmery, it'd be a minor headache - I'd resort to something like this:
sub calculate_area
{
unless ( grep { ref $_ ne 'Pair' } @_ )
{ goto \&_calculate_area_pairs; }
if (@_ % 2 == 0 and not ref $_[0])
{ goto \&_calculate_area_x_list_y_list; }
unless ( grep { ref $_ ne 'Point' } @_ )
{ goto \&_calculate_area_list_of_points; }
unless ( grep { ref $_ ne 'ARRAY' } @_ )
{ goto \&_calculate_area_list_of_arrays; }
if (UNIVERSAL::isa($_[0], 'Shape'))
{ goto \&_calculate_area_share; }
if (UNIVERSAL::isa($_[0], 'Equation') and @_ == 3)
{ goto \&_calculate_area_equation; }
die "Didn't recognise how you called calculate_area";
}
And then I'd implement each possibility in a separate function. The interface is simple, and very perlish (DWIM's very well - and can add more DWIMmery later). The code is ugly. MMD handles the ugly part of the code in a very generic manner which means that it only needs to be debugged by the p6 team, not by each developer who wants it. Because I'm quite sure I've got sneaky little bugs in that untested code above. |