/
Negate.html
198 lines (168 loc) · 7.87 KB
/
Negate.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html><head><title>R: Common Higher-Order Functions in Functional Programming...</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel="stylesheet" type="text/css" href="R.css">
</head><body>
<table width="100%" summary="page for funprog"><tr><td>funprog</td><td align="right">R Documentation</td></tr></table>
<h2>Common Higher-Order Functions in Functional Programming Languages</h2>
<h3>Description</h3>
<p><code>Reduce</code> uses a binary function to successively combine the
elements of a given vector and a possibly given initial value.
<code>Filter</code> extracts the elements of a vector for which a predicate
(logical) function gives true. <code>Find</code> and <code>Position</code> give
the first or last such element and its position in the vector,
respectively. <code>Map</code> applies a function to the corresponding
elements of given vectors. <code>Negate</code> creates the negation of a
given function.
</p>
<h3>Usage</h3>
<pre>
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_)
</pre>
<h3>Arguments</h3>
<table summary="R argblock">
<tr valign="top"><td><code>f</code></td>
<td>
<p>a function of the appropriate arity (binary for
<code>Reduce</code>, unary for <code>Filter</code>, <code>Find</code> and
<code>Position</code>, <i>k</i>-ary for <code>Map</code> if this is called with
<i>k</i> arguments). An arbitrary predicate function for
<code>Negate</code>.</p>
</td></tr>
<tr valign="top"><td><code>x</code></td>
<td>
<p>a vector.</p>
</td></tr>
<tr valign="top"><td><code>init</code></td>
<td>
<p>an <font face="Courier New,Courier" color="#666666"><b>R</b></font> object of the same kind as the elements of
<code>x</code>.</p>
</td></tr>
<tr valign="top"><td><code>right</code></td>
<td>
<p>a logical indicating whether to proceed from left to
right (default) or from right to left.</p>
</td></tr>
<tr valign="top"><td><code>accumulate</code></td>
<td>
<p>a logical indicating whether the successive reduce
combinations should be accumulated. By default, only the final
combination is used.</p>
</td></tr>
<tr valign="top"><td><code>nomatch</code></td>
<td>
<p>the value to be returned in the case when
“no match” (no element satisfying the predicate) is found.</p>
</td></tr>
<tr valign="top"><td><code>...</code></td>
<td>
<p>vectors.</p>
</td></tr>
</table>
<h3>Details</h3>
<p>If <code>init</code> is given, <code>Reduce</code> logically adds it to the start
(when proceeding left to right) or the end of <code>x</code>, respectively.
If this possibly augmented vector <i>v</i> has <i>n > 1</i> elements,
<code>Reduce</code> successively applies <i>f</i> to the elements of <i>v</i>
from left to right or right to left, respectively. I.e., a left
reduce computes <i>l_1 = f(v_1, v_2)</i>, <i>l_2 = f(l_1, v_3)</i>, etc.,
and returns <i>l_{n-1} = f(l_{n-2}, v_n)</i>, and a right reduce does
<i>r_{n-1} = f(v_{n-1}, v_n)</i>, <i>r_{n-2} = f(v_{n-2}, r_{n-1})</i>
and returns <i>r_1 = f(v_1, r_2)</i>. (E.g., if <i>v</i> is the
sequence (2, 3, 4) and <i>f</i> is division, left and right reduce give
<i>(2 / 3) / 4 = 1/6</i> and <i>2 / (3 / 4) = 8/3</i>, respectively.)
If <i>v</i> has only a single element, this is returned; if there are
no elements, <code>NULL</code> is returned. Thus, it is ensured that
<code>f</code> is always called with 2 arguments.
</p>
<p>The current implementation is non-recursive to ensure stability and
scalability.
</p>
<p><code>Reduce</code> is patterned after Common Lisp's <code>reduce</code>. 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.
</p>
<p><code>Filter</code> applies the unary predicate function <code>f</code> to each
element of <code>x</code>, coercing to logical if necessary, and returns the
subset of <code>x</code> for which this gives true. Note that possible
<code>NA</code> values are currently always taken as false; control over
<code>NA</code> handling may be added in the future. <code>Filter</code>
corresponds to <code>filter</code> in Haskell or <code>remove-if-not</code> in
Common Lisp.
</p>
<p><code>Find</code> and <code>Position</code> are patterned after Common Lisp's
<code>find-if</code> and <code>position-if</code>, 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
<code>right</code> is false (default) or true, respectively. If there is no
such element, the value specified by <code>nomatch</code> is returned. The
current implementation is not optimized for performance.
</p>
<p><code>Map</code> is a simple wrapper to <code>mapply</code> which does not
attempt to simplify the result, similar to Common Lisp's <code>mapcar</code>
(with arguments being recycled, however). Future versions may allow
some control of the result type.
</p>
<p><code>Negate</code> corresponds to Common Lisp's <code>complement</code>. Given a
(predicate) function <code>f</code>, it creates a function which returns the
logical negation of what <code>f</code> returns.
</p>
<h3>See Also</h3>
<p>Function <code>clusterMap</code> and <code>mcmapply</code> (not
Windows) in package <span class="pkg">parallel</span> provide parallel versions of <code>Map</code>.
</p>
<h3>Examples</h3>
<pre>
## 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
## 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.
## 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:
cfrac(c(3, 7, 15, 1, 292))
## Continued fraction approximation for Euler's number (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))
Reduce(Funcall, list(log, exp, acos, cos), 0, right = TRUE)
## n-fold iterate of a function, functional style:
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
## function t |-> (t + x / t) / 2, as a function of the initial value:
asqrt <- function(x, n) Iterate(function(t) (t + x / t) / 2, n)
asqrt(2, 30)(10) # Starting from a positive value => +sqrt(2)
asqrt(2, 30)(-1) # Starting from a negative value => -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:
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()))
</pre>
</body></html>