question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Lazy but finite reduce for recursive data structures and an unbounded number of reducers (see comments)

See original GitHub issue

Good day everyone!

Over the past weeks we’ve been working with arbitrarily nested objects and arrays (mixed altogether), up until we’ve spotted the necessity of a recursive reduce function that could be used to transform, but at least to obtain, certain if not every property of this nested structure, which could also be stopped at any given point if a condition is sufficient. Essentially, we’ve been looking at what in haskell would be a fold takeWhile, but for deep/recursive objects/arrays.

After several iterations, the function valid for these use cases that we haven’t been able to simplify anymore is the following:

// Recursive fold takeWhile (like in haskell)
// fn is the iterator
// obj is the iteratee
// r is the accumulator
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
}

Note: this is implemented with lodash in our code, but since it doesn’t need lodash, I’m using generic es6 to show what it does.
Note2: Besides using functions from the Ramda ecosystem, this would benefit from a trampoline.

The main point of the function is combining the use of every recursively to stop at any point of the (lazy) evaluation of the nested object/array, with the use of an accumulator, which is handled to the user.

To see use cases and tests, see this gist: https://gist.github.com/sadasant/de106b879c61f21797dffdaa577f7868

We have been looking for opensource alternatives, but we haven’t been successful.

I know that there have been several issues in this repo about lazy evaluation and nested data structures, however there doesn’t seem to be an agreement on how to combine both the nested structures and the stopping of the fold at any given point. I know that Ramda has reduceWhile and until, but they don’t work over nested structures, right?.

Having that said,
I wonder if this is interesting to anybody. We would definitely prefer using opensource instead of maintaining our own code.

Thank you for the time.

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Reactions:10
  • Comments:55 (16 by maintainers)

github_iconTop GitHub Comments

2reactions
Izhakicommented, Dec 5, 2017

Why was this issue closed when the PR (#2133) is still open? And why is the PR still open?

2reactions
buzzdecafecommented, Mar 20, 2017

i’m interested in seeing a PR for this

Read more comments on GitHub >

github_iconTop Results From Across the Web

Recursive Data Structures and Lazy Evaluation
In this article I'm trying to explain to myself recursive data structures and how they work together with lazy evaluation.
Read more >
Recursive data structures - Google Groups
2 and define a generalized graph in terms of sets of nodes & edges with restrictions, but I'm interested in whether there are...
Read more >
',, ADS "r'|) h Frederic R. Morgenthaler - CORE
recursive procedure. Operational control is achieved through synchronized lazy aggregates, dynamically unfolding data structures that constrain how the ...
Read more >
@@ @@ @@ @@ @@ @@ @@ @@ @@ @@ AA AA ...
structuring elements are higher order functions and lazy data structures ... With the recursive function from, an infinite list of numbers starting at...
Read more >
Trends In Functional Programming , Volume 4 - X-Files
growth in the number of states. A number of extended finite-state languages have been proposed incorporating composition, communication and data structures ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found