我想寫一個函數,比較使用另一個函數的兩個項目,然後檢查結果是否大於也提供給該函數的其他值。我可以這樣寫:有沒有辦法在不列出參數的情況下編寫這個Javascript函數?
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);
你能解釋一下到底如何你」你喜歡用你的功能嗎? – Amit
我想用所有三個參數一次調用它,因爲我可以使用頂部的原始版本。所以:compareDifference(item1,item2,10); –
「*這似乎不是很有用*」 - 它絕對沒有錯。 – Bergi