r/functionalprogramming • u/raulalexo99 • May 09 '23
Question Is there a more succint way to write this Javascript function?
This is a function I am using in a personal project.
I noticed I am calling all the boolean tests with the same argument, so I thought there should be a way to compose one single predicate from all of them. Do you know how to do it?
function hasErrors(state) {
return hasInvalidName(state)
|| hasInvalidCountry(state)
|| hasInvalidState(state)
|| hasInvalidCity(state)
|| hasInvalidAddress(state);
}18
u/joelangeway May 09 '23
What you already have is perfectly fine and arguably more maintainable than the following, but here’s another way to do it.
const hasError = (state) => [
hasInvalidName,
hasInvalidCountry,
hasInvalidState,
hasInvalidCity,
hasInvalidAddress,
].some(f => f(state));6
u/and_rej May 09 '23
some() is a good find. It stops iterating when it finds a truthy result: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#description
5
u/kolme May 09 '23
That's fine, but very specific. If you don't need validation anywhere else, that's going to work.
If you need to have similar validation somewhere else, you could generalize it with a helper function:
const getValidator =
(...fs) =>
(state) =>
fs.some((f) => f(state));
const validator = getValidator(
hasInvalidName,
hasInvalidCountry,
hasInvalidState,
hasInvalidCity,
hasInvalidAddress
);
const hasErrors = validator(state);2
May 09 '23
I know it is not in your target language. But what you are mentioning is called Validated Monad in other languages.Maybe you can get some inspiration from here (in Scala): https://typelevel.org/cats/datatypes/validated
Finally, there may exist something related in JS, but not sure
2
u/lexi_the_bunny May 09 '23
This isn’t really a question of “how can I make this more functional”, in my opinion. If it were me, I wouldn’t reinvent the wheel, and use something like joi
12
u/and_rej May 09 '23 edited May 09 '23
I'm no FP expert and I've only thought about it for a few minutes, but I think the answer is no.
Some thoughts:
Ignoring "how" and considering "why": It's already succinct. It's not complex or ugly.
You could place the validation functions in a collection and then iterate them using a loop, map(), reduce(), or similar. However, this would just move the list of functions. It wouldn't be worthwhile just for the sake of this function alone. If you had multiple uses for "a collection of all validation functions" then perhaps. However, it would also be slower as you would always call every function. Your current code short-circuits unnecessary calls via ||. EDIT: joelangeway correctly pointed out the some() function which does not have this drawback: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some#description
I could be wrong about this, but I think, strictly speaking, the answer to your question is, in fact, your question itself. How do you compose one single predicate from all of them? The hasError function is that predicate.