pull-window
Aggregate a pull-stream into windows.
Several helpers are provided for particular types of windows,
sliding, tumbling, etc.
And also, a low level
Example: "tumbling" window
sum every 10 items.
var pull = require('pull-stream')
var window = require('pull-window')
function everyTen () {
var i = 0
return window(function (data, cb) {
if(i != 0) return
var sum = 0
return function (end, data) {
if(end) return cb(null, sum)
sum += data
if(++i >= 10) {
i = 0
cb(null, sum)
}
}
}
}
pull(
pull.count(1000),
everyTen(),
pull.log()
)
Example: variable sized window
Each window doesn't have to be the same size...
var pull = require('pull-stream')
var window = require('pull-window')
function groupTo100 () {
var sum = null
return window(function (_, cb) {
if(sum != null) return
return function (end, data) {
if(end) return cb(null, sum)
sum += data
if(sum >= 100) {
var _sum = sum; sum = null
cb(null, _sum)
}
}
})
}
pull(
pull.count(1000)
groupTo100(),
pull.log()
)
Example: sliding window
to make more over lapping windows
just return the window function more often.
var pull = require('pull-stream')
var window = require('pull-window')
function sliding () {
return window(function (_, cb) {
var sum = 0, i = 0
return function (end, data) {
if(end) return cb(null, sum)
sum += data
if(++i >= 10) {
cb(null, sum)
}
}
})
}
pull(
pull.count(100)
sliding(),
pull.log()
)
API
window (start, map)
window(function startWindow (data, cb) {
return function addToWindow (end, data) {
}
}, function mapWindow (start, data) {
})
By default, windows are mapped to {start: firstData, data: aggregate}
.
unless you pass in an different mapWindow
function.
window.sliding(reduce, size)
reduce every size
items into a single value, in a sliding window
window.recent(size, time)
tumbling window that groups items onto an array,
either every size
items, or within time
ms,
which ever occurs earliest.
License
MIT