Fun of Functional Programming !!!

Recently I have explored some really cool stuff of programming. I wasn’t much familiar with functional programming as I am used to imperative programming. I guess it’s okay, to be honest, and say that I have wasted my time writing a lot of noisy and garbage like code from the beginning of my introduction to programming. I didn’t know that I was using for loops unnecessarily in many places. In this short post, I will show some simple tricks of functional style and you will definitely want to learn about functional programming. I will use Python and Java for the examples here. I wonder why nobody told me about it earlier??? WHY?

Hey for loop : My first love
Let’s assume, you are coding in Java (I miss coding in Java. Life is so Pythonic nowadays). At some point, you have to generate a list of some (say 20) random numbers between 0 and 100 and print them. What would you do? 99% chance to use a for loop. Right? Something like this, right?

If we write it in Java, how would it look like? Probably like this.

Now I will put these 2 codes below. You have to run these codes in your machine. You can use online editors of Python and Java. Watch the outputs and let me know what do you think.

Just a single line of code, right? You will definitely say that we need to write less code in Python than Java. Okay. This one is for Java. Inside the main method it’s basically a one liner. But check it out 😉

Let me know what do you think about this magic 🙂


Facebook Comments

Leave a Comment.