Averages/Simple moving average: Difference between revisions

Add NetRexx implementation
(→‎{{header|Erlang}}: Added Erlang implementation)
(Add NetRexx implementation)
Line 1,683:
 
Some notes about this solution: unless P = 0, length(L) can never be 0, as L always incorporates at least N (a step that is accomplished in the arguments to list.take_upto/3). If the implementation of the 'state' type is hidden, and if init/1 checks for P = 0, users of this code can never cause a division-by-zero error in sma/4. Although this solution doesn't try to be as stateful as the task description would like, explicit state is by far simpler and more natural and more straightforward than the alternative in Mercury. Finally, [http://www.mercury.csse.unimelb.edu.au/information/doc-release/mercury_ref/State-variables.html#State-variables state variables] (and higher-order functions that anticipate threaded state) remove much of the potential ugliness or error in threading the same state through many users.
 
=={{header|NetRexx}}==
{{trans|Java}}
<lang NetRexx>/* NetRexx */
options replace format comments java crossref symbols nobinary
 
numeric digits 20
 
class RAvgSimpleMoving public
 
properties private
window = java.util.Queue
period
sum
 
properties constant
exMsg = 'Period must be a positive integer'
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method RAvgSimpleMoving(period_) public
if \period_.datatype('w') then signal RuntimeException(exMsg)
if period_ <= 0 then signal RuntimeException(exMsg)
window = LinkedList()
period = period_
sum = 0
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method newNum(num) public
sum = sum + num
window.add(sum)
if window.size() > period then do
sum = sum - (Rexx window.remove())
end
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method getAvg() public returns Rexx
if window.isEmpty() then do
avg = 0
end
else do
avg = sum / window.size()
end
return avg
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method run_samples(args = String[]) public static
testData = [Rexx 1, 2, 3, 4, 5, 5, 4, 3, 2, 1]
windowSizes = [Rexx 3, 5]
loop windSize over windowSizes
ma = RAvgSimpleMoving(windSize)
loop xVal over testData
ma.newNum(xVal)
say 'Next number =' xVal.right(5)', SMA =' ma.getAvg().format(10, 9)
end xVal
say
end windSize
 
return
 
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
method main(args = String[]) public static
run_samples(args)
return
</lang>
'''Output:'''
<pre style="height: 25ex; overflow: scroll">
Next number = 1, SMA = 1.000000000
Next number = 2, SMA = 1.500000000
Next number = 3, SMA = 2.000000000
Next number = 4, SMA = 3.000000000
Next number = 5, SMA = 3.666666667
Next number = 5, SMA = 3.333333333
Next number = 4, SMA = 1.333333333
Next number = 3, SMA = -2.333333333
Next number = 2, SMA = -7.000000000
Next number = 1, SMA = -11.333333333
 
Next number = 1, SMA = 1.000000000
Next number = 2, SMA = 1.500000000
Next number = 3, SMA = 2.000000000
Next number = 4, SMA = 2.500000000
Next number = 5, SMA = 3.000000000
Next number = 5, SMA = 3.800000000
Next number = 4, SMA = 4.000000000
Next number = 3, SMA = 3.400000000
Next number = 2, SMA = 1.800000000
Next number = 1, SMA = -1.000000000
 
</pre>
 
=={{header|Objective-C}}==
Anonymous user