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
, but mostly the advice from the Ramda team now is to not make a fetish of point-free code. It should only be used when it makes the code more readable. nthArg
and the rest rarely do so, and so should mostly be avoided.
However, if you’re really interested, here’s one point-free version of validator
that has the same behavior as the snippet in the question:
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>
Clearly that’s horrible. The original version is much more readable:
const validator = (errorText) => (value) =>
testString(value) ? errorText : null;
and if you want Ramda-style currying, you can just use this one:
const validator = R.curry((errorText, value) =>
testString(value) ? errorText : null);
So, while I know nothing about your mentor, I’m going to disagree with her or him about how to best structure this. If this is only a learning exercise for point-free or for nthArg
specifically, then this is fine. Otherwise, I think the original vanilla version is quite a bit better.
CLICK HERE to find out more related problems solutions.