[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以外其他的数据类型都被定义成了对象,也可以用创 ...
随机推荐
- 深入理解offsetTop与offsetLeft
做为走上前端不归路的我,以前只是认为offsetTop是元素的左边框至包含元素offsetParent的左内边框之间的像素距离,同理offsetRight是相对于上内边框.那么问题来了,包含元素off ...
- uva 1378 A Funny Stone Game (博弈-SG)
题目链接:http://vjudge.net/problem/viewProblem.action?id=41555 把第i堆的每个石子看出一堆个数为n-i的石子,转换为组合游戏 #include & ...
- Selenium 下载URL
http://mvnrepository.com/artifact/org.seleniumhq.selenium
- groovy构建和解析xml文件
原文链接:http://www.ibm.com/developerworks/cn/java/j-pg05199/ 代码示例: 构建xml文件: def static createXmlFile(){ ...
- thinkphp框架之模型(数据库查询)
1. 模型定义 文件名称必须是 表名+Model.class.php 例如:UserModel.class.php namespace Home\Model; //该模型类的命名空间 use Thin ...
- JQuery 左右拖动插件
js文件:http://hokaccha.github.io/js-flipsnap/js/flipsnap.js 官网: http://hokaccha.github.io/js-flipsnap/
- 在PyQt4中使用matplotlib
matplotlib作为Python中著名的数据可视化工具,其官网也提供了在PyQt4中使用的源码,这里举一个应用实例,以备不时之需. 1) 利用Qt Designer创建GUI界面 Demo的GUI ...
- a标签无disabled属性
<a class="button">确认</a> 我们经常会用a标签来设置按钮样式,如果点击它跳转页面,那么没有任何问题. 如果绑定了ajax事件,即点击后 ...
- DOM&BOM笔记
day01正课:1. DOM概述2. ***DOM树3. *查找 1. DOM概述: DHTML:动态网页技术的统称 DHTML=HTML+CSS+JS 鄙视题: HTML XHTML DHTML X ...
- 一个测ip和端口是否联通的工具类
public class TestIp { public static void main(String[] args) { Socket connect = new Socket(); try { ...