Code Katas in Swift (#3 Stop gninnipS My sdroW!)
Code Kata ー A way to train
型 かた:kata (set sequence of positions and movements in martial arts); style (in kabuki, noh, etc.); form
Write a function that takes in a string of one or more words, and returns the same string, but with all five or more letter words reversed (Just like the name of this Kata). Strings passed in will consist of only letters and spaces. Spaces will be included only when more than one word is present.
This kata is about reversing certain words. A healthy challenge. Try it for yourself. Go on. I dare you. This is an easy one so to make it fun, time it and see how fast you can go.
First thing I always do is make a swift playground. I wanna be able to run attempts as fast as possible, especially since I don’t have even half of all swift syntax memorized.
The Solution
And blamo. This one was a little easier than I hoped. It’s basic programming.
Extra challenge mode
If you have a few years of code experience this kata is a walk in the park. Actually a little easier since it normally takes me a walk in the park to figure them out. This is supposed to be a brain stretch, so let’s try to learn something.
The map() Function in Swift
The map function is one of those things I have ignored for my entire coding life. Since I was able to exist without it, I never gave my brain the spanking it deserved and forced it to study. Time to punish that bad boy.
How does the map function work? First I googled it. Then I learned. The map function is an array method and takes one argument. That argument is a function. It will run that function for every item in the array and return a new array with those results collected together.
func properize(name: String) -> String {
return "Mr. " + name
}
let cats = ["Buttons", "Miruku", "Kochan", "Sunshine", "Patches"]
let fancyCats = cats.map(properize)
print(fancyCats) // ["Mr. Buttons", "Mr. Miruku", "Mr. Kochan", "Mr. Sunshine", "Mr. Patches"]
Even better, you can map using closures, meaning you don’t need to write a needless function.
let cats = ["Buttons", "Miruku", "Kochan", "Sunshine", "Patches"]
let fancyCats = cats.map({ (name: String) -> String in
return "Mr. " + name
})
print(fancyCats) // ["Mr. Buttons", "Mr. Miruku", "Mr. Kochan", "Mr. Sunshine", "Mr. Patches"]
Finally, that can be boiled down to this.
["Buttons", "Miruku", "Kochan", "Sunshine", "Patches"].map({"Mr. \($0)"})
// ["Mr. Buttons", "Mr. Miruku", "Mr. Kochan", "Mr. Sunshine", "Mr. Patches"]
As you can see, we omitted a lot of code. We don’t need to specify the parameter type, the return type, the parameter name, or the write the return statement.
That’s sexy. And nothing is sexier, (including you Chris Evans), than writing complex code on one line. Idk, it’s just the coolest thing in the world. So that’s the challenge. Turn the spinWords code into a one liner.
The one liner
func spinWords(in sentence: String) -> String {
String(sentence.components(separatedBy: " ").map({
$0.count > 4 ? $0.reversed() + " " : $0 + " "
}).joined().dropLast())
}
And there we go. First time I’ve used the map function. Not good code. But it tickles my jimmies so I’m leaving it. Really happy to see how easy the map function is to use and regret not learning it 10 years ago. But that’s what doing these Kata are for. To get you in a new environment and thus make you learn new, useful, things.
Check out the previous challenge if you wanna give your brain a six pack. If you want more challenges, clap so I know and I’ll post a bunch.
P.S.
I’m broke, jobless and starting from zero trying to get a job as a iOS developer. Subscribe to my blog if you wanna root on, or if you’re in the same boat and wanna pursue this goal together.
Thanks for reading. Love ya, see you later. 🐶🇯🇵 さよなら