Skip to content

Commit

Permalink
Merge pull request apache#91 from sun-rui/add_max_min
Browse files Browse the repository at this point in the history
Add maximum() and minimum() API to RDD.
  • Loading branch information
shivaram committed Oct 29, 2014
2 parents 624ac9d + 4381efa commit 579db58
Show file tree
Hide file tree
Showing 5 changed files with 106 additions and 0 deletions.
2 changes: 2 additions & 0 deletions pkg/NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ exportMethods(
"mapPartitions",
"mapPartitionsWithIndex",
"mapValues",
"maximum",
"minimum",
"partitionBy",
"reduce",
"reduceByKey",
Expand Down
42 changes: 42 additions & 0 deletions pkg/R/RDD.R
Original file line number Diff line number Diff line change
Expand Up @@ -554,6 +554,48 @@ setMethod("reduce",
Reduce(func, partitionList)
})

#' Get the maximum element of an RDD.
#'
#' @param rdd The RDD to get the maximum element from
#' @export
#' @rdname maximum
#' @examples
#'\dontrun{
#' sc <- sparkR.init()
#' rdd <- parallelize(sc, 1:10)
#' maximum(rdd) # 10
#'}
setGeneric("maximum", function(rdd) { standardGeneric("maximum") })

#' @rdname maximum
#' @aliases maximum,RDD
setMethod("maximum",
signature(rdd = "RDD"),
function(rdd) {
reduce(rdd, max)
})

#' Get the minimum element of an RDD.
#'
#' @param rdd The RDD to get the minimum element from
#' @export
#' @rdname minimum
#' @examples
#'\dontrun{
#' sc <- sparkR.init()
#' rdd <- parallelize(sc, 1:10)
#' minimum(rdd) # 1
#'}
setGeneric("minimum", function(rdd) { standardGeneric("minimum") })

#' @rdname minimum
#' @aliases minimum,RDD
setMethod("minimum",
signature(rdd = "RDD"),
function(rdd) {
reduce(rdd, min)
})

#' Take elements from an RDD.
#'
#' This function takes the first NUM elements in the RDD and
Expand Down
10 changes: 10 additions & 0 deletions pkg/inst/tests/test_rdd.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,3 +178,13 @@ test_that("distinct() on RDDs", {
actual <- sort(unlist(collect(uniques)))
expect_equal(actual, nums)
})

test_that("maximum() on RDDs", {
max <- maximum(rdd)
expect_equal(max, 10)
})

test_that("minimum() on RDDs", {
min <- minimum(rdd)
expect_equal(min, 1)
})
26 changes: 26 additions & 0 deletions pkg/man/maximum.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% Generated by roxygen2 (4.0.2): do not edit by hand
\docType{methods}
\name{maximum}
\alias{maximum}
\alias{maximum,RDD}
\alias{maximum,RDD-method}
\title{Get the maximum element of an RDD.}
\usage{
maximum(rdd)

\S4method{maximum}{RDD}(rdd)
}
\arguments{
\item{rdd}{The RDD to get the maximum element from}
}
\description{
Get the maximum element of an RDD.
}
\examples{
\dontrun{
sc <- sparkR.init()
rdd <- parallelize(sc, 1:10)
maximum(rdd) # 10
}
}

26 changes: 26 additions & 0 deletions pkg/man/minimum.Rd
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
% Generated by roxygen2 (4.0.2): do not edit by hand
\docType{methods}
\name{minimum}
\alias{minimum}
\alias{minimum,RDD}
\alias{minimum,RDD-method}
\title{Get the minimum element of an RDD.}
\usage{
minimum(rdd)

\S4method{minimum}{RDD}(rdd)
}
\arguments{
\item{rdd}{The RDD to get the minimum element from}
}
\description{
Get the minimum element of an RDD.
}
\examples{
\dontrun{
sc <- sparkR.init()
rdd <- parallelize(sc, 1:10)
minimum(rdd) # 1
}
}

0 comments on commit 579db58

Please sign in to comment.