Commits

Kang-min Liu committed 1815d4f

cache the result of median_absolute_deviation in a private accessor.

Comments (0)

Files changed (1)

Statistics-Descriptive/lib/Statistics/Descriptive.pm

 __PACKAGE__->_make_private_accessors(
     [qw(data samples frequency geometric_mean harmonic_mean
         least_squares_fit median mode
-        skewness kurtosis
+        skewness kurtosis median_absolute_deviation
        )
     ]
 );
 
 sub median_absolute_deviation {
     my ($self) = @_;
-    my $stat = $self->new;
-    $stat->add_data(map { abs($_ - $self->median) } $self->get_data);
-    return $stat->median;
+
+    if (!defined($self->_median_absolute_deviation()))
+    {
+        my $stat = $self->new;
+        $stat->add_data(map { abs($_ - $self->median) } $self->get_data);
+        $self->_median_absolute_deviation($stat->median);
+    }
+
+    return $self->_median_absolute_deviation();
 }