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的更多相关文章

  1. [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 ...

  2. java基础64 JavaScript中的Arrays数组对象和prototype原型属性(网页知识)

    1.Arrays数组对象的创建方式 方式一: var 变量名=new Array(); //创建一个长度为0的数组. 方式二: var 变量名=new Array(长度); //创建一个指定长度的数组 ...

  3. [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 ...

  4. [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 ...

  5. [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 ...

  6. jQuery JavaScript Library v3.2.1

    /*! * jQuery JavaScript Library v3.2.1 * https://jquery.com/ * * Includes Sizzle.js * https://sizzle ...

  7. 把Javascript 对象转换为键值对连接符字符串的方法总结

    307down votefavorite 93 Do you know a fast and simple way to encode a Javascript Object into a strin ...

  8. JavaScript如何比较两个数组的内容是否相同

    今天意外地发现JavaScript是不能用==或===操作符直接比较两个数组是否相等的. alert([]==[]); // false alert([]===[]); // false 以上两句代码 ...

  9. [Javascript] Create an Array concatAll method

    In addition to flat Arrays, programmers must often deal with nested Arrays. For example let's say we ...

随机推荐

  1. vue2.0版本指令v-if与v-show的区别

    v-if: 判断是否加载,可以减轻服务器的压力,在需要时加载. v-show:调整css dispaly属性,可以使客户端操作更加流畅. v-if示例: <!DOCTYPE html> & ...

  2. Which dispatch method would be used in Swift?-Existential Container

    In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...

  3. Beta冲刺提交-星期三

    - 这个作业属于哪个课程 <https://edu.cnblogs.com/campus/xnsy/SoftwareEngineeringClass1> 这个作业要求在哪里 <htt ...

  4. 自动交互脚本之expect使用记录

    之前一直没怎么用这个命令,意外用了一下,还不错,那这个是干嘛的呢 我们或多或少会远程登录其他服务器,需要执行某项任务,通常需要手动接入,输入密码啊,等等 那我们如何有效的自动执行呢,expect可以解 ...

  5. sosoapi的安装

    sosoapi简介及其用户手册:http://www.sosoapi.com/pass/help/manual.htm 该随笔的大概分为: 1.sosoapi的基础安装  2.sosoapi使用域名访 ...

  6. php生成订单号-当天从1开始自增

    /** * 生成订单号 * -当天从1开始自增 * -订单号模样:20190604000001 * @param Client $redis * @param $key * @param $back: ...

  7. Android网站

    http://blog.csdn.net/airsaid/article/details/52902299 android调用传感器的代码 http://blog.csdn.net/huangbiao ...

  8. 关闭警告&关闭eslint

    1.main.js中添加 vue.config.productiontip = false  这样即可去除警告!  第一个除了那个配置意外,还需要将 NODE_ENV 设置为 production 参 ...

  9. python3+beautifulSoup4.6抓取某网站小说(二)基础功能设计

    本章学习内容:1.网页编码还原读取2.功能设计 stuep1:网页编码还原读取 本次抓取对象: http://www.cuiweijuxs.com/jingpinxiaoshuo/ 按照第一篇的代码来 ...

  10. C#语言中循环分类总结

    C#语言中,循环主要分为4种,分别是:while循环.do while循环.for循环.foeach循环.下面我将分类对循环语句总结. 1.while循环: 如果循环条件为真,则执行循环体:执行完循环 ...