[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 ...
随机推荐
- 自己编辑Nuget拓展包,并发布Nuget服务器,提供下载使用
1. 在NuGet官网上注册并获取API Key 到NuGet上注册一个新的账号,然后在My Account页面,获取一个API Key,如果没有则在API keys 页面创建一个就可以. 2. 下载 ...
- 迅为7寸工业触摸屏嵌入式平台可用于ARM嵌入式一体机
7寸触摸屏介绍产品名称:7寸IPS高清屏幕分辨率:1280*800触摸屏类型:电容屏(五点触摸)接线方式: FPC 可应用于嵌入式一体机.自动售货机.广告机.智能自动终端.零售终端等 ARM平台处理器 ...
- Android(java)学习笔记187:多媒体之SurfaceView
1. SurfaceView: 完成单位时间内界面的快速切换(游戏界面流畅感). 我们之前知道一般的View,只能在主线程里面显示,主线程中更新UI.但是SurfaceView可以在子线程中里 ...
- opencv实现人脸,人眼,微笑检测
1.首先实现人脸检测 import cv2 img = cv2.imread("people.jpg",1) #读入图像 #导入人脸级联分类器引擎,“.xml”文件里包含了训练出来 ...
- mysql 优化策略
from:https://dbaplus.cn/news-155-1531-1.html MySQL逻辑架构 如果能在头脑中构建一幅MySQL各组件之间如何协同工作的架构图,有助于深入理解MySQL服 ...
- hdfs深入:06、hdfs的写入过程
7.HDFS的文件写入过程 详细步骤解析: 1. client发起文件上传请求,通过RPC与NameNode建立通讯,NameNode检查目标文件是否已存在,父目录是否存在,返回是否可以上传: 2. ...
- Oracle learning note
oracle SQL select 'para1' || 'para2' as "para" must "" from table t where c.name ...
- 大项目之网上书城(五)——主页(End)
目录 大项目之网上书城(五)--主页(End) 主要改动 1.主页(终于完成啦) 完整代码 效果图 2.head.jsp的小改动 代码 3.login.jsp ###代码 效果图 4.login.js ...
- 启发式合并CodeForces - 1009F
E - Dominant Indices CodeForces - 1009F You are given a rooted undirected tree consisting of nn vert ...
- 小DEMO之manifest初体验
前言 补漏洞系列~今天来动手体验一下HTML5中的离线应用之mainifest缓存清单.实际上H5还提供了一个JavaScript接口来用于更新缓存文件的方法以及对缓存文件的操作.在Chrome中,输 ...