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. Oracle数据库锁表的查询方法以及解锁的方法

    1,锁表语句简单查询方法   select t2.username,t2.sid,t2.serial#,t2.logon_time from v$locked_object t1,v$session ...

  2. rtsp转发服务器设计

    做一个设备实时监控.需求是这样的,一个用户有多个设备(android系统,支持摄像头),设备分布在家中或者其它地方:用户可以远程通过终端(手机.pc.ipad,etc...)管理操纵这些设备(包括实时 ...

  3. windows下安装mysql笔记

    接着上几篇文章再来看下windows下安装mysql. 我这里是windows7 64位, 安装过程中还是遇到一些坑,这里记录下. 一.下载安装包 打开mysql官网下载页面:http://dev.m ...

  4. thinkphp 配置

    ThinkPHP框架中所有配置文件的定义格式均采用返回PHP数组的方式,格式为: //项目配置文件 return array( 'DEFAULT_MODULE' => 'Index', //默认 ...

  5. oracle创建主键序列和在ibatis中应用

    oracle创建主键序列 oracle主键序列的查询和ibitas中应用

  6. Pycharm在创建py文件时,如何自动添加文件头注释(类似于钩子特性)?

    在每次新建一个py文件的时候 1 如何自动添加/usr/bin/env python2 自动添加 coding=utf8 操作方法: File->settings->Editor-> ...

  7. Python----Tornado安装

    Tornado安装,环境准备:          1.python安装包及安装 2.Tornado安装包 Python包安装 Linux下安装 如果使用的是 Linux系统 或 Mac OS X ,系 ...

  8. mysql_config not found

    在python中安装MySQL_python.不想通过下载源码编译,而是想用 easy_install MySQL_python 来安装.结果一直报错: mysql_config not found ...

  9. Webbrowser判断页面加载完成

    Webbrowser 请求加载页面,页面中包含各种资源,不能够很准确的判断加载是否完成,需要通过特定的方法判断. 1.使用计数器判断页面是否加载完成.精准可控. // 计数器 ; // 添加事件响应函 ...

  10. Swift—类型检查与转换-备

    继承会发生在子类和父类之间,是一系列类的继承关系. 例如:Person是类层次结构中的根类,Student是Person的直接子类,Worker是Person的直接子类. 这个继承关系类的具体实现代码 ...