Ramda 之 concat()

栏目: 编程语言 · 发布时间: 7年前

内容简介:Ramda 的VS Code 1.33.1Quokka 1.0.209

Ramda 的 concat() 有 2 種功能:合併 Array 與 String。

Version

VS Code 1.33.1

Quokka 1.0.209

Ramda 0.26.1

Array

const first = [
  { title: 'FP in JavaScript', price: 100 }
];

const second = [
  { title: 'RxJS in Action', price: 200},
  { title: 'Speaking JavaScript', price: 300 }
];

// concat :: [a] → [a] → [a]
let concat = (arr1, arr2) => arr1.concat(arr2);

console.dir(concat(first, second));

first 與 secod 兩個 array 結構完全相同,想將其合併成單一 array。

其實 ECMAScript 內建的 Array.prototype.concat() 就已經提供此功能,可直接使用。

Ramda 之 concat()

import { concat } from 'ramda';

const first = [
  { title: 'FP in JavaScript', price: 100 }
];

const second = [
  { title: 'RxJS in Action', price: 200},
  { title: 'Speaking JavaScript', price: 300 }
];

console.dir(concat(first, second));

事實上 Ramda 已經內建 concat() ,可直接使用。

concat()

[a] → [a] → [a]

將兩個相同結構的 array 合併成單一 array

其實 Ramda 的 concat() 就是以 Array.prototype.concat() 實現,只是以 FP 形式,且提供了 currying 可以做 function composition。

Ramda 之 concat()

String

const first = 'FP in JavaScript,';
const second = ' RxJS in Action';

let concat = (str1, str2) => str1 + str2;

console.log(concat(first, second));

想將 first 與 second 兩字串加以合併,字串合併只要 + operator 即可。

Ramda 之 concat()

import { concat } from 'ramda';

const first = 'FP in JavaScript,';
const second = ' RxJS in Action';

console.log(concat(first, second));

Ramda 的 concat() 亦能對字串加以合併。

concat()

String -> String -> String

將兩個 string 合併成單一 string

其實 Ramda 的 concat() 就是以 + 實現,只是以 FP 形式,且提供了 currying 可以做 function composition。

Ramda 之 concat()

Conclusion

  • Ramda 的 concat() 其實很簡單,且 array 與 string 都能用,也展現了 function 與 data 分離後,不再只是單一型別的 method,可套用在不同型別上

Preference

Ramda , concat()


以上所述就是小编给大家介绍的《Ramda 之 concat()》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们。

Perl入门经典

Perl入门经典

[美]Curtis "Ovid" Poe / 朱允刚、韩雷、叶斌 / 清华大学出版社 / 2013-9-20 / 78.00

作为最有影响力的编程语言之一,Perl被广泛用在Web开发、数据处理和系统管理中。无论是Perl新手,还是想要加强自己实战技能的Perl程序员,《Perl入门经典》都提供了处理日常情况所需的各种技术。凭借十多年的Perl经验,作者Curtis“Ovid”Poe一开始先简单回顾了Perl的基础知识,然后以此为出发点,举例说明了Perl在工作场所中的各种真实用法。此外,书中还包含了一些动手练习、宝贵建......一起来看看 《Perl入门经典》 这本书的介绍吧!

JS 压缩/解压工具
JS 压缩/解压工具

在线压缩/解压 JS 代码

JSON 在线解析
JSON 在线解析

在线 JSON 格式化工具

图片转BASE64编码
图片转BASE64编码

在线图片转Base64编码工具