When it comes to tasks such as filtering, PyFunctional provides an incredibly diverse range of operators that we can leverage to transform our streams into our desired results.
In this example, we'll show just how simple it is to do something such as filter an array of stock transactions before mapping the results to an array and then summing this array to find the total costs of a given transaction.
We'll leverage the namedtuple module from collections in order to define a Stock tuple. With this namedtuple structure, we'll then define an array of stock transactions that consists of the stock ticker and the cost of, the said transaction:
from functional import seq
from collections import namedtuple
Stock = namedtuple('Stock', 'tckr price')
stocks = [
Stock('AMZN', 100),
Stock('FACE', 200),
Stock('JPM', 80),
Stock('TSLA', 500),
Stock('TSLA', 450)
]
costs = seq(stocks)\
.filter(lambda x: x.tckr == 'TSLA')\
.map(lambda x: x.price)\
.sum()
print("Total cost of TSLA transactions: {}".format(costs))