找回密码
 注册
查看: 1762|回复: 0

R语言:funprog()函数中文帮助文档(中英文对照)

[复制链接]
发表于 2012-2-16 17:44:49 | 显示全部楼层 |阅读模式
funprog(base)
funprog()所属R语言包:base

                                        Common Higher-Order Functions in Functional Programming Languages
                                         常见的高阶函数在函数式编程语言

                                         译者:生物统计家园网 机器人LoveR

描述----------Description----------

Reduce uses a binary function to successively combine the elements of a given vector and a possibly given initial value. Filter extracts the elements of a vector for which a predicate (logical) function gives true.  Find and Position give the first or last such element and its position in the vector, respectively.  Map applies a function to the corresponding elements of given vectors.  Negate creates the negation of a given function.
Reduce使用一个二元函数,先后结合的向量和可能给定的初始值的元素。 Filter提取一个谓词(逻辑)功能提供了真正的矢量元素。 Find和Position给的第一个或最后一个这样的元素,其位置在向量,分别。 Map应用功能向量给予相应的元素。 Negate创建一个给定函数的否定。


用法----------Usage----------


Reduce(f, x, init, right = FALSE, accumulate = FALSE)
Filter(f, x)
Find(f, x, right = FALSE, nomatch = NULL)
Map(f, ...)
Negate(f)
Position(f, x, right = FALSE, nomatch = NA_integer_)



参数----------Arguments----------

参数:f
a function of the appropriate arity (binary for Reduce, unary for Filter, Find and Position, k-ary for Map if this is called with k arguments).  An arbitrary predicate function for Negate.
一个适当的元数的函数(Reduce,Filter一元二元,Find和Position,k元Map如果这称为k参数)。为Negate的任意谓词函数。


参数:x
a vector.
一个向量。


参数:init
an R object of the same kind as the elements of x.
同类作为x元素的R对象。


参数:right
a logical indicating whether to proceed from left to right (default) or from right to left.
逻辑表明是否继续由左到右(默认)或由右至左。


参数:accumulate
a logical indicating whether the successive reduce combinations should be accumulated.  By default, only the final combination is used.
逻辑是否应累计减少了连续组合。默认情况下,只有最后的组合使用。


参数:nomatch
the value to be returned in the case when “no match” (no element satisfying the predicate) is found.
在“不匹配”(没有满足谓词的元素)被发现时的情况下返回值。


参数:...
vectors.
向量。


Details

详情----------Details----------

If init is given, Reduce logically adds it to the start (when proceeding left to right) or the end of x, respectively. If this possibly augmented vector v has n > 1 elements, Reduce successively applies f to the elements of v from left to right or right to left, respectively.  I.e., a left reduce computes l_1 = f(v_1, v_2), l_2 = f(l_1, v_3), etc., and returns l_{n-1} = f(l_{n-2}, v_n), and a right reduce does r_{n-1} = f(v_{n-1}, v_n), r_{n-2} = f(v_{n-2}, r_{n-1}) and returns r_1 = f(v_1, r_2).  (E.g., if v is the sequence (2, 3, 4) and f is division, left and right reduce give (2 / 3) / 4 = 1/6 and 2 / (3 / 4) = 8/3, respectively.) If v has only a single element, this is returned; if there are no elements, NULL is returned.  Thus, it is ensured that f is always called with 2 arguments.
如果init是Reduce逻辑上把它添加到开始(左至右时出发)或x年底,分别。如果这可能增强向量vn > 1元素,Reduce先后应用于fv由左到右或从右到左,分别元素。即左减少计算l_1 = f(v_1, v_2)l_2 = f(l_1, v_3)等,并返回l_{n-1} = f(l_{n-2}, v_n),并有权减少不r_{n-1} = f(v_{n-1}, v_n),r_{n-2} = f(v_{n-2}, r_{n-1})“回报<X > (例如,如果r_1 = f(v_1, r_2)序列(2,3,4)v分工,左,右减少给f和(2 / 3) / 4 = 1/6,分别)。如果 2 / (3 / 4) = 8/3只有一个单一的元素,这是返回;如果有任何元素,v返回。因此,它是确保NULL总是2个参数调用。

The current implementation is non-recursive to ensure stability and scalability.
目前实施非递归,以确保稳定性和可扩展性。

Reduce is patterned after Common Lisp's reduce.  A reduce is also known as a fold (e.g., in Haskell) or an accumulate (e.g., in the C++ Standard Template Library).  The accumulative version corresponds to Haskell's scan functions.
Reduce图案后的Common Lisp的reduce。一个reduce也被称为一折(例如,在Haskell)或累积(例如,在C + +标准模板库)。累计版本对应Haskell的扫描功能。

Filter applies the unary predicate function f to each element of x, coercing to logical if necessary, and returns the subset of x for which this gives true.  Note that possible NA values are currently always taken as false; control over NA handling may be added in the future.  Filter corresponds to filter in Haskell or remove-if-not in Common Lisp.
Filter适用于一元谓词函数f每个x元素,胁迫的逻辑,如果有必要,和返回x的子集,这给真正的。注意可能NA值始终为false采取的控制NA处理可能会在未来加入。 Filter对应filterHaskell或remove-if-not在Common Lisp的。

Find and Position are patterned after Common Lisp's find-if and position-if, respectively.  If there is an element for which the predicate function gives true, then the first or last such element or its position is returned depending on whether right is false (default) or true, respectively.  If there is no such element, the value specified by nomatch is returned.  The current implementation is not optimized for performance.
Find和Position后的Common Lisp的find-if和position-if,分别图案。如果有一个谓词函数给出真正的元素,然后在第一个或最后一个元素或它的位置取决于是否right是假的(默认)或真实的,分别返回。如果没有这样的元素,nomatch指定的值返回。目前的执引号况并没有得到优化性能。

Map is a simple wrapper to mapply which does not attempt to simplify the result, similar to Common Lisp's mapcar (with arguments being recycled, however).  Future versions may allow some control of the result type.
Map是一个简单的包装mapply这并不试图简化的结果,类似的Common Lisp的mapcar(参数被回收,但是)。未来版本可能会允许一些控制的结果类型。

Negate corresponds to Common Lisp's complement.  Given a (predicate) function f, it creates a function which returns the logical negation of what f returns.
Negate对应的Common Lisp的complement。鉴于(谓词)的功能f,它会创建一个函数,它返回什么f返回的逻辑否定。


参见----------See Also----------

clusterMap in package parallel provides a parallel version of Map.
clusterMap包parallel提供了Map并行版本。


举例----------Examples----------


## A general-purpose adder:[#通用的加法:]
add <- function(x) Reduce("+", x)
add(list(1, 2, 3))
## Like sum(), but can also used for adding matrices etc., as it will[#类似SUM(),但也可以用于添加矩阵等,因为它会]
## use the appropriate '+' method in each reduction step.[#使用相应的“+”在每个还原步骤的方法。]
## More generally, many generics meant to work on arbitrarily many[#更普遍,许多仿制药意味着任意多个工作]
## arguments can be defined via reduction:[#参数可以通过减少定义:]
FOO <- function(...) Reduce(FOO2, list(...))
FOO2 <- function(x, y) UseMethod("FOO2")
## FOO() methods can then be provided via FOO2() methods.[#美孚()方法,然后可以通过foo2()的方法。]

## A general-purpose cumulative adder:[#通用的累计加法:]
cadd <- function(x) Reduce("+", x, accumulate = TRUE)
cadd(seq_len(7))

## A simple function to compute continued fractions:[#一个简单的函数来计算连分数:]
cfrac <- function(x) Reduce(function(u, v) u + 1 / v, x, right = TRUE)
## Continued fraction approximation for pi:[#继续PI分式逼近:]
cfrac(c(3, 7, 15, 1, 292))
## Continued fraction approximation for Euler's number (e):[#续分式逼近欧拉数(E):]
cfrac(c(2, 1, 2, 1, 1, 4, 1, 1, 6, 1, 1, 8))

## Iterative function application:[#迭代函数的应用:]
Funcall <- function(f, ...) f(...)
## Compute log(exp(acos(cos(0))[#计算LOG(EXP(ACOS(COS(0))]
Reduce(Funcall, list(log, exp, acos, cos), 0, right = TRUE)
## n-fold iterate of a function, functional style:[#n倍迭代函数,功能风格:]
Iterate <- function(f, n = 1)
    function(x) Reduce(Funcall, rep.int(list(f), n), x, right = TRUE)
## Continued fraction approximation to the golden ratio:[#续分式逼近黄金比例:]
Iterate(function(x) 1 + 1 / x, 30)(1)
## which is the same as[#这是一样的]
cfrac(rep.int(1, 31))
## Computing square root approximations for x as fixed points of the[#x的平方根近似计算作为固定点]
## function t |-&gt; (t + x / t) / 2, as a function of the initial value:[#函数t |  - >(T + X / T)/ 2作为初始值的功能:]
asqrt <- function(x, n) Iterate(function(t) (t + x / t) / 2, n)
asqrt(2, 30)(10) # Starting from a positive value =&gt; +sqrt(2)[从正值开始=> + SQRT(2)]
asqrt(2, 30)(-1) # Starting from a negative value =&gt; -sqrt(2)[从负值开始=> SQRT(2)]

## A list of all functions in the base environment:[#在基地环境的所有功能列表:]
funs <- Filter(is.function, sapply(ls(baseenv()), get, baseenv()))
## Functions in base with more than 10 arguments:[#基地超过10个参数与功能:]
names(Filter(function(f) length(formals(args(f))) > 10, funs))
## Number of functions in base with a '...' argument:[#在与基地“......”的功能参数:]
length(Filter(function(f)
              any(names(formals(args(f))) %in% "..."),
              funs))

## Find all objects in the base environment which are *not* functions:[#这是*不*功能的基础环境中的所有对象:]
Filter(Negate(is.function),  sapply(ls(baseenv()), get, baseenv()))

转载请注明:出自 生物统计家园网(http://www.biostatistic.net)。


注:
注1:为了方便大家学习,本文档为生物统计家园网机器人LoveR翻译而成,仅供个人R语言学习参考使用,生物统计家园保留版权。
注2:由于是机器人自动翻译,难免有不准确之处,使用时仔细对照中、英文内容进行反复理解,可以帮助R语言的学习。
注3:如遇到不准确之处,请在本贴的后面进行回帖,我们会逐渐进行修订。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|小黑屋|生物统计家园 网站价格

GMT+8, 2025-1-23 03:05 , Processed in 0.020511 second(s), 16 queries .

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表