up previous next

a maximum element of a list

MaxBy(L: LIST, LessThanFunc: FUNCTION)

This function return a maximum of the elements of the list in 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.

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

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

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

See Also