node.js - RangeError: Maximum call stack size exceeded (Babel Ramda) -
this code produces stack trace below it:
import r 'ramda'; function quicksort(list) { if ( r.isempty(list) ) return list; let pivot = r.head(list); let lesser = r.filter( e => e < pivot , list ); let greater = r.filter( e => e >= pivot, list ); return r.concat( quicksort(lesser), pivot, quicksort(greater) ); } var sorted = quicksort([2,3,1]); console.log(sorted);
stack trace:
$ babel-node quicksort2015.js /users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.object.to-string.js:3 var classof = require('./$.classof') ^ rangeerror: maximum call stack size exceeded @ array.tostring (native) @ module.exports (/users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/$.cof.js:4:19) @ module.exports (/users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/$.classof.js:13:13) @ array.tostring (/users/ivan/.nvm/versions/node/v4.1.2/lib/node_modules/babel/node_modules/babel-core/node_modules/core-js/modules/es6.object.to-string.js:8:25) @ type (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:3345:100) @ f1 (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:166:27) @ _equals (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4038:13) @ equals (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4664:16) @ f2 (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:201:24) @ object.isempty (/users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:5220:29)
i though babel handle tail call optimization? in case, it's such short list, should not deep, right?
this due filtering entire list, rather tail of list lesser
, greater
.
e.g. try following:
function quicksort(list) { if (list.length <= 1) return list; var x = r.head(list); var xs = r.tail(list); var lesser = r.filter(r.lt(r.__, x), xs); var greater = r.filter(r.gte(r.__, x), xs); return r.concat(quicksort(lesser), r.prepend(x, quicksort(greater))); }
unfortunately doesn't benefit tail-call optimisation either, recursive calls quicksort
not in tail-call position.
Comments
Post a Comment