[Javascript] Intro to Recursion - Detecting an Infinite Loop
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的更多相关文章
- [Javascript] Intro to Recursion
Recursion is a technique well suited to certain types of tasks. In this first lesson we’ll look at s ...
- [Javascript] Intro to Recursion - Refactoring to a Pure Function
Previous post: http://www.cnblogs.com/Answer1215/p/4990418.html let input, config, tasks; input = [' ...
- 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 ...
- css infinite loop animation
css infinite loop animation @keyframes loop { 0% { transform: translateX(0%); } constructed styleshe ...
- [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 ...
- 深入理解 JavaScript 事件循环(一)— event loop
引言 相信所有学过 JavaScript 都知道它是一门单线程的语言,这也就意味着 JS 无法进行多线程编程,但是 JS 当中却有着无处不在的异步概念 .在初期许多人会把异步理解成类似多线程的编程模式 ...
- JavaScript 运行机制详解:Event Loop
参考地址:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 一.为什么JavaScript是单线程? JavaScript语言的一大特点就是 ...
- [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 ...
- JavaScript 运行机制详解:Event Loop——续
转自:http://www.ruanyifeng.com/blog/2014/10/event-loop.html 五.定时器 除了放置异步任务的事件,"任务队列"还可以放置定时事 ...
随机推荐
- memcache缓存命中深入理解转载
http://www.iteye.com/topic/225692 memcache的方法有 add,set,replace,get,delete,getstats,increment,decreme ...
- Dragger代码实现
转自:http://www.apkbus.com/blog-705730-60436.html 在工程中引入Dagger 如果想使用Dagger的话,需要添加两个函数库: dependencies { ...
- 增加Android模拟器的内存
1,在window中,打开'C:\Users\Administrator\.android\avd\4.4.2.avd\config.ini'文件(我的是win7,xp的貌似不是'Users',是'D ...
- java rmi 小记
最近在搞Quartz任务监控管理,碰到了jmx,后来发现Quartz对jmx的支持不是很好,介绍的文档也比较少,另外Quartz可以很方便的支持rmi于是就看了一下rmi.下面把写的一些测试小例子附上 ...
- C#中数据源绑定DataSource以及相关控件(DataGridView)的使用总结
我们在编程过程中,会涉及到表格数据的显示,存储等,就可能涉及到DataGridView,DataSource, DataTable等概念. 下面我就我自己模糊的一些知识点串讲以下: 1)首先我要讲的是 ...
- Java反射 - 3(动态代理)
动态代理是对包装模式的升级,可以动态的传入需要代理的对象实现代理 准备如下 1. 被代理类的接口 2.被代理类 3.处理器:InvocationHandler 4.代理调用:Proxy.newInst ...
- IO流(文件字节输入输出
输入输出流可能有不允许操作,可能有出现错误,必须在try语句中进行 FileOutputStream out1=new FileOutputStream("test1.txt") ...
- 磁盘阵列RAID
磁盘阵列RAID 年提出的.RAID名为独立 ...
- centos 下 apache 重启启动命令
apache 启动 usr/local/apache243/bin/apachectl start apache 重启 usr/local/apache243/bin/apachectl restar ...
- 列表框List Box
List Box/Check List Box ListBox窗口用来列出一系列的文本,每条文本占一行.创建一个列表窗口可以使用成员函数: BOOL CListBox::Create( LPCTSTR ...