A functional query engine, that operates over streams of js objects, and can be optimized via database indexes.
count the total number of mentions in ssb.
var mfr = require('map-filter-reduce')
pull(
ssbc.createLogStream(),
mfr([
//get all posts.
{$filter: { value: { content: { type: 'post' } } }},
//extract the mentions
{$map: {mentions: ['value', 'content', 'mentions', 'length']}},
//sum all the lengths
{$reduce: {$sum: true}}
])
pull.log()
)This example uses a full scan, so it looks at every object in the database. But if we have indexes, that can be made much more efficient, see flumeview-query
map-filter-reduce provides three functional stream processing
operators. usually, generally, filter should be used first,
then map then reduce, but operators can be combined in any order,
and used any number of times.
a filter removes items from an incoming stream.
strings, numbers and booleans match exactly.
{$filter: 'okay'}matches all strings that equal "okay"
$prefix operator matches strings that start with a given string.
{$filter: {$prefix: 'o'}}this will match "okay" "ok" "oh no", etc.
match values that are greater than, less than, greater than or equal, or less than or equal. A greater operator and a lesser operator may be combined into one comparison.
{$filter: {$gte: 0, $lt: 1}}will filter numbers from 0 up to but not including 1.
basic operations:
{$eq: value}test input is equal to value{$ne: value}test input is not equal to value{$truthy: true}test if input converts to true.{$not: true}test if input converts to false.{$type: true}returns thetypeofthe input. (useful in $map, but not $filter){$is: 'object'|'string'|'number'|'boolean'|'undefined'}test if input is particular type, use instead of$typein filters.{$in: [...]}check if input is one of a set. input must be a primitive, not an object.
an object is used to filter objects that have the same properties. an object is combined with other operators that apply to the keys.
{$filter: {name: {$prefix: 'bob'}, age: {$gt: 10}}}select "bob", "bobby", and "bobalobadingdog", if their age is greater than 10.
arrays are used treated somewhat like strings, in that you can also
use the $prefix and $gt, $lt, $gte, $lte operators.
arrays are like objects, but their keys are compared in order from left to right.
{$filter: {$prefix: ['okay']}}
If the incoming stream has fields you are not interested in, or fields that you'd like to remap, you can map those objects.
lets say, we have a nested object incoming, but we just want to pluck off the a nested name field.
{$map: {name: ['value', 'name']}}if the input was {key: k, value: {foo: blah, name: 'bob'}}
the output would just be {name: 'bob'}.
strings and numbers are used to get keys off the input.
{$map: 'value'}would return the value property of the input, and drop the rest. use numbers (integers) if the input will be an array.
for a {} object, each value is considered a rule, and applied to the value of the incoming data. a value with the transformations applied will be returned.
the keys in the object do not need to be the same as in the input, since the values will map to which keys to operate on from the input.
{$map: {foo: 'bar', bar: 'foo'}}transform the input stream into a stream with foo and bar keys switched,
and any other keys dropped.
arrays are used to lookup deep into an object. they are analogous to useing chaining dots in javascript.
{$map: ['value', 'name']}get the value property, and then get the name property off that.