Learn, Transfer, Repeat

Ternary operator in its generic form

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.

Factorial in Python

Succinct and elegant in calculating factorials.

However, as I progressed in my learning, I began to wander around beyond the reaches of Python. Browsers do not understand Python. They speak JavaScript.

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:

Factorial in C++

As the default use cases for C++ in web and data science were not that many, I never really found myself tearing my hair out trying to make some C++ code work. I did, however, deal with JavaScript quite a bit.

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.

Factorial in JavaScript

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:

Factorial in PHP

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.

Factorial in Swift

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.



Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store