In case it matters this is about functional programming in JavaScript and in my examples I’ll be using Ramda.
While everybody at work has fully embraced functional programming, there’s also a lot of discussions around how to do it “right”.
These two functions will do exactly the same thing: take a list and return a new list in which all strings have been trimmed.
// data-centric style
const trimList = list => R.map(R.trim, list);
// point-free style
const trimList = R.map(R.trim);
So far so good. However with a more complex example, the difference between the two styles is striking: take a list and return a new list in which all strings are equal to a property found in an object.
var opts = {a: 'foo', b: 'bar', c: 'baz'};
var list = ['foo', 'foo', 'bar', 'foo', 'baz', 'bar'];
myFilter(opts, 'a', list); //=> ["foo", "foo", "foo"]
myFilter(opts, 'b', list); //=> ["bar", "bar"]
// data-centric style
const myFilter = (opts, key, list) => {
var predicate = R.equals(opts[key]);
return R.filter(predicate, list);
};
// point-free style
const myFilter = R.converge(
R.filter, [
R.converge(
R.compose(R.equals, R.prop), [
R.nthArg(1),
R.nthArg(0)]),
R.nthArg(2)]);
Besides readability and personal taste, are there any reliable evidences to suggest that one style is better suited than the other in some circumstances?
See Question&Answers more detail:
os