Skip to content
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

Closed
wants to merge 7 commits into from
Closed

Conversation

sadasant
Copy link
Contributor

No description provided.

@sadasant sadasant self-assigned this Mar 14, 2017
@sadasant sadasant requested a review from daedalus28 March 14, 2017 04:53
@sadasant sadasant changed the title Feature/fold while (alternative to deepFind) foldWhile (alternative to deepFind) Mar 14, 2017
@sadasant sadasant force-pushed the feature/foldWhile branch from 0a28293 to c654e5a Compare March 14, 2017 10:31
@sadasant
Copy link
Contributor Author

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'`
@sadasant sadasant force-pushed the feature/foldWhile branch from 7fa114f to b4a6be2 Compare March 14, 2017 21:33
@sadasant sadasant force-pushed the feature/foldWhile branch 2 times, most recently from 8ff5ce0 to 7773611 Compare March 14, 2017 21:36
@sadasant
Copy link
Contributor Author

Ramda has: http://ramdajs.com/docs/#reduceWhile but it's not recursive.

There was a lodash-deep repo but it now only does recursion over mapValues: https://github.com/marklagendijk/lodash-deep

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 every, which allows lazy evaluation.

@sadasant
Copy link
Contributor Author

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
})
Copy link
Contributor Author

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 :)

@sadasant
Copy link
Contributor Author

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
}

@sadasant
Copy link
Contributor Author

Other libraries:

(loading)

sadasant added a commit that referenced this pull request Mar 15, 2017
More abstract universal
[foldWhile](#58),
for infinite reducers.
sadasant added a commit that referenced this pull request Mar 15, 2017
More abstract universal
[foldWhile](#58),
for infinite reducers.
sadasant added a commit that referenced this pull request Mar 15, 2017
More abstract universal
[foldWhile](#58),
for infinite reducers.
@sadasant
Copy link
Contributor Author

Closing this on favor of the groupid

@sadasant sadasant closed this Mar 18, 2017
@sadasant sadasant deleted the feature/foldWhile branch March 18, 2017 22:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant