Congratulations on completing this module on functional programming in Python. We’ve covered many topics on how to write neat and efficient solutions using functional programming. Let’s briefly recap everything covered so far:
- Functional programming follows the declarative approach to programming; the programmer describes what needs to be done as opposed to how it is done.
- Functional programming relies heavily on immutable data structures.
- It is preferable to use a tuple instead of a list when writing code because a tuple is immutable.
- Using a
namedtupleto store data leads to a more readable data structure.
namedtuplecollection resides in the
namedtupleis created like so:name = ("name", ["property1", "property2", …, "property"])
- Representing an object by a
namedtupleis done like so:obj = name(property1, property2, …, property)
- Referencing an element of a
namedtupleis done like this:property = name.property
- A lambda function is an anonymous function.
- A lambda function can be stored in a variable like any other data type.
- A lambda function can be defined directly while passing it as an argument of another function.
- The three higher-order functions Python provides are:
- These functions can be supplied as the iterable parameter to any of the higher-order functions. Ex:
- h = map(lambda, filter(lambda, map(lambda, iterable)))
filter()filters an iterable before a mapping function is applied.
filter()filters an iterable before it is reduced.
- A CSV file can be processed by supplying the CSV reader as the iterable to the higher-order functions.
- A JSON file can only be represented in Python by a dictionary.
- An iterable is not available to read data stored in a JSON file.
- A dictionary must be provided to the higher-order functions when processing a JSON file.