A Consumer-class to reduce N successive records
into a single yield.
Usage
Reducer(FUN, init, ..., yieldNth = NA_integer_)
Arguments
FUN
A function of two arguments, where the first argument is
the result of the previous reduction (or init, if specified,
for the first record) and the second argument is the current
record.
init
An optional initial value to initiate the reduction. When
present, init is used to initial each yield.
...
Additional arguments, passed to the $new method of
the underlying reference class. Currently unused.
yieldNth
A positive integer indicating how many upstream yields
are combined before the Reducer yields. A value of
NA_integer_ indicates reduction of all records in the input
stream.
Methods
See Consumer Methods.
Internal Class Fields and Methods
Internal fields of this class are are described with, e.g.,
getRefClass("Reducer")$fields.
Internal methods of this class are described with
getRefClass("Reducer")$methods() and
getRefClass("Reducer")$help().
s <- Stream(Seq(to=10), Reducer("+"))
yield(s) ## sum(1:10), i.e., Reduce over the entire stream
s <- Stream(Seq(to=10), Reducer("+", yieldNth=5))
yield(s) ## sum(1:5)
yield(s) ## sum(6:10)
s <- Stream(Seq(to=10), Reducer("+", init=10, yieldNth=5))
sapply(s, c) ## 10 + c(sum(1:5), sum(6:10))
if (.Platform$OS.type != "windows") {
s <- Stream(Seq(to=10),
Team(function(i) { Sys.sleep(1); i },
param=MulticoreParam(10L)),
Reducer("+"))
system.time(y <- yield(s))
y
}
Results
R version 3.3.1 (2016-06-21) -- "Bug in Your Hair"
Copyright (C) 2016 The R Foundation for Statistical Computing
Platform: x86_64-pc-linux-gnu (64-bit)
R is free software and comes with ABSOLUTELY NO WARRANTY.
You are welcome to redistribute it under certain conditions.
Type 'license()' or 'licence()' for distribution details.
R is a collaborative project with many contributors.
Type 'contributors()' for more information and
'citation()' on how to cite R or R packages in publications.
Type 'demo()' for some demos, 'help()' for on-line help, or
'help.start()' for an HTML browser interface to help.
Type 'q()' to quit R.
> library(Streamer)
> png(filename="/home/ddbj/snapshot/RGM3/R_BC/result/Streamer/Reducer-class.Rd_%03d_medium.png", width=480, height=480)
> ### Name: Reducer
> ### Title: Consumer class to combine successive records
> ### Aliases: Reducer Reducer-class
> ### Keywords: classes
>
> ### ** Examples
>
> s <- Stream(Seq(to=10), Reducer("+"))
> yield(s) ## sum(1:10), i.e., Reduce over the entire stream
[1] 55
> s <- Stream(Seq(to=10), Reducer("+", yieldNth=5))
> yield(s) ## sum(1:5)
[1] 15
> yield(s) ## sum(6:10)
[1] 40
> s <- Stream(Seq(to=10), Reducer("+", init=10, yieldNth=5))
> sapply(s, c) ## 10 + c(sum(1:5), sum(6:10))
[1] 25 50
> if (.Platform$OS.type != "windows") {
+ s <- Stream(Seq(to=10),
+ Team(function(i) { Sys.sleep(1); i },
+ param=MulticoreParam(10L)),
+ Reducer("+"))
+ system.time(y <- yield(s))
+ y
+ }
[1] 55
>
>
>
>
>
>
> dev.off()
null device
1
>