我正在尝试编写一个使用另一个函数比较两个项目的函数,然后检查结果是否大于同时提供给函数的其他值。我可以这样写:
const compareDifference = function(t1, t2, threshold) {
return getDifference(t1, t2) > threshold;
};
......但这似乎不太实用。我找到的经典组合的每个例子都假设我在调用函数之前就知道要比较的值,在这种情况下我可以像这样在函数上编写它:
const greaterThan = (x, y) => x > y;
const greaterThan10 = _.partial(greaterThan, _, 10);
const compareDifference = _.compose(greaterThan10, getDifference);
因为我对函数式编程比较陌生,所以我觉得我在这里缺少一些简单或基本的东西。有没有办法编写函数,以便它包含要传递给greaterThan的参数,而我不必明确提及它?理想情况下它会是这样的:
const compareDifference = _.compose(_.partial(greaterThan, _), getDifference);
答案 0 :(得分:2)
我认为LUH3417的答案对初学者来说很棒。它涉及一些基础知识,但我认为还有其他一些信息的空间
首先,如果您想在原始问题中使用完全相同的API,则可以将其分解为这样的部分。
const comp = f=> g=> x=> f (g (x))
const comp2 = comp (comp) (comp)
const flip = f=> x=> y=> f (y) (x)
const sub = x=> y=> y - x
const abs = x=> Math.abs
const diff = comp2 (Math.abs) (sub)
const gt = x=> y=> y > x
// your function ...
// compose greaterThan with difference
// compareDifference :: Number -> Number -> Number -> bool
const compareDifference = comp2 (flip(gt)) (diff)
console.log(compareDifference (3) (1) (10))
// = gt (10) (abs (sub (1) (3)))
// = Math.abs(1 - 3) > 10
// => false
console.log(compareDifference (5) (17) (10))
// = gt (10) (abs (sub (5) (17)))
// = Math.abs(17 - 5) > 10
// => true

但是,你怀疑你的原始代码并没有感觉到功能是正确的。我在这里给你的代码有效,但它仍然感觉...... 关闭,对吧?我觉得如果你把它变成一个higher-order function,就是一个接受一个函数作为参数的函数(和/或返回一个函数),这会大大改善你的功能。
黄砖路
然后我们可以创建一个名为testDifference
的泛型函数,它将阈值函数t
作为输入,2
数字作为阈值计算的基础
// testDifference :: (Number -> bool) -> Number -> Number -> bool
const testDifference = t=> comp2 (t) (diff)
看看实施情况,这是有道理的。要测试差异,我们需要测试(某个函数),我们需要两个计算差异的数字。
以下是使用它的示例
testDifference (gt(10)) (1) (3)
// = gt (10) (abs (sub (1) (3)))
// = Math.abs(1 - 3) > 10
// = Math.abs(-2) > 10
// = 2 > 10
// => false
这是一个很大的改进,因为>
(或gt
)不再在您的函数中进行硬编码。这使得它更加通用。请注意,我们可以轻松地将其与lt
const lt = x=> y=> y < x
testDifference (lt(4)) (6) (5)
// = lt (4) (abs (sub (6) (5)))
// = Math.abs(5 - 6) < 4
// = Math.abs(-1) < 4
// = 1 < 4
// => true
或者让我们定义一个非常严格的阈值,强制数字与<{1}}的完全不同
1
因为const eq = x=> y=> y === x
const mustBeOne = eq(1)
testDifference (mustBeOne) (6) (5)
// = eq (1) (abs (sub (6) (5)))
// = Math.abs(5 - 6) === 1
// = Math.abs(-1) === 1
// = 1 === 1
// => true
testDifference (mustBeOne) (5) (8)
// = eq (1) (abs (sub (5) (8)))
// = Math.abs(8 - 5) === 1
// = Math.abs(3) === 1
// = 3 === 1
// => false
是curry,你也可以将它用作部分应用的函数
testDifference
现在一起
这是一个已实施// return true if two numbers are almost the same
let almostSame = testDifference (lt(0.01))
almostSame (5.04) (5.06)
// = lt (0.01) (abs (sub (5.04) (5.06)))
// = Math.abs(5.06 - 5.04) < 0.01
// = Math.abs(0.02) < 0.01
// = 0.02 < 0.01
// => false
almostSame (3.141) (3.14)
// = lt (0.01) (abs (sub (3.141) (3.14)))
// = Math.abs(3.14 - 3.141) < 0.01
// = Math.abs(-0.001) < 0.01
// = 0.001 < 0.01
// => true
的代码段,您可以在浏览器中运行以查看其是否正常工作
testDifference
&#13;
答案 1 :(得分:0)
如果我用这个咆哮错误的树,那么告诉我,我会编辑,但如果我想做这样的“功能更强大”,我会做以下事情:
let greaterThan = _.curry((x, y) => y > x); // notice the args are flipped
let difference = _.curry((x, y) => Math.abs(x - y));
let greaterThan5 = greaterThan(5); // this naming is why we ordered the args backwards
let differenceBetweenGreaterThan5 = _.compose(greaterThan5, difference);
differenceBetweenGreaterThan5(10, 34); // true
differenceBetweenGreaterThan5(10, 6); // false
然后我们可以像这样重写原始函数:
let compareDiff = (threshold, x, y) => {
return _.compose(greaterThan(threshold), difference)(x)(y);
};
虽然我可能只使用differenceBetweenGreaterThan5
我也为漫长的变量名称道歉,但我希望它很清楚我的命名。还有一些需要注意的事项:我重新排序了greaterThan
的参数,以使部分应用程序的命名更加明智,并避免使用_
占位符。虽然我讨论difference
,并认为它对于通用的东西很好,但对于这个例子来说并不是绝对必要的。
至于我认为你缺少什么,在这种情况下的功能方法(根据我对“功能方法”意味着什么的理解)是我们打破了复杂的情况,即获得两个数字之间的差异,看看第三个数字是否下降在该范围内,将其分解为原子成分,并将其构造为greaterThan
和difference
等原子元素的组合。
它的破坏和重建是困难的:干净利落地要求重新安排论据的灵活性,便利性和清晰度(甚至相对于上面段落中拼写的英文版本,因为我先给出'第三个'数字)。在我看来,论证和片段重新排序部分是你所缺少的。