Say I have the following input (to be used with
Node, but the problem is more general, and not Node related):
- Absolute path to a directory, call it
dirPathAbs- An array of basenames (call it
namesSeq) of some JS files that exist inside that folder
For example:
I might have namesSeq = ['a', 'b', 'c'] which corresponds to some a.js, b.js, c.js inside dirPathAbs.
Problem:
How do I resolve the paths of the files, in a purely functional way, but also in a covariant way? (i.e. without having to ever talk about a variable that iterates the array. Covariant might not be the word for this, sorry).
What I don't want:
namesSeq.map(base => path.join(dirPathAbs, `${base}.js`));
Nor
namesSeq.map(base => require.resolve(path.join(dirPathAbs, base)));
Nor
namesSeq.map(base => path.resolve.bind(dirPathAbs)(base));
Nor
const cb = base => path.resolve.bind(dirPathAbs)(base);
namesSeq.map(cb);
I was expecting this one to work
namesSeq.map(path.resolve.bind(dirPathAbs))
but it doesn't. I think path.resolve.bind(dirPathAbs) receives as input namesSeq, which is the 3rd param of the callback provided to Array.prototype.map, because the error I see is
TypeError: Path must be a string. Received [ 'a', 'b', 'c' ]
This is just one exercise of this kind that frustrates me, but a whole category of analogous exercises gave me headaches since learning JS. I am still missing something when it comes to how this binds, and how all these Function.prototype, Array.prototype & friends should be used.