funprog: Common higher-order functions in functional programming...

Description Usage Arguments Value See Also Examples

Description

Reduce uses a binary function to successively combine the elements of a given list-like or vector-like object and a possibly given initial value. Filter extracts the elements of a list-like or vector-like object for which a predicate (logical) function gives true. Find and Position give the first or last such element and its position in the object, respectively. Map applies a function to the corresponding elements of given list-like or vector-like objects.

NOTE: This man page is for the Reduce, Filter, Find, Map and Position S4 generic functions defined in the BiocGenerics package. See ?base::Reduce for the default methods (defined in the base package). Bioconductor packages can define specific methods for objects (typically list-like or vector-like) not supported by the default methods.

Usage

1
2
3
4
5
Reduce(f, x, init, right=FALSE, accumulate=FALSE)
Filter(f, x)
Find(f, x, right=FALSE, nomatch=NULL)
Map(f, ...)
Position(f, x, right=FALSE, nomatch=NA_integer_)

Arguments

f, init, right, accumulate, nomatch

See ?base::Reduce for a description of these arguments.

x

A list-like or vector-like object.

...

One or more list-like or vector-like objects.

Value

See ?base::Reduce for the value returned by the default methods.

Specific methods defined in Bioconductor packages should behave as consistently as possible with the default methods.

See Also

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
Reduce  # note the dispatch on the 'x' arg only
showMethods("Reduce")
selectMethod("Reduce", "ANY")  # the default method

Filter  # note the dispatch on the 'x' arg only
showMethods("Filter")
selectMethod("Filter", "ANY")  # the default method

Find  # note the dispatch on the 'x' arg only
showMethods("Find")
selectMethod("Find", "ANY")  # the default method

Map  # note the dispatch on the '...' arg only
showMethods("Map")
selectMethod("Map", "ANY")  # the default method

Position  # note the dispatch on the 'x' arg only
showMethods("Position")
selectMethod("Position", "ANY")  # the default method

Example output

Loading required package: parallel

Attaching package:BiocGenericsThe following objects are masked frompackage:parallel:

    clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
    clusterExport, clusterMap, parApply, parCapply, parLapply,
    parLapplyLB, parRapply, parSapply, parSapplyLB

The following objects are masked frompackage:stats:

    IQR, mad, sd, var, xtabs

The following objects are masked frompackage:base:

    anyDuplicated, append, as.data.frame, basename, cbind, colnames,
    dirname, do.call, duplicated, eval, evalq, Filter, Find, get, grep,
    grepl, intersect, is.unsorted, lapply, Map, mapply, match, mget,
    order, paste, pmax, pmax.int, pmin, pmin.int, Position, rank,
    rbind, Reduce, rownames, sapply, setdiff, sort, table, tapply,
    union, unique, unsplit, which.max, which.min

standardGeneric for "Reduce" defined from package "BiocGenerics"

function (f, x, init, right = FALSE, accumulate = FALSE) 
standardGeneric("Reduce")
<bytecode: 0x55f145479b40>
<environment: 0x55f14544f7b0>
Methods may be defined for arguments: x
Use  showMethods("Reduce")  for currently available ones.
Function: Reduce (package BiocGenerics)
f="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (f, x, init, right = FALSE, accumulate = FALSE) 
{
    mis <- missing(init)
    len <- length(x)
    if (len == 0L) 
        return(if (mis) NULL else init)
    f <- match.fun(f)
    if (!is.vector(x) || is.object(x)) 
        x <- as.list(x)
    ind <- seq_len(len)
    if (mis) {
        if (right) {
            init <- x[[len]]
            ind <- ind[-len]
        }
        else {
            init <- x[[1L]]
            ind <- ind[-1L]
        }
    }
    if (!accumulate) {
        if (right) {
            for (i in rev(ind)) init <- forceAndCall(2, f, x[[i]], 
                init)
        }
        else {
            for (i in ind) init <- forceAndCall(2, f, init, x[[i]])
        }
        init
    }
    else {
        len <- length(ind) + 1L
        out <- vector("list", len)
        if (mis) {
            if (right) {
                out[[len]] <- init
                for (i in rev(ind)) {
                  init <- forceAndCall(2, f, x[[i]], init)
                  out[[i]] <- init
                }
            }
            else {
                out[[1L]] <- init
                for (i in ind) {
                  init <- forceAndCall(2, f, init, x[[i]])
                  out[[i]] <- init
                }
            }
        }
        else {
            if (right) {
                out[[len]] <- init
                for (i in rev(ind)) {
                  init <- forceAndCall(2, f, x[[i]], init)
                  out[[i]] <- init
                }
            }
            else {
                for (i in ind) {
                  out[[i]] <- init
                  init <- forceAndCall(2, f, init, x[[i]])
                }
                out[[len]] <- init
            }
        }
        if (all(lengths(out) == 1L)) 
            out <- unlist(out, recursive = FALSE)
        out
    }
}
<bytecode: 0x55f145489068>
<environment: namespace:base>

Signatures:
        f    
target  "ANY"
defined "ANY"
standardGeneric for "Filter" defined from package "BiocGenerics"

function (f, x) 
standardGeneric("Filter")
<bytecode: 0x55f14339f920>
<environment: 0x55f144654710>
Methods may be defined for arguments: x
Use  showMethods("Filter")  for currently available ones.
Function: Filter (package BiocGenerics)
f="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (f, x) 
{
    ind <- as.logical(unlist(lapply(x, f)))
    x[which(ind)]
}
<bytecode: 0x55f143cac2c0>
<environment: namespace:base>

Signatures:
        f    
target  "ANY"
defined "ANY"
standardGeneric for "Find" defined from package "BiocGenerics"

function (f, x, right = FALSE, nomatch = NULL) 
standardGeneric("Find")
<bytecode: 0x55f14338e3b8>
<environment: 0x55f143c8f508>
Methods may be defined for arguments: x
Use  showMethods("Find")  for currently available ones.
Function: Find (package BiocGenerics)
f="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (f, x, right = FALSE, nomatch = NULL) 
{
    f <- match.fun(f)
    if ((pos <- Position(f, x, right, nomatch = 0L)) > 0L) 
        x[[pos]]
    else nomatch
}
<bytecode: 0x55f143e97700>
<environment: namespace:base>

Signatures:
        f    
target  "ANY"
defined "ANY"
standardGeneric for "Map" defined from package "BiocGenerics"

function (f, ...) 
standardGeneric("Map")
<bytecode: 0x55f1433bfc60>
<environment: 0x55f143986028>
Methods may be defined for arguments: ...
Use  showMethods("Map")  for currently available ones.
Function: Map (package BiocGenerics)
f="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (f, ...) 
{
    f <- match.fun(f)
    mapply(FUN = f, ..., SIMPLIFY = FALSE)
}
<bytecode: 0x55f1433c3790>
<environment: namespace:base>

Signatures:
        f    
target  "ANY"
defined "ANY"
standardGeneric for "Position" defined from package "BiocGenerics"

function (f, x, right = FALSE, nomatch = NA_integer_) 
standardGeneric("Position")
<bytecode: 0x55f1453269e8>
<environment: 0x55f145315ff8>
Methods may be defined for arguments: x
Use  showMethods("Position")  for currently available ones.
Function: Position (package BiocGenerics)
f="ANY"

Method Definition (Class "derivedDefaultMethod"):

function (f, x, right = FALSE, nomatch = NA_integer_) 
{
    ind <- if (right) 
        rev(seq_along(x))
    else seq_along(x)
    for (i in ind) if (f(x[[i]])) 
        return(i)
    nomatch
}
<bytecode: 0x55f145336010>
<environment: namespace:base>

Signatures:
        f    
target  "ANY"
defined "ANY"

BiocGenerics documentation built on April 17, 2021, 6:01 p.m.