JS to CLJS #1
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 accepts a string and returns the same string with alternate casing.
This problem came up in a JavaScript slack group. Several JS solutions were given, with most differences being insignificant.
Solutions
Comparative Thoughts
- Both versions map over each character without counting the number of characters.
- The cljs version doesn’t use the index to determine which function to apply.
- The cljs version will be simpler to extend, e.g. replace every third char with a space. (In the cljs case, we just add the function to the end of the
cycle
d vector. In js, we’d need to add another branch to ourindex
check.) - The cljs version gives us functions as first class, i.e. we can use the string functions as arguments. In js, we have
toLowerCase
andtoUpperCase
that can only be applied to string instances. If we want to pass them as arguments, we have to wrap them as functions or perform some other workaround.