Averages/Arithmetic mean: Difference between revisions

Content added Content deleted
m (Lang tags)
Line 686: Line 686:


=={{header|UnixPipes}}==
=={{header|UnixPipes}}==
term() {
<lang bash>term() {
b=$1;res=$2
b=$1;res=$2
echo "scale=5;$res+$b" | bc
echo "scale=5;$res+$b" | bc
}</lang>
}


sum() {
<lang bash>sum() {
(read B; res=$1;
(read B; res=$1;
test -n "$B" && (term $B $res) || (term 0 $res))
test -n "$B" && (term $B $res) || (term 0 $res))
}</lang>
}


fold() {
<lang bash>fold() {
func=$1
func=$1
(while read a ; do
(while read a ; do
fold $func | $func $a
fold $func | $func $a
done)
done)
}</lang>
}


mean() {
<lang bash>mean() {
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
tee >(wc -l > count) | fold sum | xargs echo "scale=5;(1/" $(cat count) ") * " | bc
}</lang>
}


<lang bash>(echo 3; echo 1; echo 4) | mean</lang>

(echo 3; echo 1; echo 4) | mean


=={{header|V}}==
=={{header|V}}==