[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 五.定时器 除了放置异步任务的事件,"任务队列"还可以放置定时事 ...
随机推荐
- C#网络编程之WebClient
1.什么是WebClient? 源自MSDN:提供用于将数据发送到由 URI 标识的资源及从这样的资源接收数据的常用方法. 2.OpenRead() 为从具有String指定的URI的资源下载的数据 ...
- 请写一个C函数,判断处理器是大端存储还是小端存储,若处理器是Big_endian的,则返回0;若是Little_endian的,则返回1
[解答] int checkCPU() { { union w { int a; char b; }c; c.a=1; return (c.b==1); } } [剖析] 嵌入式系统开发者应该对Lit ...
- cmd命名设置成全局
如在jsdoc里.想要把jsdoc命名设置成全局.只要把环境变量里面的用户变量里面的path变量值增加 C:\Program Files\nodejs;E:\Program Files (x86)\j ...
- 1.3.4 try-with-resources (TWR)
其基本设想是把资源(比如文件或类似的东西)的作用域限定在代码块内,当程序离开这个代码块时,资源会被自动关闭: 要确保try-with-resources生效,正确的用法是为各个资源声明独立变量: 目前 ...
- JQUERY1.9学习笔记 之基本过滤器(二) 等于选择器
等于选择器 :eq() 描述:选择与设定下标匹配的元素.jQuery( ":eq(index)" )jQuery( ":eq(-index)" ) <!D ...
- LNMP笔记:域名重定向、读写权限、显示WP主题、北京时间
边写边记,以后还会用到的. 将 xxx.com 重定向到 www.xxx.com 1.打开 /usr/local/nginx/conf/vhost/你网站的域名.com.conf 2.查看原有的 se ...
- R for installing package 'omg'
The time i have tried to install the package named 'PODBC' and it worked. But now i meet a problem ...
- Unity中的各种寻找GameObject方法
1.GameObject.Find():寻找Hierarchy面板中的activie 不为false的游戏对象: 路径如官方事例写法: public class ExampleClass : Mono ...
- (摘)SP2-0750: You may need to set ORACLE_HOME to your Oracle software directory
Linux下安装好Oracle 10g后运行sqlplus出现故障如下:[oracle@localhost oracle]$ ./sqlplusError 6 initializing SQL*Plu ...
- 涂抹Oracle—Flashback
11.1 基于flashback查询过去的数据 a.基于时间的查询(as of timestamp) 构造表falsh_tbl,删除数据然后查询 SQL>select * from flash ...