如何根据 R.nthArg 重写

How to rewrite this in terms of R.nthArg

我理解 R.nthArg returns 一个函数 returns 第 n 个参数。但是我不知道如何将它与下面的函数一起使用:

const REG = /^\s*$/;
const string = R.test(REG);
const result = (errorText) => (value) => string(value) ? errorText : null

result(translate(message));

示例用法

const testString = R.test(/^\s*$/);

// const validator = (errorText) => (value) => 
//  testString(value) ? errorText : null;  

const validator = R.curry((errorText, value) =>      
  testString(value) ? errorText : null);  

const valid = validator('my Error');  

console.log('resultOne ' + valid('ololo')); 
console.log('resultTwo ' + valid(''));
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

我认为您正在寻找 R.curry:

const string = R.test(REG);
const result = R.curry((errorText, value) => string(value) ? errorText : null)

result(translate(message));

nthArg was written in the early days of Ramda (disclaimer: I'm one of the founders of Ramda and a core team member.) It was a way to make point-free functions which were otherwise difficult to write that way. There are a few other functions like this, such as useWith and converge,但现在 Ramda 团队的大部分建议是不要迷恋 point-free 代码。仅当它使代码更具可读性时才应使用它。 nthArg 其他人很少这样做,因此应该尽量避免。

但是,如果您真的感兴趣,这里有一个 validator 的 point-free 版本,它与问题中的代码片段具有相同的行为:

const testString = R.test(/^\s*$/);

const validator = R.curryN (2) (
  R.ifElse (
    R.pipe (R.nthArg (1), testString), 
    R.nthArg (0), 
    R.always (null)
  )
)

const valid = validator ('my Error');  

console.log('resultOne ' + valid('ololo')); 
console.log('resultTwo ' + valid(' '));
<script src="//cdnjs.cloudflare.com/ajax/libs/ramda/0.27.1/ramda.min.js"></script>

显然那太可怕了。原始版本更具可读性:

const validator = (errorText) => (value) => 
  testString(value) ? errorText : null;  

如果你想 Ramda-style 柯里化,你可以只使用这个:

const validator = R.curry((errorText, value) =>      
  testString(value) ? errorText : null);  

所以,虽然我对你的导师一无所知,但我不同意她或他关于如何最好地构建它的观点。如果这只是针对 point-free 或 nthArg 的学习练习,那么这很好。不然的话,我觉得原版的还是香草版的好一点。