Wednesday, February 19, 2014

My path to JavaScript: Refactoring fizzbuzz to accept any kind of predicates on numbers for substitutions

I've been playing a bit more with scopes to make the generalized version of FizzBuzz using Array.map and Array.join I posted about recently accept substitution rules with any predicate on a number. I also coded a factory function that creates the substitute function.

This is the factory that creates a substitute function given a list of substitution descriptions objects which are composed of a predicate on the number and a replacement for the number:


Next you can see how this factory is used in the tests to create each substitute function:


This has been a nice practice with higher order functions, closures and free variables.
Next time, I'll move on to a new kata to practice with JavaScript objects.

No comments:

Post a Comment