Skip to content

Instantly share code, notes, and snippets.

@busypeoples
Last active August 14, 2017 12:46
Show Gist options
  • Save busypeoples/346ac6098ff7cd20c9c25f35f6e5dcbe to your computer and use it in GitHub Desktop.
Save busypeoples/346ac6098ff7cd20c9c25f35f6e5dcbe to your computer and use it in GitHub Desktop.
nest map for ramda
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