[Javascript] Intro to Recursion - Refactoring to a Pure Function
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的更多相关文章
- [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 - Detecting an Infinite Loop
When using recursion, you must be mindful of the dreaded infinite loop. Using the recursive function ...
- Function Programming - 纯函数(Pure Function)
纯函数的定义,非常重要!! Pure function 意指相同的输入,永远会得到相同的输出,而且没有任何显著的副作用. 老样子,我们还是从最简单的栗子开始: var minimum = 21; va ...
- 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 ...
- 动手实现 Redux(三):纯函数(Pure Function)简介
我们接下来会继续优化我们的 createStore 的模式,让它使我们的应用程序获得更好的性能. 但在开始之前,我们先用一节的课程来介绍一下一个函数式编程里面非常重要的概念 —— 纯函数(Pure F ...
- [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] 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——关于字符串的replace函数中的function函数的参数
<!DOCTYPE> <html> <head> </head> <body> <script type="text/jav ...
- JavaScript(JS)之Javascript对象BOM,History,Location,Function...(二)
https://www.cnblogs.com/haiyan123/p/7594046.html 在JavaScript中除了null和undefined以外其他的数据类型都被定义成了对象,也可以用创 ...
随机推荐
- Android onConfigurationChanged(Configuration cfg) 无法触发问题
1.android:configChanges="orientation|keyboardHidden"的使用 当在activity加上android:configChange ...
- UIImageView添加边框和阴影
- (void)viewDidLoad { [super viewDidLoad]; //添加显示 UIImage *image = [UIImage imageNamed:@"0_wang ...
- Active控件有关问题
ActiveX 控件是允许网站提供视频等内容的网站. 当你浏览 Web 时,它们允许你使用工具栏.股票代号.视频和其它内容. 但是,这些程序有时可能出现问题,或者向你提供不需要的内容. 在某些情况下, ...
- CentOS 6.5下安装MySql 5.7
不管您按下面的方法安装成功否,请留个言,把您遇到的问题写上共勉! 包下载http://url.cn/WrNg5S 环境: 1).软硬件:E6420双核CPU,8G内存,1T硬盘 2).虚拟机下 Cen ...
- ubuntu 更新 php5.5.9 到 php 5.6
add-apt-repository ppa:ondrej/php5-5.6 apt-get update apt-get install php5 为了使用 add-apt-repsitory 需要 ...
- Spring4.0学习笔记(9) —— Spring泛型依赖注入
1.定义基础仓库 package com.spring.generic.di; public class BaseRepository<T> { } 2.定义基础服务层 package c ...
- WebAPI接口测试之matthewcv.WebApiTestClient
WebAPI接口测试之matthewcv.WebApiTestClient matthewcv.WebApiTestClient 1.安装matthewcv.WebApiTestClient包 打开v ...
- Day5 双层装饰器、字符串格式化、生成器、迭代器、递归
双层装饰器实现用户登录和权限认证 #!/usr/bin/env python# -*- coding: utf-8 -*-# Author: WangHuafeng USER_INFO = {} de ...
- 使用putty登陆cygwin出现server unexpectedly ...error.解决方案
将cygwin安装目录下/etc/passwd中的passwd文件中user:unused:32707:10513:U-CYOU-INC\user,S-1-5-21-2645613570-259884 ...
- About Technology Research
做了大量的了解(research).尝试(experimental).探索(exploration).摸索(groping); 解决方案的选择时,要做前期的调研和Demo尝试. 对各方面的技术都做了一 ...