我在一本书上看到tap函数(也叫K-Combinator)的函数签名如下:
tap :: (a -> *) -> a -> a
"This function takes an input object a and a function that performs some action on a. It runs the given function with the supplied object and then returns the object."
- 谁能帮我解释一下函数签名中的星号 (*) 是什么意思?
- 以下实现是否正确?
- 如果三种实现方式都正确,应该在什么时候使用哪一种?有什么例子吗?
实现1:
const tap = fn => a => { fn(a); return a; };
tap((it) => console.log(it))(10); //10
实现2:
const tap = a => fn => { fn(a); return a; };
tap(10)((it) => console.log(it)); //10
实现3:
const tap = (a, fn) => {fn(a); return a; };
tap(10, (it) => console.log(it)); //10
最佳答案
这看起来很像 Ramda definition .里面的*
应该是错误的。 (免责声明:我是 Ramda 的作者之一。)它可能应该是
// tap :: (a -> b) -> a -> a
类似于您的第一个实现:
const tap = fn => a => { fn(a); return a; };
或者 Ramda 的版本:
const tap = curry((fn, a) => { fn(a); return a; });
匹配该签名并且恕我直言,主要在调试上下文中很有用。我用它临时将日志记录语句引入功能管道1:
// :: Map String User
const users = fetchUsersFromSomewhere();
// :: [Comment] -> [Number]
const userRatingForComments = R.pipe(
R.pluck('username'), // [Comment] -> [String]
R.tap(console.log), // for debugging, need to include `bind` in browser envs
// ^^^^^^^^^^^^^^^^^^
R.map(R.propOf(users)), // [String] -> [User]
R.pluck('rating') // [User] -> [Number]
);
不过,这实际上不是 K 组合器。
1 此代码示例来自 old article我在 Ramda 上的。
关于javascript - Tap 的函数签名(K 组合器),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38614679/