Average (GenericNamedArraylist function)

From m204wiki
Jump to navigation Jump to search

Calculate average of items or function of items (FloatNamedArraylist, NamedArraylist, and UnicodeNamedArraylist classes)

[Introduced in Sirius Mods 7.8]


This method returns by default the average of the values of the items in the collection, or it returns the average of the items after a specified function has been applied to them.

Syntax

%number = anyNal:Average[( [itemFunction])]

Syntax terms

%number A Float variable to contain the numeric result.
anyNal A FloatNamedArraylist, NamedArraylist, or UnicodeNamedArraylist object.
itemFunction A function that operates on the type of the items in the collection. It may be a local method or method variable or a class member (Variable, Property), and it must return an intrinsic (probably Float) value.

The default itemFunction value is the special identity function, This, which simply returns the item value.

Usage notes

  • The optional itemFunction parameter lets you further manipulate the collection item values before calculating the average. If your collection items are not intrinsic values, you must specify a function that can map the item values to intrinsic values or the method will fail.

    For an example, see the Arraylist Average "Usage notes" section.

Examples

This request calculates the average of the three items in a NamedArraylist:

b %nl is namedarrayList of float %nl = new %nl('April') = 3000 %nl('May') = 5000 %nl('June') = 8000 print %nl:average end

The result is:

5333.33333333333

See also