Last active
August 14, 2017 12:46
-
-
Save busypeoples/346ac6098ff7cd20c9c25f35f6e5dcbe to your computer and use it in GitHub Desktop.
nest map for ramda
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { | |
ifElse, | |
map, | |
partial, | |
} from 'ramda' | |
const isObject = input => typeof input === 'object' | |
/** | |
* Like map but for deeply nested objects | |
* | |
* @param {Function} f function to apply | |
* @param {Object} obj | |
*/ | |
const nestedMap = (f, obj) => map(ifElse(isObject, partial(nestedMap, [f]), f), obj) | |
// verify | |
nestedMap(x => x + 5, [1, [[[2]]]]) | |
// [ | |
// 6, | |
// [ | |
// [ | |
// [ | |
// 7 | |
// ] | |
// ] | |
// ] | |
// ] | |
nestedMap(x => x + 5, {a: 1, b: { c: { d: 2 }}}) | |
// {"a":6,"b":{"c":{"d":7}}} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment