Chaim Frenkel wrote:
> Okay, then for
>
> reduce &avg $identity, @list
>
> What should $identity be?
>
What's wrong with:

  $average = reduce (^last+^this, @^list) / scalar @^list;
  print $average->([1,3,5]);   # Prints '3'

You don't need to explicitly add a '0' to the front of the summed list.
Although I guess for a geometric average you have to be more careful:

  $geo_average =
    reduce (defined(^last)?^last:1 * ^this, @^list)
    ** (1/scalar @^list);
  print $geo_average->([1,2,4]);   # Prints '2'


Reply via email to