Tag: recursion

我如何recursion和asynchronous构build一个未知大小的树?

我试图检索我的网站给定的职位上的评论,但我无法build立嵌套的评论,因为node.js的asynchronous性质。 getBlock([], function(){}); function getBlock(comments, callback) { comments.forEach(function(comment) { getChildComments(comment, function(err, children) { if (children) { getBlock(children, callback); } comment.Comments = children; /* not sure how to decide when to be done?*/ callback(null, comments); }); }); } 上面的代码适用于同步代码,但不适用于asynchronous代码,因为我无法确定comments何时包含所有要返回到浏览器的数据。 我试图跟踪recursion调用,并结束时,有0个电话留下来,但这是错误的,有时会根据树结构提前返回。

如何recursion地卸载所有node_modules文件夹

我有一个相当复杂的项目,有依赖和子依赖。 我的项目中有git子模块,它们之间有npm依赖关系。 有没有一种快速有效的方法来recursion地清理我的项目中的所有node_modules文件夹?

如何使用可以启动/停止的recursion函数编写应用程序

我有一个需要不断运行一个function的应用程序。 该函数返回一个承诺。 我希望应用程序一直等到承诺解决之后再次启动该function。 此外,我的应用程序需要一个start和stopfunction,使其开始运行该function,或分别停止它。 这里有一个简单的例子: class App { constructor() { this.running = false this.cycles = 0 } start() { this.running = true this._run() } stop() { this.running = false } _run() { Promise.resolve().then(() => { this.cycles++ }).then(() => { if (this.running) { this._run() } }) } } module.exports = App 我的问题是,当我使用这个, setTimeout似乎放弃了我。 例如,如果我运行这个: const App […]

确定recursion函数何时完成

我有一个function,在一个大图上进行广度优先search。 目前该应用程序运行并在一段时间后完成。 我想添加一个finished事件给EventEmitter。 我的第一个想法是为每个Recursive过程实现一个计数器。 但是如果某个Recursive进程没有调用counter– method,这可能会失败。 var App = function(start, cb) { var Recursive = function(a, cb) { // **asynchronous** and recursive breadth-first search } var eventEmitter = new EventEmitter(); cb(eventEmitter); Recursive(start); }; 如果所有的Recursive函数都完成了,怎样才能发出finished消息呢? 编辑应用程序不是search图中的东西,它必须遍历完整的graphics才能完成。 并不知道图中有多less元素。 Edit2像计算reflection一样是完美的,但似乎并不存在于JavaScript中。 该图非常不稳定,我正在做一些嵌套的asynchronous调用,都可能失败。 有没有办法知道什么时候所有的asynchronousrecursion调用完成而不使用计数器?

重新格式化节点中的JSON树

我正在尝试一个不同的方法来解决我之前的问题 。 基本上,我有一个JSON对象,如下所示: var data = { "tree": { "id": "99842", "label": "Bill", "children": [ { "id": "27878", "label": "Tom", "children": [] } ] }, "index": { "27878": { "birthdate": "1/21/1988", "spouse": "June", "hometown": "Tulsa, OK" }, "99842": { "birthdate": "4/15/1969", "spouse": "Mary", "hometown": "Dallas, TX" } } }; 正如你所看到的,有两个“顶级”项目:一个“树”对象和一个“索引”对象。 我想一起parsing它们来得到这个: { "rows": [ { […]

按键值recursionsortingJavaScript对象

通过其值也是对象的键sorting对象,并对该内部对象进行sorting,即recursionsorting对象。 sorting应该按键。 我看着Stackoverflow的其他问题,但没有一个是对象recursionsorting 。 我看了一下问题: 通过属性值对JavaScript对象进行sorting 例: input = { "Memo": { "itemAmount1": "5", "taxName1": "TAX", "productPrice1": "10", "accountName1": "Account Receivable (Debtors)" }, "Footer": { "productDescription2": "Maggie", "itemQuantity2": "49.5", "accountName2": "Account Receivable (Debtors)", "taxName2": "TAX" }, "Header": { "itemDiscount3": "10", "accountName3": "Account Receivable (Debtors)", "productPrice3": "10", "taxName3": "TAX" } } 产量 output = { "Footer": […]

我如何强制node.js做更深的recursion?

我有一个memoizedrecursionalgorithmf(n) 当我运行f(1000)一切正常,它需要不到一分钟的时间运行。 当我运行f(10000)我得到一个范围错误/最大调用堆栈大小超过。 我不相信这个错误与我的algorithm(这是归因于多项式)的效率有什么关系,似乎更像是一个configuration。 我试着做node app.js –stack-size=32000 ,并没有使错误消失。 我以为10X目前的堆栈大小就足够了。 这个堆栈大小是否可能太小,或者是否有办法在节点中强制/设置一个configuration来处理n = 10000情况? 此外, – 堆栈大小选项似乎没有做任何事情…我减less到“1”,我的程序运行速度一样快。

树结构中的recursion回溯

我有这个algorithm,我想实现一个图search,使用recursion回溯。 首先我的代码: public static boolean buildTree(GenericTreeNode<String> inputNode){ while(!interruptFlag) { try { Thread.sleep(200); } catch(InterruptedException e) {} gui.frame.MainWindow.progress.setText("Iterations Deployment: " + c); gui.panel.ResultMatrix.setResult(mappingList); Multimap<String,String> openList = LinkedHashMultimap.create(); openList = UtilityClasses.getOpenList.getOpenList(dataMap, ApplicationList, HardwareList, mappingList); if(openList.isEmpty() && !mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI)) { gui.frame.MainWindow.labelSuccess.setText("Mapping not succesful!"); return false; } if(openList.isEmpty() && mappingList.keySet().containsAll(XMLParser.ApplicationsListGUI)) { System.out.println(calculateOverallCost.getOverallCosts()); System.out.println("Mapping done:" + " " + mappingList); gui.panel.ResultMatrix.setResult(mappingList); […]

避免在基于promise的循环中发生recursion堆栈溢出?

作为一个简单的例子程序,我有一个连续ping服务器的节点脚本,希望这个程序长时间运行。 程序设置为一个返回一个promise对象的ping函数。 这个承诺是根据ping是工作还是失败来解决或拒绝的。 我希望这个函数在一个循环中运行,所以无论ping是否成功,在前一个请求被parsing之后的一段时间之后 ,下一个ping将被触发。 问题不是这个任务本身,而是我关心我的实现。 我相信最终会导致堆栈溢出。 这里有一些代码来看看发生了什么: function doPing(host) { // returns a promise object. } function doEvery(ms, callback, callbackArgs) { setTimeout(function() { callback.apply(null, callbackArgs) .always(function() { doEvery(ms, callback, callbackArgs); }); }, ms); } doEvery(1000, doPing, [host]); 我试图限制代码只是为了反映以下问题的范围: 这最终会导致堆栈溢出? 有一种模式可以防止使用promise的基于callback的循环溢出吗?

有JavaScript内存泄漏recursion调用callback函数?

比方说,例如,您正在编写一个程序,等待队列中的消息,对其进行处理,然后等待下一条消息,并且这个消息永远持续下去。 在像C或Java这样的语言中,它看起来像这样: void processMessage() { while (true) { // waitForMessage blocks until the next message is received msg = waitForMessage(); // handle msg here } } 在Javascript(我使用node.js,顺便说一句),因为使用callback,它通常看起来像这样: function processMessage() { waitForMessage(function(msg) { // handle msg or error here processMessage(); }); } 我的担心是,你基本上有一个callback链recursion调用原来的function,这样做的开销可能会慢慢地消耗内存。 我猜这实际上不是一个问题,因为也许JavaScriptcallback独立存在于自己的堆栈上,而不是推到原始函数堆栈上? 有人向我解释了JavaScriptcallback和范围,并向我保证,在运行时间长达任意长时间的情况下,JavaScript代码不会耗尽内存,同时接收到任意大量的消息