So you feel as programmer because you can handle one programming language. Well, the bad news are the fact that you are not a programmer and even you don’t know too much about programming. At least you should know the three main programming paradigms, which comes from the two main branches called functional and imperative, so you should know at least functional, procedural and object oriented programming. The reason is quite simple, all languages are becoming hybrid languages supporting those three programming paradigms, if you do not understand their basic concepts, you are lost and messing the code.
As example, on Python I have a list of integers and I want the power of two of each element. Someone with the basic approach of imperative programming and procedural programming will implement something like the code that follows.
nums = xrange(1, 11) resl = list() for n in nums: resl.append(n ** 2) print(resl)
If you know a little of functional programming and you know that Python is a multi-paradigm programming language that allows you to use some functional features on it, you know that the code above have bad implementation and should be simplified just to three lines of code, because Python supports lambda expressions and higher order functions, as code the code that follows.
nums = xrange(1, 11) resl = map(lambda x: x ** 2, nums) print(resl)
Among other stuff, like reducing cyclomatic complexity and making more elegant code, you are not conscious about those changes that are present in other programming languages you will fall in very ugly mistakes about programming. Most programming languages are currently including support for various functional features like lambda expressions, higher order functions, closures, continuations and many other features considered functional. I do not need to name each language that has added support for them, but as examples we have Java, C Sharp, C++, JavaScript, and many others.
We have reached a point where the functional paradigm cannot be ignored any more. You must understand many concepts that comes from functional programming to understand concepts related to technologies that are currently widely used, like MapReduce, which has its conceptual basis on functional operations too, by mixing two higher order functions called map and fold — which is called reduce() in Python and (apply) in Lisp.
So, you should start learning about functional programming, and learning well, from its basis, concepts and implementations. Also, you should know that most languages used in algorithm research are functional languages. So, the next time that I see a for statement placed where should be a statement where you can use a higher-order function, I will consider your code not good as you think.
So you prefer
rather than
?
Is the later still considered functional programming?
Well, using map() as an example function, is not bad. The second Python construct is not bad too, it is similar to the Haskell construct bellow:
So there is no problem using that expression, but seems to be hard to understand in certain cases. That kind of expression are derived from Miranda I think, and still are considered functional.
The nice think about list comprehensions is that they match the mathematical notation for defining sets:
Set-builder notation, Parallels in programming languages.
Yes, that is because they are cool, among other stuff with strong mathematical basis in many functional programming languages :)