When using recursion, you must be mindful of the dreaded infinite loop. Using the recursive function that we’ve built up over the previous lessons, we look at how a simple duplicated configuration item could cause chaos for our program as it has no context of which items it has previously seen. We fix this problem by introducing a parents array, which can keep track of which top-level commands have already been accessed.

Previous:

let input, config;

input = ['dist'];

config = {
"dist": ["build", "deploy"],
"build": ['js', 'css', 'vender'],
"js": ['babel', 'ng-Annotate', "uglify"],
"css": ["sass", "css-min"]
}; var res = getTasks(config, input, []); function getTasks(config, input, initial){ return input.reduce((prev, next)=>{
if(config[next]){
return getTasks(config ,config[next], prev);
}else{
return prev.concat(next);
}
}, initial);
}; console.log(res);

----------

Code:

let input, config;

input = ['dist'];

config = {
"dist": ["build", "deploy"],
"build": ['js', 'css', 'vender', 'dist'],
"js": ['babel', 'ng-Annotate', "uglify"],
"css": ["sass", "css-min"]
}; var res = getTasks(config, input); function getTasks(config, input, initial, parent){ initial = initial || [];
parent = parent || []; return input.reduce((prev, next)=>{ if(parent.indexOf(next) > -1){
console.log('infinite loop detected!');
return prev;
} if(config[next]){
return getTasks(config ,config[next], prev, parent.concat(next));
}else{
return prev.concat(next);
}
}, initial);
}; console.log(res);

[Javascript] Intro to Recursion - Detecting an Infinite Loop的更多相关文章

  1. [Javascript] Intro to Recursion

    Recursion is a technique well suited to certain types of tasks. In this first lesson we’ll look at s ...

  2. [Javascript] Intro to Recursion - Refactoring to a Pure Function

    Previous post: http://www.cnblogs.com/Answer1215/p/4990418.html let input, config, tasks; input = [' ...

  3. Infinite loop when using cookieless session ID on Azure

    If you use cookieless session ID and deploy them on Azure, you might get infinite loop when you quer ...

  4. css infinite loop animation

    css infinite loop animation @keyframes loop { 0% { transform: translateX(0%); } constructed styleshe ...

  5. [Algorithms] Refactor a Loop in JavaScript to Use Recursion

    Recursion is when a function calls itself. This self calling function handles at least two cases, th ...

  6. 深入理解 JavaScript 事件循环(一)— event loop

    引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...

  7. JavaScript 运行机制详解:Event Loop

    参考地址:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 一.为什么JavaScript是单线程? JavaScript语言的一大特点就是 ...

  8. [Javascript] Intro to the Web Audio API

    An introduction to the Web Audio API. In this lesson, we cover creating an audio context and an osci ...

  9. JavaScript 运行机制详解:Event Loop——续

    转自:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 五.定时器 除了放置异步任务的事件,"任务队列"还可以放置定时事 ...

随机推荐

  1. phpmyadmin导出数据库为什么是php文件

    你的迅雷在作怪,把它卸载了,或者在迅雷的高级设置中,关闭监听浏览器,就不会触发迅雷下载,就没问题了.或者360浏览器的话,把急速模式改为兼容模式

  2. 《CSS网站布局实录》学习笔记(一)

    今天开始,认真学习前端技术,哈哈哈~~~加油~~~ 推荐这本<CSS网站布局实录>(第2版)给初级入门选手,虽然这本书年代有点久远,不过很经典. 注明一下:这里讲述的CSS均为CSS 2. ...

  3. MySQL MyISAM/InnoDB高并发优化经验

    最近做的一个应用,功能要求非常简单,就是 key/value 形式的存储,简单的 INSERT/SELECT,没有任何复杂查询,唯一的问题是量非常大,如果目前投入使用,初期的单表 insert 频率约 ...

  4. 华为S5300交换机配置基于接口的本地端口镜像

    配置思路 1.  将Ethernet0/0/20接口配置为观察端口(监控端口) 2.  将Ethernet0/0/1----Ethernet0/0/10接口配置为镜像端口 配置步骤 1.  配置观察端 ...

  5. swift 闭包 由浅入深 优化

    //: Playground - noun: a place where people can play import UIKit ////////////////////////////////// ...

  6. 洛谷 P1515 旅行

    P1515 旅行 题目描述 你要进行一个行程为7000KM的旅行,现在沿途有些汽车旅馆,为了安全起见,每天晚上都不开车,住在汽车旅馆,你手里现在已经有一个旅馆列表,用离起点的距离来标识,如下: 0, ...

  7. Uncaught TypeError: Cannot set property 'innerHTML' of null

    学习Chrome插件时,要在弹出页面中显示当前时间,结果怎样也显示不出来 看了 http://www.cnblogs.com/mfryf/p/3701801.html 这篇文章后感悟颇深 通过调试发现 ...

  8. JNDI Tutorial

    Naming Concepts A fundamental facility in any computing system is the naming service--the means by w ...

  9. 关于@font-face的一些问题

    @font-face 能够加载服务器端的字体文件,让客户端显示客户端所没有安装的字体.[微软的IE 5已经是开始支持这个属性,但是只支持微软自有的.eot (Embedded Open Type) 格 ...

  10. 《python基础教程》笔记之 其它语句1

    print 相关 print可以打印多个表达式,只要将它们用逗号隔开就好,结果中每个参数之间都会插入一个空格,使用+可以避免空格,如 >>> print 'age:',42age: ...