BigBrainKotlin#6: The Morse Code 🤫

I have a secret code for you to crack detective! 🕵️‍♀️

originally published on

Don’t be a Java-ish dev in Kotlin-ish world, improve your knowledge about Koltin Standard Library and write better Kotlin code. ✌🏻 If your Java dev migrating to Kotlin this will help you learn alot!

This quiz was suggested by Rohan Singh on Twitter, sorry for the delay mate!

Question: You have received a secret message! unfortunately, you can’t just read it, it is encoded in Morse code. Your task is to implement a function that would take the morse code as input and return a human-readable string. 3 spaces are used to separate words and

Extra spaces before or after the code have no meaning and should be ignored.

Message : -. — — — ..- …. .- …- . .- -… .. — . -… .-. .- .. -. ..-. — — .-. -.- — — — .-.. .. -.

val morseCode = "-.-- --- ..-   .... .- ...- .   .-   -... .. --.   -... .-. .- .. -.   ..-. --- .-.   -.- --- - .-.. .. -."  // write a function that converts this MorseCode to English fun decodeMorse(morseCode:String):String{   
// .. do stuff

Given Morse Code Decoder

Try it put yourself :

Solution 1:

Same old imperative way. 😪

Btw there is nothing wrong in this way, just the loops make it very expressive, there is no issue of mutations and accidental updates in it. 🙌🏻

Checkout why accumulator pattern in imperative style is bad.💡

Solution 2:

Imperative equivalent code in functional style | Stdlib function

The performance hit of there is due to joinToString operation one at the outer loop and multiple times into the inner loop, not good! dood (dude)...☹️

learn joinToString and it’s advanced use cases 💡

Solution 3:

What if we call it once? , if we eliminate the nesting, i.e flatten our words to char?

👨🏻‍💻 Let’s flatten our List using FlatMap


Solution 4:

Or Change the way you are solving this problem 💁‍♀️

Don’t get attached to a solution!


The aim of these articles is not hated on Java, but to help people learn various ways in which they can write the same logic better and more Kotlin standard library-focused way.

Hope you find it informative and if you have any feedback or post request or want to subscribe to my mailing list forms are below.

Until next time. Happy Hacking! 👩‍💻

Solve many more 👇🏻

Coding enthusiast! love Android #kotlinAlltheWay, want to explore all opportunity around it! CodingMantra: #cleanCoder #TDD #SOLID #designpatterns