Ramda 之 concat()

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

内容简介: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));

firstsecod 兩個 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));

想將 firstsecond 兩字串加以合併,字串合併只要 + 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()》,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对 码农网 的支持!

查看所有标签

猜你喜欢:

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

Once Upon an Algorithm

Once Upon an Algorithm

Martin Erwig / MIT Press / 2017-9-8 / GBP 22.95

How Hansel and Gretel, Sherlock Holmes, the movie Groundhog Day, Harry Potter, and other familiar stories illustrate the concepts of computing. Picture a computer scientist, staring at a screen and......一起来看看 《Once Upon an Algorithm》 这本书的介绍吧!

SHA 加密
SHA 加密

SHA 加密工具

RGB CMYK 转换工具
RGB CMYK 转换工具

RGB CMYK 互转工具

HEX HSV 转换工具
HEX HSV 转换工具

HEX HSV 互换工具