[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以外其他的数据类型都被定义成了对象,也可以用创 ...
随机推荐
- 重新开始学习javase_多态(动态绑定、推迟绑定或者运行期绑定)
一,谈向上转换,或者上溯造型 什么是向上转换(上溯造型),一句话就是父类的引用指向子类的对象.或者把子类的对象当作父类来用 为什么要进行向上转换?我们先看一个例子吧! @Test public voi ...
- TalkingData游戏版本在Cocos2d-x 3.0使用
Cocos2dx在3.0的版本中改动确实不少啊,所以导致原来可以在Cocos2.x版本上的demo都不能直接用,所以不得不重要写一个新的demo 但是TalkingData的库一直都是可以用的,只是之 ...
- 利用Php ssh2扩展实现svn自动提交到测试服务器
1.安装ssh2扩展 (1)window . 下载 php extension ssh2 下载地址 http://windows.php.net/downloads/pecl/releases/ssh ...
- 用python+selenium获取XX省交通违章数据
前言: 目前在研究易信公众号,想给公众号增加一个获取个人交通违章的查询菜单,通过点击返回查询数据.以下是实施过程. 一.首先,用火狐浏览器打开XX省交管网,分析页面信息: 可以看到共有4种查询种类,我 ...
- 写给新手看的Flask+uwsgi+Nginx+Ubuntu部署教程
学习 Flask,写完一个 Flask 应用需要部署的时候,就想着折腾自己的服务器.根据搜索的教程照做,对于原理一知半解,磕磕碰碰,只要运行起来了,谢天谢地然后不再折腾了,到下一次还需要部署时,这样的 ...
- Primary Expression
Primary expressions are the building blocks of more complex expressions. They are literals, names, a ...
- UvaLive 6661 Equal Sum Sets (DFS)
Let us consider sets of positive integers less than or equal to n. Note that all elements of a set a ...
- IOS6.0 应用内直接下载程序 不需跳转AppStore -b
闲来没事看了篇文章 应用内创建应用商店环境,不跳转AppStore. 先武断的想一句:放屁.然后好奇的进去看看,原来是IOS6.0的新特性,顿感惭愧.研究下 SKStoreProductViewCon ...
- SpEL快速入门
Spring表达式语言(简称SpEL)是一种鱼JSP2 EL功能类似的变道时语言,它可以在运行时查询和操作对象图.与JSP 2的EL相比,SpEL功能更加强大,它甚至支持方法的调用和基本字符串模板. ...
- js方法在iframe父子窗口
http://developer.51cto.com/art/201009/228891.htm http://developer.51cto.com/art/201009/228891.htm ht ...