Average (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
No edit summary
 
(4 intermediate revisions by one other user not shown)
Line 12: Line 12:
<td>An <var>Arraylist</var> object.</td></tr>
<td>An <var>Arraylist</var> object.</td></tr>


<tr><th>method</th>
<tr><th>itemFunction</th>
<td>A function that operates on the type of the items in the <var>Arraylist</var>. It may be a [[Local and Common entities#Defining and invoking a local method|local method]] or [[Method variables|method variable]] or a class member (<var>Variable</var>, <var>Property</var>), and it must return an [[Intrinsic classes|intrinsic]] (probably <var>Float</var>) value.
<td>A function that operates on the type of the items in the <var>Arraylist</var>. It may be a [[Local and Common entities#Defining and invoking a local method|local method]] or [[Method variables|method variable]] or a class member (<var>Variable</var>, <var>Property</var>), and it must return an [[Intrinsic classes|intrinsic]] (probably <var>Float</var>) value.
<p>
<p>
The default <var class="term">method</var> value is the special identity function, <var>This</var>, which simply returns the item value.</p></td></tr>
The default <var class="term">itemFunction</var> value is the special identity function, <var>[[Collections#Using the This function as the Maximum parameter|This]]</var>, which simply returns the item value.</p></td></tr>
</table>
</table>


==Usage notes==
==Usage notes==
<ul>
<ul>
<li>The optional <var class="term">method</var> parameter lets you further manipulate the <var>Arraylist</var> item values
 
before performing the <var>Average</var> method's operation.
what goes up
If your <var>Arraylist</var>'s items are not intrinsic values, you must specify a function that can map the item values to intrinsic values or the method will fail.
 
 
<li>The optional <var class="term">itemFunction</var> parameter lets you further manipulate the <var>Arraylist</var> item values
before calculating the average. The <var class="term">itemFunction</var> default (identity function <var>This</var>) fails against non-intrinsic values, however. If your collection items are <i>not</i> intrinsic values, you must specify an <var class="term">itemFunction</var> function that can map the item values to intrinsic values.
<p>  
<p>  
For example, for an <var>Arraylist</var> that is a list of coordinates, you could
For example, for an <var>Arraylist</var> that is a list of coordinates, you could
return the average of their distance from the origin by first applying a local function as the <var>Average</var> method's <var class="term">method</var> parameter: </p>
return the average of their distance from the origin by first applying a local function as the <var>Average</var> method's <var class="term">itemFunction</var> parameter: </p>
<p class="code">b
<p class="code">b
class point
class point

Latest revision as of 19:50, 8 March 2013

Average the members of this ArrayList (Arraylist class)

[Introduced in Sirius Mods 7.8]


Syntax

%average = al:Average[( [itemFunction])]

Syntax terms

%average A Float variable to contain the numeric result.
al An Arraylist object.
itemFunction A function that operates on the type of the items in the Arraylist. 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

    what goes up
  • The optional itemFunction parameter lets you further manipulate the Arraylist item values before calculating the average. The itemFunction default (identity function This) fails against non-intrinsic values, however. If your collection items are not intrinsic values, you must specify an itemFunction function that can map the item values to intrinsic values.

    For example, for an Arraylist that is a list of coordinates, you could return the average of their distance from the origin by first applying a local function as the Average method's itemFunction parameter:

    b class point public constructor new(%x is float, %y is float) variable x is float variable y is float end public constructor new(%x is float, %y is float) %this:x = %x %this:y = %y end constructor end class local function (point):distance is float return (%this:x * %this:x + %this:y * %this:y):squareRoot end function %al is arrayList of object point %al = new %al:add(new(1,1)) %al:add(new(3,4)) %al:add(new(-5,12)) print %al:average(distance) end

    The result is 6.47140452079103.

Examples

This request calculates the average of a three-item Arraylist:

b %al is arrayList of float %al = new %al = list(5, 3, 8) print %al:average end

The result is:

5.33333333333333

See also