Anonymous user
Averages/Arithmetic mean: Difference between revisions
→{{header|Ruby}}: ++ sather
m (Added a Trith example.) |
(→{{header|Ruby}}: ++ sather) |
||
Line 860:
<lang ruby>nums = [3, 1, 4, 1, 5, 9]
nums.empty? ? 0 : nums.inject(:+) / Float(nums.size)</lang>
=={{header|Sather}}==
Built to work with VEC, ("geometric" vectors), whose elements must be floats. A 0-dimension vector yields "nan".
<lang sather>class VECOPS is
mean(v:VEC):FLT is
m ::= 0.0;
loop m := m + v.aelt!; end;
return m / v.dim.flt;
end;
end;
class MAIN is
main is
v ::= #VEC(|1.0, 5.0, 7.0|);
#OUT + VECOPS::mean(v) + "\n";
end;
end;</lang>
=={{header|Scala}}==
|