我仍在学习 Ramda,并且经常无法将看似简单的 lamda 函数转换为无点的纯 Ramda 函数。这是一个简单的例子:
export const consoleTap = arg =>
R.compose(
R.tap,
R.curryN(2, console[arg])
);
export const errorTap = consoleTap("error");
export const logTap = consoleTap("log");
// example usage
R.map(logTap("Value"))([1, 2]) // Results in printing "Value 1" "Value 2"
这些函数运行良好,我什至为它们编写了测试。我只是觉得 consoleTap 可以写成无点的,但有些东西我没有看到或理解正确。函数可以重写吗?
最佳答案
我无法想出一个看起来不太复杂的免费版本。我认为你所拥有的已经相当不错了。
我提出的唯一建议是将事情分开:将日志记录与tap
分开。您可以单独重复使用“带前缀”的日志记录版本:
const logWith = (method, prefix) => curryN(2, console[method])(prefix);
const log = logWith('log', 'INFO: ');
const error = logWith('error', 'ERR: ');
log(10); // INFO: 10
error(10); // ERR: 10
并通过点击
:
const logTap = tap(logWith('error', 'ERR: '));
logTap(10);
// ERR: 10
// => 10
关于javascript - Ramda 中的简单无点函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57444509/