Learn, Transfer, Repeat
When I started out on my journey as a coder, I was obsessed with finding out which is THE language to learn and master. My work and personal interests revolved around data and the analysis of data — and still do till today — hence I naturally selected Python, the mainstay of data manipulation, machine learning and artificial intelligence. Think numpy, pandas, matplotlib, scikit-learn, tensorflow, just to name a few.
Python’s beauty is in its readability. It is as readable as pseudocode, so why bother writing code that only some humans can understand when we could express our thoughts in a way that even computers could fathom.
Consider the following code, which includes an anonymous function, a ternary operator and recursion expressed in one line.
Succinct and elegant in calculating factorials.
When I first looked at the language, it felt oddly familiar. You see, I dabbled in C when I was in secondary school. The syntax was very similar, curly brackets and all. Given the similarities, I started to work on some challenges in C++, C’s object-oriented cousin.
In C++, I learned that you could express a ternary operator in the following way, declaring types along the way:
One fine day, I was presented with a familiar logical refrain — return something if some condition is true else return something else. So out of curiosity, I plugged my C++ ternary operator into my JS code to see if the interpreter throws a fit.
In the example above, I replace the lambda function with an arrow function, ensure strict equality (‘===’) test to ensure the code does not end up with some peculiar JS-only behaviour, and then plug in the rest of the C++ ternary operator.
And it worked!
With the cat out of the bag, I tried this in PHP — which is still a must-learn language given its ubiquity in web development — with the C++-like syntax:
Wow, it worked again!
Apart from PHP, I tried to smash the same code into Swift — Apple’s preferred language for the development of iOS apps.
In the attempt below, type declaration is back in vogue with syntax very similar to (optional) type hinting in Python, in addition to the interesting underscore (‘_’) notation when I define the input to allow me to pass an argument into the function without having to know the argument’s name.
Some make use of compulsory curly brackets in declaring functions or creating loops or implementing if-else conditions, some make it optional for one liners, while others don’t use them at all. Some make semi-colons mandatory, others don’t. Some require compulsory type declarations, others don’t. Some have easy anonymous functions, others don’t. Many allow ternary operators ala C++-style, while Python insists on a more plain English expression. And the list goes on.
Learning truly never stops.