Skip to content

Commit 2f63512

Browse files
committed
change comp to key in min/max
1 parent dd91e08 commit 2f63512

File tree

1 file changed

+8
-20
lines changed

1 file changed

+8
-20
lines changed

python/pyspark/rdd.py

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -810,45 +810,33 @@ def func(iterator):
810810

811811
return self.mapPartitions(func).fold(zeroValue, combOp)
812812

813-
def max(self, comp=None):
813+
def max(self, key=None):
814814
"""
815815
Find the maximum item in this RDD.
816816
817-
@param comp: A function used to compare two elements, the builtin `cmp`
818-
will be used by default.
817+
@param key: A function used to generate key for comparing
819818
820819
>>> rdd = sc.parallelize([1.0, 5.0, 43.0, 10.0])
821820
>>> rdd.max()
822821
43.0
823-
>>> rdd.max(lambda a, b: cmp(str(a), str(b)))
822+
>>> rdd.max(key=str)
824823
5.0
825824
"""
826-
if comp is not None:
827-
func = lambda a, b: a if comp(a, b) >= 0 else b
828-
else:
829-
func = max
830-
831-
return self.reduce(func)
825+
return self.reduce(lambda a, b: max(a, b, key=key))
832826

833-
def min(self, comp=None):
827+
def min(self, key=None):
834828
"""
835829
Find the minimum item in this RDD.
836830
837-
@param comp: A function used to compare two elements, the builtin `cmp`
838-
will be used by default.
831+
@param key: A function used to generate key for comparing
839832
840833
>>> rdd = sc.parallelize([2.0, 5.0, 43.0, 10.0])
841834
>>> rdd.min()
842835
2.0
843-
>>> rdd.min(lambda a, b: cmp(str(a), str(b)))
836+
>>> rdd.min(key=str)
844837
10.0
845838
"""
846-
if comp is not None:
847-
func = lambda a, b: a if comp(a, b) <= 0 else b
848-
else:
849-
func = min
850-
851-
return self.reduce(func)
839+
return self.reduce(lambda a, b: min(a, b, key=key))
852840

853841
def sum(self):
854842
"""

0 commit comments

Comments
 (0)