JS to CLJS # 3
I’ll regularly take a JavaScript problem and work it out in ClojureScript. This will hopefully take away some of the Magic™ of functional programming and show how to solve real problems. I promise you won’t have to learn what a monad is.
Problem
Write a function that takes a list of pairs and classifies them as valid or invalid. Pairs are valid if the age is over 35 and the signature is true.
Example input:
For example:
Solutions
Thoughts
Mostly straightforward:
- We destructure each
age
/signed?
pair. Such destructuring is a normal occurrence. - We return a lazy sequence via
map
. We could do something like “get the first three ‘Valid’” and the calculations would stop as soon as we found them.
This is clearly a tutorial type problem, but I suggest getting into the habit of using more descriptive data structures:
I’m partially anticipating that this function will be part of a larger data-flow, so the return is transformed data instead of just the valid?
data apart from the context.