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, persistence, that takes in a positive parameter num and returns its multiplicative persistence, which is the number of times you must multiply the digits in num until you reach a single digit.
persistence-recursive: Grows the stack and potential stack overflow, but doesn’t need to maintain state.
persistence-iterative: Doesn’t grow the stack and no stack overflow, but needs to update state. Additionally, cljs isn’t tail call optimized (TCO), so we need to use loop/recur.