up previous next
MaxBy    --    a maximum element of a list


Syntax
MaxBy(L: LIST, LessThanFunc: FUNCTION)

Description
This function returns a maximum of the elements of the list L with respect to the comparisons made by LessThanFunc .

The comparison function LessThanFunc takes two arguments and returns true if the first argument is less than the second, otherwise it returns false .

NOTE: to call MaxBy(L,LessThanFunc) inside a function you will need to make the name LessThanFunc accessible using TopLevel LessThanFunc;

NOTE: if both LessThanFunc(A, B) and LessThanFunc(B, A) return true , then A and B are viewed as being equal: for example, when comparing two polynomials by their LPP only.

Example
/**/  Define ByLength(S, T)    -- define the sorting function
/**/    Return len(S) < len(T);
/**/  EndDefine;

/**/  L := ["bird", "mouse", "cat", "elephant"];
/**/  MaxBy(L, ByLength);
elephant

/**/  use QQ[x,y];
/**/  Define ByLPP(S, T)  return LPP(S) < LPP(T);  EndDefine;
/**/  L := [x^2*y -y -3, x^5 -1, x^5 -y^2 +3];
/**/  MaxBy(L, ByLPP);
x^5 -1

See Also