Skip to content

Commit

Permalink
Add 'simplify' argument to Reduce() (PR#18569, from Steve Martin).
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.r-project.org/R/trunk@84737 00db46b3-68df-0310-9c12-caf00c1e9a41
  • Loading branch information
hornik committed Jul 23, 2023
1 parent f38fd67 commit 65d64a0
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 4 deletions.
4 changes: 2 additions & 2 deletions src/library/base/R/funprog.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
# https://www.R-project.org/Licenses/

Reduce <-
function(f, x, init, right = FALSE, accumulate = FALSE)
function(f, x, init, right = FALSE, accumulate = FALSE, simplify = TRUE)
{
mis <- missing(init)
len <- length(x)
Expand Down Expand Up @@ -95,7 +95,7 @@ function(f, x, init, right = FALSE, accumulate = FALSE)
## If all results have length one, we can simplify.
## (Note that we do not simplify to arrays in case all results
## have a common length > 1.)
if(all(lengths(out) == 1L))
if(all(lengths(out) == 1L) && simplify)
out <- unlist(out, recursive = FALSE)
out
}
Expand Down
7 changes: 5 additions & 2 deletions src/library/base/man/funprog.Rd
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
% File src/library/base/man/funprog.Rd
% Part of the R package, https://www.R-project.org
% Copyright 1995-2021 R Core Team
% Copyright 1995-2023 R Core Team
% Distributed under GPL 2 or later
\name{funprog}
\alias{Filter}
Expand All @@ -23,7 +23,7 @@
}
}
\usage{
Reduce(f, x, init, right = FALSE, accumulate = FALSE)
Reduce(f, x, init, right = FALSE, accumulate = FALSE, simplify = TRUE)
Filter(f, x)
Find(f, x, right = FALSE, nomatch = NULL)
Map(f, ...)
Expand All @@ -44,6 +44,9 @@ Position(f, x, right = FALSE, nomatch = NA_integer_)
\item{accumulate}{a logical indicating whether the successive reduce
combinations should be accumulated. By default, only the final
combination is used.}
\item{simplify}{a logical indicating whether accumulated results
should be simplified (by unlisting) in case they all are length
one.}
\item{nomatch}{the value to be returned in the case when
\dQuote{no match} (no element satisfying the predicate) is found.}
\item{\dots}{vectors to which the function is \code{Map()}ped, and other
Expand Down

0 comments on commit 65d64a0

Please sign in to comment.