Skip to content

Commit d9079df

Browse files
Add future_Filter() [#125]
1 parent ee76dc0 commit d9079df

9 files changed

Lines changed: 97 additions & 11 deletions

File tree

DESCRIPTION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Package: future.apply
2-
Version: 1.11.3-9023
2+
Version: 1.11.3-9024
33
Title: Apply Function to Elements in Parallel using Futures
44
Depends:
55
R (>= 3.2.0),

NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ S3method(future_by,default)
55
S3method(future_kernapply,default)
66
S3method(future_kernapply,ts)
77
export(future_.mapply)
8+
export(future_Filter)
89
export(future_Map)
910
export(future_apply)
1011
export(future_by)

NEWS.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515
parallel RNG seed. Consolidating random number generation to the
1616
core package will allow us to add central support for custom
1717
parallel RNG methods beyond the built-in L'Ecuyer-CMRG method.
18-
18+
19+
* Added `future_Filter()`, which is parallel version of
20+
`base::Filter()`.
21+
1922
* Added `future_kernapply()`, which is parallel version of
2023
`stats::kernapply()`.
2124

R/future_Filter.R

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#' @inheritParams future_mapply
2+
#'
3+
#' @param f A function of the arity \eqn{k} if `future_Map()` is called with
4+
#' \eqn{k} arguments. Unary for `future_Filter()`.
5+
#'
6+
#' @param x A vector-like object to iterate over.
7+
#'
8+
#' @return
9+
#' See [base::Filter()] for details.
10+
#'
11+
#' @examples
12+
#' is_even <- function(x) { x %% 2 == 0 }
13+
#' x <- sample.int(100, size = 1000, replace = TRUE)
14+
#' y <- Filter(is_even, x)
15+
#'
16+
#' @author
17+
#' The implementations of `future_Filter()` is adopted from the source code
18+
#' of the corresponding base \R function `Filter()`, which is licensed under
19+
#' GPL (>= 2) with 'The R Core Team' as the copyright holder.
20+
#'
21+
#' @rdname future_mapply
22+
#' @export
23+
future_Filter <- function(f, x, ...) {
24+
f <- match.fun(f)
25+
z <- unlist(future_lapply(x, f, ...))
26+
if (is.null(z))
27+
return(x[integer()])
28+
ind <- as.logical(z)
29+
x[which(ind)]
30+
}

README.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
2+
13
<div id="badges"><!-- pkgdown markup -->
24
<a href="https://CRAN.R-project.org/web/checks/check_results_future.apply.html"><img border="0" src="https://www.r-pkg.org/badges/version/future.apply" alt="CRAN check status"/></a> <a href="https://github.com/futureverse/future.apply/actions?query=workflow%3AR-CMD-check"><img border="0" src="https://github.com/futureverse/future.apply/actions/workflows/R-CMD-check.yaml/badge.svg?branch=develop" alt="R CMD check status"/></a> <a href="https://app.codecov.io/gh/futureverse/future.apply"><img border="0" src="https://codecov.io/gh/futureverse/future.apply/branch/develop/graph/badge.svg" alt="Coverage Status"/></a>
35
</div>
@@ -42,19 +44,20 @@ Where does the **[future.apply]** package fit in the software stack? You can th
4244
<br>
4345
</td>
4446
<td>
45-
Future-versions of common goto <code>*apply()</code> functions available in base R (of the <strong>base</strong> package):<br>
47+
Future-versions of common goto <code>*apply()</code> functions available in base R (of the <strong>base</strong> and <strong>stats</strong> packages):<br>
4648
<code>future_apply()</code>,
4749
<code>future_by()</code>,
4850
<code>future_eapply()</code>,
51+
<code>future_Filter()</code>,
4952
<code>future_lapply()</code>,
53+
<code>future_kernapply()</code>,
5054
<code>future_Map()</code>,
5155
<code>future_mapply()</code>,
5256
<code>future_.mapply()</code>,
5357
<code>future_replicate()</code>,
5458
<code>future_sapply()</code>,
55-
<code>future_tapply()</code>,
56-
<code>future_vapply()</code>, and
57-
<code>future_kernapply()</code>.
59+
<code>future_tapply()</code>, and
60+
<code>future_vapply()</code>.
5861
<br>
5962
<em>The following function is not implemented:</em><br>
6063
<code>future_rapply()</code><br>
@@ -182,3 +185,4 @@ This will install the package from source.
182185
## Contributing
183186

184187
To contribute to this package, please see [CONTRIBUTING.md](CONTRIBUTING.md).
188+

inst/testme/test-future_Filter.R

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#' @tags future_Filter
2+
#' @tags sequential multisession multicore
3+
4+
library(future.apply)
5+
6+
message("*** future_Filter() ...")
7+
8+
is_even <- function(x) { x %% 2 == 0 }
9+
x <- sample.int(100, size = 1000, replace = TRUE)
10+
11+
y_truth <- x[vapply(x, FUN.VALUE = NA, FUN = is_even)]
12+
str(y_truth)
13+
14+
for (strategy in supportedStrategies()) {
15+
message(sprintf("*** strategy = %s ...", sQuote(strategy)))
16+
plan(strategy)
17+
18+
y <- Filter(is_even, x)
19+
str(y)
20+
21+
stopifnot(identical(y, y_truth))
22+
23+
message(sprintf("*** strategy = %s ... done", sQuote(strategy)))
24+
}
25+
26+
plan(sequential)
27+
28+
message("*** future_Filter() ... DONE")

man/future_mapply.Rd

Lines changed: 18 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/test-future_Filter.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## This runs testme test script inst/testme/test-future_Filter.R
2+
## Don't edit - it was autogenerated by inst/testme/deploy.R
3+
future.apply:::testme("future_Filter")

vignettes/future.apply-1-overview.md.rsp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,15 +57,16 @@ Future-versions of common goto <code>*apply()</code> functions available in base
5757
<code>future_apply()</code>,
5858
<code>future_by()</code>,
5959
<code>future_eapply()</code>,
60+
<code>future_Filter()</code>,
6061
<code>future_lapply()</code>,
62+
<code>future_kernapply()</code>,
6163
<code>future_Map()</code>,
6264
<code>future_mapply()</code>,
6365
<code>future_.mapply()</code>,
6466
<code>future_replicate()</code>,
6567
<code>future_sapply()</code>,
66-
<code>future_tapply()</code>,
67-
<code>future_vapply()</code>, and
68-
<code>future_kernapply()</code>.
68+
<code>future_tapply()</code>, and
69+
<code>future_vapply()</code>.
6970
<br>
7071
<em>The following function is not implemented:</em><br>
7172
<code>future_rapply()</code><br>

0 commit comments

Comments
 (0)