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

Reduce blows call stack #3

Open
wvl opened this issue Mar 21, 2011 · 2 comments
Open

Reduce blows call stack #3

wvl opened this issue Mar 21, 2011 · 2 comments

Comments

@wvl
Copy link

wvl commented Mar 21, 2011

var arr = [];
for (var i=0; i<=100000; i++) { arr.push(i) }
zo(arr).reduce(0, function(memo, item, into) {
  into(memo + item);
}).results(function(sum) {
  console.log("Sum", sum);
});

This will now blow the call stack, since reduce is recursive.

fwiw, I was looking at zo (which I hadn't seen before), because I was writing my own functional async library, and ran into the above problem. I just figured it out -- thanks to mikegerwitz on irc. If you use process.nextTick, you won't exhaust the call stack.

So, in your foldl, do:
process.nextTick(function() {
xyz(folded, index + 1, items, next);
})

Cheers

@refractalize
Copy link
Owner

I've got to say, I'm having a tough time blowing the call stack on my machine, even with 10M items in the list... running node 0.4.3. That said, nice solution to the problem.

@refractalize
Copy link
Owner

My reduce is now sequential, so it will spend a lot of its time waiting for IO to complete. I think there's room in here for an reduce that doesn't preserve order on the folded result. Good for things like sum.

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

No branches or pull requests

2 participants