Maximum (Arraylist function): Difference between revisions

From m204wiki
Jump to navigation Jump to search
m (1 revision)
m (1 revision)
Line 24: Line 24:
In the following example, the <var>Maximum</var> and <var>[[Minimum (Arraylist function)|Minimum]]</var> functions first apply the <var>Stringlist</var> <var>[[Count (Stringlist function)|Count]]</var> function, then return the position of the <var>Arraylist</var> item <var>Stringlist</var> that has the most items and the one that has the fewest.  The <var>Stringlist</var> <var>[[List (Stringlist function)|List]]</var> function simplifies the construction of the lists.
In the following example, the <var>Maximum</var> and <var>[[Minimum (Arraylist function)|Minimum]]</var> functions first apply the <var>Stringlist</var> <var>[[Count (Stringlist function)|Count]]</var> function, then return the position of the <var>Arraylist</var> item <var>Stringlist</var> that has the most items and the one that has the fewest.  The <var>Stringlist</var> <var>[[List (Stringlist function)|List]]</var> function simplifies the construction of the lists.
<p class="code">begin
<p class="code">begin
<p class="code">%list1 is object stringlist
%list1 is object stringlist
%list2 is object stringlist
%list2 is object stringlist
%list3 is object stringlist
%list3 is object stringlist
 
%list1 = List('the', 'quick', 'brown')
%list1 = List('the', 'quick', 'brown')
%list2 = List('fox', 'jumped', 'over', 'the')
%list2 = List('fox', 'jumped', 'over', 'the')
%list3 = List('lazy', 'dog', 'yesterday', 'two', 'times')
%list3 = List('lazy', 'dog', 'yesterday', 'two', 'times')
 
al is collection arraylist of object stringlist
al is collection arraylist of object stringlist
al = List(%list1, %list2, %list3)
al = List(%list1, %list2, %list3)
 
PrintText {~} is {al:maximum(count)}
PrintText {~} is {al:maximum(count)}
PrintText {~} is {al:minimum(count)}
PrintText {~} is {al:minimum(count)}
</p>
end
end
</p>
</p>
Line 49: Line 48:
The local method used with <var>Maximum</var> in the following request applies <var>[[ToIntegerPower and ToPower (Float functions)|ToIntegerPower]]</var>, which (because it requires its own argument) the syntax of <var>Maximum</var> does not allow as a parameter.
The local method used with <var>Maximum</var> in the following request applies <var>[[ToIntegerPower and ToPower (Float functions)|ToIntegerPower]]</var>, which (because it requires its own argument) the syntax of <var>Maximum</var> does not allow as a parameter.
<p class="code">begin
<p class="code">begin
<p class="code">%alist is collection arraylist of float
%alist is collection arraylist of float
%alist = List(10, -11, 0, 11)
%alist = List(10, -11, 0, 11)
 
local function (float):Squared is float
local function (float):Squared is float
  return %this:toIntegerPower(2)
    return %this:toIntegerPower(2)
end function
end function
 
PrintText {~} is {%alist:maximum(Squared)}
PrintText {~} is {%alist:maximum(Squared)}
PrintText {~} is {%alist:minimum(Squared)}
PrintText {~} is {%alist:minimum(Squared)}
</p>
end
end
</p>
</p>

Revision as of 23:00, 7 February 2011

Get number of maximum item (Arraylist class)


This function returns the number of the Arraylist item that has the maximum numeric value after the application of a specified function to each item. The function that gets applied to each Arraylist item, which you identify in the argument to Maximum, must be a method that operates on the item type and returns a User Language intrinsic datatype (Float, String, Longstring, or Unicode) value.

Syntax

%number = al:Maximum[( [itemFunction])]

Syntax terms

%numberA numeric variable to return the item number of the item in the indicated Arraylist that has the maximum value after the argument function is applied.
alAn Arraylist object.
methodA method value (a method name literal, a method variable, or even a method that returns a method value) for a method that operates on items of the type specified on the al declaration and that returns a numeric or string value.

As of Sirius Mods version 7.6, the special identity function, This is the default function value for the Maximum and Minimum methods. See Using the This function as the Maximum parameter for more information.

Usage notes

  • If the function applied by Maximum returns string values, Maximum uses the decimal-equivalent value of the character bytes and determines the number of the item that has the greatest value. Lowercase letters are first ranked alphabetically, then upper case letters, also ranked alphabetically, followed by the numbers; ie: 'a'..'z','A'..'Z',0..9.
  • If two or more Arraylist items have equal, maximum, values, Maximum returns the position of the item that appears closest to the beginning of the Arraylist.
  • The parameter for Maximum is a method value, not a User Language expression. That is, you cannot provide a function that itself has an argument (say, ToIntegerPower(2)) as the Maximum parameter. The example of Maximum / Minimum with local method shows a way to apply ToIntegerPower with Maximum.
  • Maximum is available in Sirius Mods version 7.3 and later.

Examples

Maximum and Minimum using Count function

In the following example, the Maximum and Minimum functions first apply the Stringlist Count function, then return the position of the Arraylist item Stringlist that has the most items and the one that has the fewest. The Stringlist List function simplifies the construction of the lists.

begin %list1 is object stringlist %list2 is object stringlist %list3 is object stringlist %list1 = List('the', 'quick', 'brown') %list2 = List('fox', 'jumped', 'over', 'the') %list3 = List('lazy', 'dog', 'yesterday', 'two', 'times') al is collection arraylist of object stringlist al = List(%list1, %list2, %list3) PrintText {~} is {al:maximum(count)} PrintText {~} is {al:minimum(count)} end

The result is:

al:maximum(count) is 3 al:minimum(count) is 1

Maximum/Minimum with local method

The local method used with Maximum in the following request applies ToIntegerPower, which (because it requires its own argument) the syntax of Maximum does not allow as a parameter.

begin %alist is collection arraylist of float %alist = List(10, -11, 0, 11) local function (float):Squared is float return %this:toIntegerPower(2) end function PrintText {~} is {%alist:maximum(Squared)} PrintText {~} is {%alist:minimum(Squared)} end

The result is:

%alist:maximum(Squared) is 2 %alist:minimum(Squared) is 3

Although applying the local method above to item 2 and item 4 produces the same value, Maximum returns the item that is closer to the beginning of the arraylist.

See also

  • Minimum is the opposite of the Maximum function.