Ramda 之 uniqWith()

栏目: JavaScript · 发布时间: 6年前

内容简介:對於一般需求,VS Code 1.33.1Quokka 1.0.212

對於一般需求, uniq() 即可勝任,但若需自行提供特殊比較方式,則要使用 uniqWith() ,自行傳入 Callback。

Version

VS Code 1.33.1

Quokka 1.0.212

Ramda 0.26.1

Primitive

import { uniqBy } from 'ramda';

let data = [1, 2, 3, '1'];

console.log(uniqBy(x => String(x))(data));

data 中包含 number 1string 1 ,若 number 1string 1 均視為相同而不想重複顯示,也就是我們希望結果只顯示 [1, 2, 3]

uniqBy()

(a -> b) -> [a] -> [a]

將 array 經過 supply function 轉換,回傳 element 不重複 array

Ramda 之 uniqWith()

uniqWith()

import { uniqWith } from 'ramda';

let data = [1, 2, 3, '1'];

console.log(uniqWith((x, y) => x === String(y))(data));

我們也可以使用 uniqWith() 完成。

uniqWith()

((a, a) → Boolean) → [a] → [a]

自行提供 predicate 決定比較方式,回傳 element 不重複 array

((a, a) → Boolean) :predicate function,自行決定比較方式

[a] :data 為 array

[a] :回傳 element 不重複 array

由 Ramda source code 得知, uniqWith() 其實是兩層 for loop,第一層為第一個 argument,第二層為第二個 argument,也就是第一個 argument 代表 array 中每個 element,第二個 argument 代表所比較 array 中每個 element

Ramda 之 uniqWith()

Point-free

import { uniqWith, eqBy } from 'ramda';

let data = [100, 200, 300, '100'];

console.log(uniqWith(eqBy(String))(data));

(x, y) => x === String(y) 可進一步由 eqBy(String) 化簡成 point-free。

eqBy()

(a → b) → a → a → Boolean

若兩 value 經過 function 轉換後結果相同則回傳 true ,否則 false

Ramda 之 uniqWith()

Object

import { uniqWith } from 'ramda';

let data = [
  { title: 'FP in JavaScript', price: 100 },
  { title: 'RxJS in Action', price: 200 },
  { title: 'Speaking JavaScript', price: 300 },
  { title: 'FP in JavaScript', price: 400 },
  { title: 'Exploring ReasonML', price: 200 },
];

console.dir(uniqWith((x, y) => x.title === y.title || x.price === y.price)(data));

若 element 為 object,只要 titleprice 相等就視為相同,也就是第四筆與第一筆相同,第五筆與第二筆相同,我們希望結果只顯示不重複的前三筆。

此時就不能再使用 uniqBy() ,因為條件太特殊,只能使用 uniqWith()

Ramda 之 uniqWith()

Point-free

import { uniqWith, anyPass, eqProps } from 'ramda';

let data = [
  { title: 'FP in JavaScript', price: 100 },
  { title: 'RxJS in Action', price: 200 },
  { title: 'Speaking JavaScript', price: 300 },
  { title: 'FP in JavaScript', price: 400 },
  { title: 'Exploring ReasonML', price: 200 },
];

console.dir(uniqWith(anyPass([
  eqProps('title'),
  eqProps('price')
]))(data));

(x, y) => x.title === y.title || x.price === y.price 也可由 anyPass()eqProps() 產生,使其 point-free。

anyPass()

[(*… → Boolean)] → (*… → Boolean)

將眾多 predicate 組合成單一 predicate,只要任何 predicate 成立即可

Ramda 之 uniqWith()


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

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

Trading and Exchanges

Trading and Exchanges

Larry Harris / Oxford University Press, USA / 2002-10-24 / USD 95.00

This book is about trading, the people who trade securities and contracts, the marketplaces where they trade, and the rules that govern it. Readers will learn about investors, brokers, dealers, arbit......一起来看看 《Trading and Exchanges》 这本书的介绍吧!

HTML 压缩/解压工具
HTML 压缩/解压工具

在线压缩/解压 HTML 代码

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

在线进制转换器
在线进制转换器

各进制数互转换器