List and dictionaries in Python:
Iteration over a list in Python:
Iteration over a dictionary in Python:
Furthermore, Python provides a shorthand for iterating over collections and processing them, which is not merely syntactic sugar, but a simple form of functional programming.
For instance, a shorthand for:
Iterating over a list and doubling the value of the elements:
Can also be rewritten, using a list comprehension:
or using the map function to alter the elements of V:
In this example, the expression :
They are typically used as follows:
a slight variant (with a touch of syntactic sugar a la C++ or Python) is:
Where the temporary variable i is defined locally in the for loop (which is simpler and safer, since its scope gets limited to the for loop which makes use of it).
These two expressions are simple to use and familiar to the practitioner of the dark arts of C, C++, and Java.
This is more concise, more readable, more expressive and the index variable is implicit.
Please note that the array iteration is:
- A for-of construct in the case of an Array.
- A for-in construct in the case of an Object
A for-in construct applied to an Array would get the (successive values of the ) index i of V[i] and not the value of V[i].
The syntax is essentially the same for an associative array or map:
Reformulation of the problem
- The formulation is not functional (expressions cannot be chained).
- This paradigm offers not list comprehensions, unlike Python.
- One possible solution: Using the RxJs Library.
The RxJS library can be downloaded from its open-source library on GitHub (https://github.com/ReactiveX/rxjs), or installed by the command-line utility which comes with the Node.js interpreter, as follows:
npm installation command for RxJs:
In order to use the RxJs library, one has to refer to it in the code as follows:
Here is a very (very) succinct introduction to RxJS.
Once the sequence is transformed into an observable, it can be processed in a standard way, irrespectively of the underlying data structure or data source.
In other words, one we have an observable, one can deal with it very much like the way Python deals with lists (list comprehension, map, filter, reduce, functional programming, etc.)
Let’s first consider a trivial case: an Array.
Here is how to transform an Array into an observable and how to process it, using RxJs:
In this snippet, the expression:
Is an anonymous function (or “callback” ), the equivalent of a lambda function in Python.
RxJs Observables can be constructed from:
- Any sequence of data.
The beauty of it is:
regardless of the underlying source of data or data structure, observables are always handled the same way.
For instance, if one wants to apply a function f(x) to all the elements x of an observable Obs, the syntax is always (irrespectively of the underlying structure from which the observable has been derived):
Which is analogous to the Python expression:
or with an altenative formatting:
- Standard, uniform,
- With an intuitive syntax which is almost identical to the one used in Python.