| 70 | } |
| 71 | |
| 72 | @StarlarkMethod( |
| 73 | name = "max", |
| 74 | doc = |
| 75 | "Returns the largest one of all given arguments. If only one positional argument is" |
| 76 | + " provided, it must be a non-empty iterable.It is an error if elements are not" |
| 77 | + " comparable (for example int with string), or if no arguments are given." |
| 78 | + "<pre class=\"language-python\">\n" // |
| 79 | + "max(2, 5, 4) == 5\n" |
| 80 | + "max([5, 6, 3]) == 6\n" |
| 81 | + "max(\"two\", \"three\", \"four\", key = len) ==\"three\" # the longest\n" |
| 82 | + "max([1, -1, -2, 2], key = abs) == -2 # the first encountered with maximal key" |
| 83 | + " value\n" |
| 84 | + "</pre>", |
| 85 | extraPositionals = @Param(name = "args", doc = "The elements to be checked."), |
| 86 | parameters = { |
| 87 | @Param( |
| 88 | name = "key", |
| 89 | named = true, |
| 90 | positional = false, |
| 91 | allowedTypes = { |
| 92 | @ParamType(type = StarlarkCallable.class), |
| 93 | @ParamType(type = NoneType.class), |
| 94 | }, |
| 95 | doc = "An optional function applied to each element before comparison.", |
| 96 | defaultValue = "None") |
| 97 | }, |
| 98 | useStarlarkThread = true) |
| 99 | public Object max(Object key, Sequence<?> args, StarlarkThread thread) |
| 100 | throws EvalException, InterruptedException { |
| 101 | return findExtreme( |
| 102 | args, Starlark.toJavaOptional(key, StarlarkCallable.class), Starlark.ORDERING, thread); |
| 103 | } |
| 104 | |
| 105 | /** Returns the maximum element from this list, as determined by maxOrdering. */ |
| 106 | private static Object findExtreme( |