我正在尝试转换一个旧的 api,它使用大量需要保留的点符号链接(symbolic link),即:
[1,2,3,4].newSlice(1,2).add(1) // [3]
我想在此示例 Ramda 中添加函数式组合风格,但 lodash 或其他的都可以:
const sliceAddOne = R.compose(add(1), newSlice(1,2))
sliceAddOne([1,2,3,4])// [3]
我的问题是如何在我的函数 newSlice
中同时进行链接和组合,这个函数会是什么样子?
我有一点jsBin示例。
最佳答案
编辑
我想我最初误解了你。您想要一个函数 f
,您可以将其调用为
f(...args)(someObj) === someObj.f(...args)
我会这样做
// infix
Array.prototype.newSlice = function(x,y) { return this.slice(x,y) }
// prefix
const newSlice = (x,y) => F => F.newSlice(x,y)
这是一个很好的设计,因为您可以在任何您希望具有 newSlice
功能的对象上实现 newSlice
,并且前缀函数将正常工作。这也允许您在每种对象类型(数组、字符串、其他...)上有一个独特的 newSlice
实现,因为我们正在切片的基础数据可能会有所不同 – 你得到所有this without 不必在函数体内执行愚蠢的条件 this
检查。
// newSlice :: [a] -> (Integer,Integer) -> [a]
Array.prototype.newSlice = function(x,y) {
return this.slice(x,y)
}
// newSlice :: String -> (Integer,Integer) -> String
String.prototype.newSlice = function(x,y) {
return this.substring(x,y)
}
// even on a custom object
class LottoTicket {
constructor(...nums) { this.nums = nums }
newSlice(x,y) { return this.nums.slice(x,y) }
}
// newSlice :: (Array, String) a => (Integer,Integer) -> a -> a
const newSlice = (x,y) => F => F.newSlice(x,y)
// use it in prefix position
console.log(newSlice(1,2)([1,2,3,4])) // [2]
console.log(newSlice(1,2)("abcd")) // 'b'
console.log(newSlice(1,2)(new LottoTicket(9,8,7))) // [8]
// use it infix position
console.log([1,2,3,4].newSlice(1,2)) // [2]
console.log("abcd".newSlice(1,2)) // 'b'
console.log((new LottoTicket(9,8,7)).newSlice(1,2)) // [8]
关于javascript - 一个可以在 Javascript 中组合和链接(点符号)的函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39119253/