[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 五.定时器 除了放置异步任务的事件,"任务队列"还可以放置定时事 ...
随机推荐
- css空格和去浮动的应用
今天做了项目用到css,请教前端解决,第一个是记得css空格之间的关系是隶属关系,但是在元素中却是并列关系,如<div class="right_side_item_moban gra ...
- 关于.NET中的验证码
常用的生成验证码程序 ,图片效果如下: 源程序如下: 复制代码 代码如下:using System; using System.IO; using System.Drawing; using Syst ...
- 《CSS网站布局实录》学习笔记(六)
第六章 CSS高级应用与技巧 6.1 id与class 6.1.1 什么是id id是XHTML元素的一个属性,用于标识对象名称.无论是class还是id,都是XHTML所支持的公共属性,并且也是其核 ...
- WCF调用问题
1---WCF常用的节点绑定 basicHttpBinding和netTcpBinding 当通过Spring对象注入 如 WCFForOrderService 对象注入 public WCFForO ...
- 一个打砖块的小游戏1.0 KILL THE BLOCKS !
/******************************************** * 程序名称:MR.DUAN 的打砖块(KILL THE BLOCKS !) * 作 者:WindAutum ...
- c-参数(argument)
In C, array arguments behave as though they are passed by reference, and scalar variables and cons ...
- VM下Linux网卡丢失(pcnet32 device eth0 does not seem to be ...)解决方案
系统启动日志:Bringing up interface eth0: pcnet32 device eth0 does not seepresent, delaying initialization. ...
- 用css3实现闪烁效果
1. css3 @keyframes 参考 css3 @keyframes规则. 特别注意浏览器支持: Internet Explorer 10, Firefox, 和 Opera 支持 @keyfr ...
- android JNI (二) 第一个 android工程
下载NDK 后 它自带有 sample,初学者 可以导入Eclipse 运行 这里 我是自己创建的一个新工程 第一步: 新建一个Android工程 jni_test(名字自取) 第二步:为工程添加 本 ...
- Win7下Solr4.10.1和TomCat8的安装
1.系统为win7 64位系统,安装有wamp的环境,我的所有网站放在 d:\webserver下,域名指向该目录下的子目录: 2.安装TomCat8到 D:\Tomcat 8.0: 3.在 d:\w ...