删除Ramda函数中的重复

时间:2017-11-16 23:52:24

标签: javascript functional-programming ramda.js

如何删除findIndexInOcompareIndexes的重复来电?

const a = [
  {name: 'charlie', value: 1},
  {name: 'bravo', value: 7},
  {name: 'delta', value: 12},
  {name: 'alpha', value: 19},
]

const propEqName = propEq('name');
const findIndexInO = findIndex(__, a);
const compareIndexes = comparator((from, to) => lt(findIndexInO(propEqName(from)), findIndexInO(propEqName(to))))
const sortOnIndex = sort(compareIndexes);

const decideOrder = (...args) => sortOnIndex(args);

decideOrder('delta', 'charlie'); // ['charlie', 'delta']
decideOrder('bravo', 'alpha'); // ['bravo', 'alpha']
decideOrder('alpha', 'charlie'); // ['charlie', 'alpha']

REPL

1 个答案:

答案 0 :(得分:2)

怎么样

const order = addIndex(reduce)((ord, a, idx) => assoc(a.name, idx, ord), {}, a)
const decideOrder = unapply(sortBy(prop(__, order)))

?因为您已经在函数中嵌入了a。作为额外的好处,如果对您而言,您也可以这样做:

decideOrder('delta', 'alpha', 'bravo') //=> ["bravo", "delta", "alpha"]