Previous post: http://www.cnblogs.com/Answer1215/p/4990418.html

let input, config, tasks;

input = ['dist'];

config = {
"dist": ["build", "deploy"],
"build": ['js', 'css', 'vender'],
"js": ['babel', 'ng-Annotate', "uglify"],
"css": ["sass", "css-min"]
}; tasks = []; getTasks(input); function getTasks(input){ input.forEach((task)=>{
if(config[task]){
getTasks(config[task]);
}else{
tasks.push(task);
}
})
}; console.log(tasks);

The getTasks works but has some problem:

  • we depend on the outside variable 'tasks' and 'config', so there are side effect
  • there is no return value, hard to test
let input, config, tasks;

input = ['dist'];

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

The code has been improved, we return the value from the getTasks() function and we don't modify the tasks array anymore.

Just one thing we still need to do is we still depend on 'config':

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);

[Javascript] Intro to Recursion - Refactoring to a Pure Function的更多相关文章

  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 - Detecting an Infinite Loop

    When using recursion, you must be mindful of the dreaded infinite loop. Using the recursive function ...

  3. Function Programming - 纯函数(Pure Function)

    纯函数的定义,非常重要!! Pure function 意指相同的输入,永远会得到相同的输出,而且没有任何显著的副作用. 老样子,我们还是从最简单的栗子开始: var minimum = 21; va ...

  4. react事件绑定的三种常见方式以及解决Cannot update during an existing state transition (such as within `render`). Render methods should be a pure function of props and state问题思路

    在 React 组件中,每个方法的上下文都会指向该组件的实例,即自动绑定 this 为当前组件. 而且 React 还会对这种引用进行缓存,以达到 CPU 和内存的优化.在使用 ES6 classes ...

  5. 动手实现 Redux(三):纯函数(Pure Function)简介

    我们接下来会继续优化我们的 createStore 的模式,让它使我们的应用程序获得更好的性能. 但在开始之前,我们先用一节的课程来介绍一下一个函数式编程里面非常重要的概念 —— 纯函数(Pure F ...

  6. [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 ...

  7. [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 ...

  8. JavaScript——关于字符串的replace函数中的function函数的参数

    <!DOCTYPE> <html> <head> </head> <body> <script type="text/jav ...

  9. JavaScript(JS)之Javascript对象BOM,History,Location,Function...(二)

    https://www.cnblogs.com/haiyan123/p/7594046.html 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创 ...

随机推荐

  1. 运用linq查找所有重复的元素

    如题: 有一个List<string>类型的List<T> List<String> list = "};` 需要返回结果List包含 {"6& ...

  2. 更新Xcode7 后 .dylib变成了.tbd的问题解决

    拿添加libsqlite3.dylib为例 1.打开你添加的libsqlite3.tbd 文本文件,然后有一行 install-name:    /usr/lib/libsqlite3.dylib   ...

  3. JavaScript验证身份证号

    <%@ page language="java" contentType="text/html; charset=GB18030" pageEncodin ...

  4. cocos2dx 3.2中的物理引擎初探(一)

    cocos2dx在设计之初就集成了两套物理引擎,它们是box2d和chipmunk.我目前使用的是最新版的cocos2dx 3.2.引擎中默认使用的是chipmunk,如果想要改使用box2d的话,需 ...

  5. sizeof(int *) 和 sizeof(int)型的大小问题

    小问题,暂时记录注意一下   printf("sizeof(int): %d\n", (int)sizeof(int));     printf("sizeof(int ...

  6. memcache锁

    锁的使用,一般情况是针对并发或者我们希望程序(crontab的job)串行处理,我们加锁的办法有很多,像文件锁,数据库锁,或者memcache锁,这里关注一下memcache锁,针对memcache锁 ...

  7. C# ORM—Entity Framework 之Code first(代码优先)(二)

    一.Entity Framework Code first(代码优先)使用过程 1.1Entity Framework 代码优先简介 不得不提Entity Framework Code First这个 ...

  8. VueJs一些资料网站链接

    https://github.com/liukaijv/laravel-vue-cmshttps://github.com/allan2coder/VUE2-SPA-Tutorialhttps://g ...

  9. iOS Block 用法 (1)-once again

    Block简介: Block的实际行为和Function很像,最大的差别是在可以存取同一个Scope的变量值.Block实体形式如下: ^(传入参数列){行为主体}; Block实体开头是“^”,接着 ...

  10. 关于-webkit-tap-highlight-color的一些事儿

    这个属性只用于iOS (iPhone和iPad).当你点击一个链接或者通过Javascript定义的可点击元素的时候,它就会出现一个半透明的灰色背景.要重设这个表现,你可以设置-webkit-tap- ...