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

Maximum call stack size exceeded while evaluating lazy sequence #38

Closed
hallettj opened this issue Sep 27, 2013 · 6 comments
Closed

Maximum call stack size exceeded while evaluating lazy sequence #38

hallettj opened this issue Sep 27, 2013 · 6 comments

Comments

@hallettj
Copy link

This is probably an issue for the upstream ClojureScript project. But I wanted to post this to see if anyone knows of a workaround.

I am working on a blog post on Mori with some examples of lazy evaluation. Here is what I have at the moment:

// With no arguments, `range` returns a lazy sequence of all whole
// numbers from zero up.
var non_neg_ints = mori.range();

// Dropping the first element, zero, results in a sequence of all of
// the natural numbers.
var nats = mori.drop(1, non_neg_ints);

// Let's take just the powers of 2.
var ln_2   = function(n) { return Math.log(n) / Math.LN2; };
var pows_2 = mori.filter(function(n) { return ln_2(n) % 1 === 0; }, nats);

// What are the first 10 powers of 2?
console.log(
    mori.take(10, pows_2)
);

// Outputs:
// > (1 2 4 8 16 32 64 128 256 512)

That works nicely. But if I change the number of evaluated sequence elements from 10 to 15 (in the node repl) I get a stack overflow exception:

> mori.take(15, pows_2);

/home/jesse/tmp/node_modules/mori/mori.js:26
b?this.Ca===b.Ca:l};Ub.prototype.toString=m("Ca");function D(a){if(a==j)return
                                                                    ^
RangeError: Maximum call stack size exceeded
@hallettj
Copy link
Author

Ok, I confirmed that this is an upstream bug:

cljs.user> (defn nats [] (drop 1 (range)))
cljs.user> (defn log-2 [n] (/ (Math.log n) Math.LN2))
cljs.user> (defn is-pow-2 [n] (= (mod (ln-2 n) 1) 0))
cljs.user> (defn pows-2 [] (filter is-pow-2 (nats)))
cljs.user> (take 10 (pows-2))
(1 2 4 8 16 32 64 128 256 512)
cljs.user> (take 15 (pows-2))
Compilation error: RangeError: Maximum call stack size exceeded

@swannodette
Copy link
Owner

Yes recently fixed this one in ClojureScript will cut a new release of Mori soon to address.

@hallettj
Copy link
Author

Nice! Thanks for the quick response! And by the way, I am really enjoying this library.

@swannodette
Copy link
Owner

Fixed, 0.2.4 just went out.

@hallettj
Copy link
Author

Very nice! Thanks again!

@hallettj
Copy link
Author

By the way, I published that post: http://sitr.us/2013/11/04/functional-data-structures.html

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