此代码在其下方生成堆栈跟踪:
import R from '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);
堆栈跟踪:
$ 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
at Array.toString (native)
at 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)
at 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)
at 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)
at type (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:3345:100)
at f1 (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:166:27)
at _equals (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4038:13)
at equals (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:4664:16)
at f2 (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:201:24)
at Object.isEmpty (/Users/ivan/dev/quicksort/node_modules/ramda/dist/ramda.js:5220:29)
我虽然那个 babel 会处理尾调用优化?不管怎样,这么短的名单,应该没那么深吧?