Skip to content

Commit

Permalink
Merge pull request #106 from DavidAwad/patch-2
Browse files Browse the repository at this point in the history
Add some information on reduce! :D
  • Loading branch information
yasoob committed Jan 7, 2016
2 parents 35f531d + d04acd5 commit 9e8cce3
Showing 1 changed file with 32 additions and 3 deletions.
35 changes: 32 additions & 3 deletions map_filter.rst
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
Map & Filter
Map, Filter and Reduce
------------

These are two functions which facilitate a functional approach to
These are three functions which facilitate a functional approach to
programming. We will discuss them one by one and understand their use
cases.

Expand Down Expand Up @@ -60,7 +60,7 @@ of a list of inputs we can even have a list of functions!
Filter
^^^^^^^^^

As the name suggests, filter creates a list of elements for which a
As the name suggests, ``filter`` creates a list of elements for which a
function returns true. Here is a short and concise example:

.. code:: python
Expand All @@ -75,3 +75,32 @@ The filter resembles a for loop but it is a builtin function and faster.

**Note:** If map & filter do not appear beautiful to you then you can
read about ``list/dict/tuple`` comprehensions.

Reduce
^^^^^^^^^

``Reduce`` is a really useful function for performing some computation on
a list and returning the result. For example, if you wanted to compute
the product of a list of integers.

So the normal way you might go about doing this task in python is using
a basic for loop:


.. code:: python
product = 1
list = [1, 2, 3, 4]
for num in list:
product = product * num
# product = 24
Now let's try it with reduce:

.. code:: python
from functools import reduce
product = reduce( (lambda x, y: x * y), [1, 2, 3, 4] )
# Output: 24

0 comments on commit 9e8cce3

Please sign in to comment.