R: Create map and reduce functions from other functions

to.map {rmr2} | R Documentation

## Create map and reduce functions from other functions

### Description

These utility functions are meant to avoid the little boilerplate code necessary to convert ordinary functions into map and reduce functions.

### Usage

    	to.map(fun1, fun2 = identity)
    	to.reduce(fun1, fun2 = identity)

### Arguments

`fun1` | A function to apply to the key, or to the key-value pair if the second argument is missing
`fun2` | A function to apply to the value

### Details

Sometimes there are functions that we could use almost directly as map or reduce functions but for a bit of boilerplate code, and we hate boilerplate code. That's where the functions documented herein can help. They take one or two functions of a single argument and return a valid map or reduce function. In the case of `to.map` when two functions are specified they are applied independently to the key and the value and the return values are returned as a key-value pair; when only one is, it is applied to the key-value pair. For `to.reduce` the behavior is the same.

### Examples

    ##The identity map:
    to.map(identity)
    ## equivalent to function(k, v) keyval(k, v)
    ##Replace key with mod 10 of the key and pass the value along:
    to.map(function(x) x %% 10, identity )
    ##Sum up all the values for the same key:
    to.reduce(identity, function(vv) sum(vv))

[Package rmr2 version 3.3.1 Index]