[Javascript] Flattening nested arrays: a little exercise in functional refactoring
In this lesson we write an imperative function to flatten nested arrays, and then use the popular map, reduce, compose, and pipe functions to transform it into a high-level, point-free, functional implementation.
const array = [, [, ], [[, [, [], ], [, ]]]]; const concatReducer = (acc, curr) => acc.concat(curr);
const map = f => ary => ary.map(f);
const reduce = (f, init) => ary => ary.reduce(f, init);
const compose = (f, g) => x => f(g(x)); const flatten1Element = x => (Array.isArray(x) ? flatten(x) : x);
const flattenEachElement = map(flatten1Element);
const flattenOneLevel = reduce(concatReducer, []); const flatten = compose(
flattenOneLevel,
flattenEachElement
); console.log(flatten(array));
//[1,2,3,4,5,6,7,8,9]
[Javascript] Flattening nested arrays: a little exercise in functional refactoring的更多相关文章
- [Javascript] Automate the process of flattening deeply nested arrays using ES2019's flat method
Among the features introduced to the language of JavaScript in ES2019 is Array.prototype.flat. In th ...
- java基础64 JavaScript中的Arrays数组对象和prototype原型属性(网页知识)
1.Arrays数组对象的创建方式 方式一: var 变量名=new Array(); //创建一个长度为0的数组. 方式二: var 变量名=new Array(长度); //创建一个指定长度的数组 ...
- [Ramda] Get a List of Unique Values From Nested Arrays with Ramda (flatMap --> Chain)
In this lesson, we'll grab arrays of values from other arrays, resulting in a nested array. From the ...
- [Javascript] Safe Nested Object Inspection
A common problem when dealing with some kinds of data is that not every object has the same nested s ...
- [Javascript] Multiply Two Arrays over a Function in JavaScript
Just like the State ADT an Array is also an Applicative Functor. That means we can do the same trick ...
- jQuery JavaScript Library v3.2.1
/*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...
- 把Javascript 对象转换为键值对连接符字符串的方法总结
307down votefavorite 93 Do you know a fast and simple way to encode a Javascript Object into a strin ...
- JavaScript如何比较两个数组的内容是否相同
今天意外地发现JavaScript是不能用==或===操作符直接比较两个数组是否相等的. alert([]==[]); // false alert([]===[]); // false 以上两句代码 ...
- [Javascript] Create an Array concatAll method
In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we ...
随机推荐
- 批处理 reg add /?
C:\Users\Administrator>reg add /? REG ADD KeyName [/v ValueName | /ve] [/t Type] [/s Separator] [ ...
- Google浏览器开发者工具:CSSViewer(一个Css查看器)
CSSViewer的简介 CSSViewer是一款可以帮助用户快速查看当前的网页元素的CSS属性的谷歌浏览器插件,在Chrome中安装了CSSViewer插件以后,用户就可以在设计网页的时候,快速地模 ...
- POJ_1125_(dijkstra)
Stockbroker Grapevine Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 35553 Accepted: ...
- vue之package.json文件解析
1.package.json是什么? 什么是Node.js的模块(Module)?在Node.js中,模块是一个库或框架,也是一个Node.js项目.Node.js项目遵循模块化的架构,当我们创建了一 ...
- JS日期,金钱处理
一丶获取两个时间的天数 <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> ...
- Python3中assert断言
一般的用法是: assert condition 用来让程序测试这个condition,如果condition为false,那么raise一个AssertionError.逻辑上等于: if not ...
- ajax请求回数组数据,Vue页面数组没同步问题
记录bug 为什么 ajax 获取到了 vm.$data.list 页面上却没有显示出来的? 代码 //页面 <tr v-for="item in list">{{ * ...
- jquery data属性 attr vs data
html5的自定义data属性相信大家都不会陌生,有了它你可以绑定所需的数据到指定元素上.然后通过jquery设置.获取数据,简直开心的不行啊.想到设置.获取元素属性值,大家一定首先想到了jquery ...
- tomcat:页面跳转
vim index.html <script language="javascript"type="text/javascript"> window ...
- 4.model 字段
一.字段名 字段名 类型 参数 AutoField(Field) - int自增列, 必须填入参数 primary_key=True BigAutoField(AutoField) - bigint自 ...