我认为 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具有精确差异
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
因为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 的代码sn-p,您可以在浏览器中运行它来查看它的工作情况
// comp :: (b -> c) -> (a -> b) -> (a -> c)
const comp = f=> g=> x=> f (g (x))
// comp2 :: (c -> d) -> (a -> b -> c) -> (a -> b -> d)
const comp2 = comp (comp) (comp)
// sub :: Number -> Number -> Number
const sub = x=> y=> y - x
// abs :: Number -> Number
const abs = x=> Math.abs
// diff :: Number -> Number -> Number
const diff = comp2 (Math.abs) (sub)
// gt :: Number -> Number -> bool
const gt = x=> y=> y > x
// lt :: Number -> Number -> bool
const lt = x=> y=> y < x
// eq :: a -> a -> bool
const eq = x=> y=> y === x
// (Number -> bool) -> Number -> Number -> bool
const testDifference = f=> comp2 (f) (diff)
console.log('testDifference gt', testDifference (gt(10)) (1) (3))
console.log('testDifference lt', testDifference (lt(4)) (6) (5))
console.log('testDifference eq', testDifference (eq(1)) (6) (5))
// almostSame :: Number -> Number -> bool
let almostSame = testDifference (lt(0.01))
console.log('almostSame', almostSame (5.04) (5.06))
console.log('almostSame', almostSame (3.141) (3.14))