lambda.tools introduces block
and range semantics to support these concepts,
respectively. Hence lambda.tools defines
mapblock and maprange and similar functions
for fold. x has 12
elements. Performing a mapblock operation with window of
length 3 applies the specified function to the following
sub-vectors: x[1:3], x[4:6], x[7:9],
x[10:12]. This is useful for processing any vector
or list produced by a function that returns a regular
length output.
Note that if the original sequence is not an integer multiple of the window length, the last sub-vector will not have the same length as the preceding sub-vectors. }
x, a
maprange operation with window of length 3 produces the
following sub-vectors as arguments: x[1:3],
x[2:4], x[3:6], ..., x[10:12].
An example of a range operation is generating n-grams
from a text document. Suppose a vector v contains
a sequence of words. Then maprange(v, 2,
function(x) paste(x, collapse=' ')) creates bigrams. }
apply function works
in this manner where the MARGIN argument defines
whether iteration operates on rows versus columns. Hence
lambda.tools introduces 2-dimensional versions of
these functions. For simplicity, the 2-dimensional
variants of map and fold only operate along columns. To
operate along rows requires transposing the data
structure.
Consider the following code that applies multiple rotations to a collection of points.
ps <- t(matrix(c(0,0, 4,0, 2,4), nrow=2)) rt <- matrix(c(cos(pi),-sin(pi),sin(pi),cos(pi), cos(pi/2), -sin(pi/2), sin(pi/2), cos(pi/2)), nrow=2) mapblock(rt, 2, function(x) ps The result is a 6x2 matrix that is the union of the two rotation operations. }
pad a
sequence to a specified length, chomp the head and
tail off a vector, slice a sequence into two
pieces based on an expression. The partition
function is similar, while quantize and
confine transform data to fit specific ranges.
Logical functions such as onlyif and
use_default eliminate the need for conditional
blocks, which can streamline code and remove the risk of
poorly scoped variables. }
map fold
samplerange slice
onlyif quantize
partition lambda.r