-
Notifications
You must be signed in to change notification settings - Fork 60
Expand file tree
/
Copy path_functools.java
More file actions
50 lines (43 loc) · 1.72 KB
/
Copy path_functools.java
File metadata and controls
50 lines (43 loc) · 1.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/* Copyright (c) Jython Developers */
package org.python.modules._functools;
import org.python.core.Py;
import org.python.core.PyBytes;
import org.python.core.PyObject;
import org.python.expose.ExposedFunction;
import org.python.expose.ExposedModule;
import org.python.expose.ModuleInit;
/**
* The Python _functools module.
*/
@ExposedModule(doc = _functools.__doc__)
public class _functools {
public static final String __doc__ = "Tools that operate on functions.";
@ModuleInit
public static void classDictInit(PyObject dict) {
dict.__setitem__("partial", PyPartial.TYPE);
}
public static final String __doc__reduce =
"reduce(function, sequence[, initial]) -> value\n\n" +
"Apply a function of two arguments cumulatively to the items of a sequence,\n" +
"from left to right, so as to reduce the sequence to a single value.\n" +
"For example, reduce(lambda x, y: x+y, [1, 2, 3, 4, 5]) calculates\n" +
"((((1+2)+3)+4)+5). If initial is present, it is placed before the items\n" +
"of the sequence in the calculation, and serves as a default when the\n" +
"sequence is empty.";
@ExposedFunction(defaults = {"null"}, doc = __doc__reduce)
public static PyObject reduce(PyObject f, PyObject l, PyObject z) {
PyObject result = z;
PyObject iter = Py.iter(l, "reduce() arg 2 must support iteration");
for (PyObject item; (item = iter.__next__()) != null;) {
if (result == null) {
result = item;
} else {
result = f.__call__(result, item);
}
}
if (result == null) {
throw Py.TypeError("reduce of empty sequence with no initial value");
}
return result;
}
}