1

我正在和 Ramda 一起玩,试图让我的头脑了解函数式 JS。以下代码尝试生成一些随机搜索过滤器对象:

var R = require('ramda');
var rWords = require('random-words');

var types = ['keyword', 'phrase', 'domain', 'person'];
var operands = ['AND', 'OR', 'NOT'];

var getRandom = (min=1, max=10) => Math.floor(Math.random() * (max - min)) + min;
var sample = (arr) => arr[getRandom(0, arr.length)];
var chooseWords = (min=1, max=getRandom()) => rWords({min, max, join: ' '});
var chooseWordsFromType = (obj) => {
    var max = obj.type === 'phrase' ? 10 : 1;
    query = chooseWords(1, max);
    return R.merge({query}, obj);
};

var makeFilter = R.compose(
    chooseWordsFromType,
    R.merge({operand: sample(operands)}),
    R.merge({type: sample(types)})
);

var initial = [];
for(var i = 0; i < getRandom(); i++){
    initial.push(makeFilter({}));
}
console.log(initial);

预期的结果应该是这样的

[
  {type: 'keyword', operand: 'AND', query:['something']},
  {type: 'domain', operand: 'AND', query:['something_else']}
]

错误在于其中的所有内容都initial具有完全相同的操作数和类型;只有查询本身不同。我想它会记住对 的调用sample,但我不知道为什么。

顺便说一句,我如何重构chooseWordsFromType以使其无需返回对象即可组合?或者我可以吗?

4

1 回答 1

2

你对这段代码有什么期望?:

var defaultOperands = {operand: sample(operands)};
var defaultTypess = {type: sample(types)};

var makeFilter = R.compose(
    chooseWordsFromType,
    R.merge({operand: defaultOperands}),
    R.merge({type: defaultTypes})
);

你的代码:

var makeFilter = R.compose(
    chooseWordsFromType,
    R.merge({operand: sample(operands)}),
    R.merge({type: sample(types)})
);

做很多相同的事情,本质上说,“调用sample,传入operands,从中创建一个对象,以'操作数'为键”,用和'type'做类似的事情types,然后创建一个makeFilter将合并每个的函数这些静态对象与您的输入并返回传递给chooseWordsFromType.


使其动态化的一种(未经测试的)方法是:

var makeFilter = R.compose(
    chooseWordsFromType,
    obj => R.merge({operand: sample(operands)}, obj),
    obj => R.merge({type: sample(types)}, obj)
);
于 2015-10-30T17:38:09.177 回答