-
Notifications
You must be signed in to change notification settings - Fork 68
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
foldWhile (alternative to deepFind) #58
Conversation
0a28293
to
c654e5a
Compare
The name "foldWhile" might not specify "depth", so perhaps something else? |
deepTransform Transforms a recursive iterable by a given function. Works like deepMap but allows you to stop transforming at any point of the tree. The traversing happens depth first. deepFind Goes over every `{ key, value }` of a recursive iterable data structure (for arrays being numeric key and the value at the key position), and uses a function to extract elements at any point in the tree onto an array that is returned. If a third argument, the limit, is provided, it will stop iterating as soon as the number of found objects reaches that limit. Why? Because this will allow me to find the first two nested occurrences of a terms object with a field property and to modify the first so that it has: `collect_mode: 'breadth_first'`
7fa114f
to
b4a6be2
Compare
8ff5ce0
to
7773611
Compare
Ramda has: http://ramdajs.com/docs/#reduceWhile but it's not recursive. There was a There are several other things that are deprecated and/or don't stop, like: https://github.com/Carrooi/Node-RecursiveMerge , https://github.com/Raynos/reduce , https://github.com/jonschlinkert/object.reduce 🍭 I'll make the PR to lodash, but this is pretty generic though, Haskell has these kind of functional structures everywhere. It follows the nature of a reduce, but it separates reduce's accumulation from the recursive use of |
Lodash-contrib has a recursive walk, but it doesn't stop: https://github.com/node4good/lodash-contrib/blob/master/dist/lodash-contrib.js#L348 we can use it to map and reduce, but for efficiency we would need to make a PR. I'm not sure if we need their other functions though: https://github.com/node4good/lodash-contrib/blob/master/docs/index.md |
let innerFold = o => !isTraversable(o) ? o : _.every(k => fn(r, o[k], k) && innerFold(o[k]), _.keys(o)) | ||
innerFold(obj) | ||
return r | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@daedalus28 fixed the style though :)
Fun fact: you can write it without lodash: const foldWhile = (fn, obj, r = []) => {
let innerFold = o => !(typeof o === 'object') ? o
: Object.keys(o).every(k => fn(r, o[k], k) && innerFold(o[k]))
innerFold(obj)
return r
} |
Other libraries:
(loading) |
More abstract universal [foldWhile](#58), for infinite reducers.
More abstract universal [foldWhile](#58), for infinite reducers.
More abstract universal [foldWhile](#58), for infinite reducers.
Closing this on favor of the groupid |
No description provided.