cond
Using cond in functional pipelines (5 min. read)
We'll cover the following...
We'll cover the following...
Sometimes we have too many conditions, making switch statements a great choice.
We could mimic this with pipe and when, but coding for a default case is tough.
Or we can use cond, which is built into languages like Lisp. It takes an array of if/then statements, which are arrays themselves.
The first function is the predicate, and the second function is what to run if the predicate returns true.
Here’s an example
It runs through each array. If the array’s first function returns true, the array’s second function is called and the logic’s cut off.
But how do we support the default case?
Have a function that always returns true at the very end. If nothing else runs, it will succeed and be called!