Given a string, capitalize the letters that occupy even indexes and odd indexes separately, and return as shown below. Index 0 will be considered e...
For further actions, you may consider blocking this person and/or reporting abuse
Elm
Note: returning a tuple because returning a List String would mean it can be a list of 0 element as it can be a list of 10, 100, 1000, Infinity elements. Returning a Tuple gives the information to the user that this function will always return two elements in this case, which is suggested by the goal of this challenge. It will be then possible to extract them easily with the Tuple.first & Tuple.second functions.
This is a hallmark of proper functional programming. Elm looks intimidating than more traditional grammars but it's essentially strongly-typed function composition. I love it, and the outer function reads like english.
I won't lie, since this was my first real functional programming language, it was tough, really tough. And this is essentially due to the fact that I was not used to no parens (or not at the same position at least) and function composition mainly.
But functional programming is like a muscle. The best competitors are working hard to obtain a great physical condition. This is the same thing for people like you and me that are used to procedural or oriented-object programming. It requires a lot of practices to get out of our habits.
But when you practiced enough, you'll start to love functional programming and that is the only certainty you have. The rest is up to you. And actually, since I'm a Web Developer, and doing most of my stuff in JavaScript at work (I hope I get to include Elm one day) this has made me a better overall JavaScript developer. There is no time wasted on learning this language, trust me.
Interesting JS solution....I haven't seen ternary operators used so haphazardly before, I'm not comfortable with them still.
Scala
A good old fold and I use the
Next
type to know which side to capitalize next.SQL:
Break the string down into a table with one character per row, use the
row_number
window function to capitalize it each way, then reassemble the final product; the latter two steps can't be combined because window functions can't be nested in aggregate functions.Kinda silly solution using 2 mutually recursive functions (Haskell)
Ooh, clever! I like it!
I'm a bit late to the party, but here's my Elixir solution:
One more Python solution:
Here is some shit I wrote using shellscript
Ruby
Solution in Haskell using folding:
The
skipCase
folding function takes care of upcasing every 2. letter. Thefolder
is a makes it easier to map the function over both capitalization variations, which then can be applied to the input string.Any Python version 2.7+.
😳 😱
This is the magic of python. Short. I just love it. 💗
If you return like this
return _change_even_casing(True), _change_even_casing(False)
python will understand that it's already atuple