Averages/Mode: Difference between revisions

no edit summary
No edit summary
Line 457:
In for loops, each key and value from the collection are [[Pattern Matching|pattern matched]] against the specified <code><var>key pattern</var> => <var>value pattern</var></code>. In "<code>for v => ==maxCount in counts</code>", the <code>==</code> is a pattern-match operator which fails unless the value examined is equal to the specified value; so this selects only the input values (keys in <code>counts</code>) whose counts are equal to the maximum count.
 
=={{header|Elena}}==
 
<lang elena>#define std'basic'*.
#define std'collections'*.
#define std'patterns'*.
#define ext'patterns'*.
#define std'dictionary'*.
 
#symbol SMA : aPeriod = List~
{
+ aNumber
[
self += NewInt32Value::aNumber.
#if (self count > aPeriod)?
[
self delete &first_item.
].
#var aCount := self count.
#if (aCount == 0)?
[ ^ 0. ].
#var aSum := Real::0.
Scan::self run:anItem => (aSum += anItem).
^ aSum / aCount.
]
}.
 
// --- Program ---
 
#symbol Program =>
[
#var SMA3 := SMA::3.
#var SMA5 := SMA::5.
#var anAction := N =>
[
'program'output << "sma3 + " << N << " = " << SMA3 + N.
'program'output << ",sma5 + " << N << " = " << SMA5 + N << "%n".
].
loop::&fromInteger:1 &to:5 run:anAction rewind:anAction.
].
</lang>
 
 
Anonymous user