Image taken from: [So You Want to be a Functional Programmer (Part 1)](https://cscalfani.medium.com/so-you-want-to-be-a-functional-programmer-part-1-1f15e387e536)
>**Functional programming** a programming paradigm where programs are constructed by applying and composing functions
`lambda` function is a single line function declared without a name, can have any number of arguments, but can have only one expression.
```python
>>>lambda[parameter_list]:expression
```
Rewriting `square` function using `lambda`
%% Cell type:code id:ebb6f7ef tags:
``` python
lambdax:x**2
```
%%%% Output: execute_result
<function __main__.<lambda>(x)>
%% Cell type:code id:5203c823 tags:
``` python
(lambdax:x**2)(2)
```
%%%% Output: execute_result
4
%% Cell type:markdown id:283f8587 tags:
##### `map` and `filter` using `lambda`
%% Cell type:code id:0cc74352 tags:
``` python
list(map(lambdax:x**2,data))
```
%%%% Output: execute_result
[1, 4, 9, 16, 25, 36, 49, 64, 81, 100]
%% Cell type:code id:2360f9ad tags:
``` python
list(filter(lambdax:len(x)>3,text))
```
%%%% Output: execute_result
['this', 'Some', 'TEXT']
%% Cell type:markdown id:429d14f6 tags:
#### Reduce
#### reduce
Upto Python 2, `reduce()` was a built-in function, but in Python 3 it is demoted to `functools`
> I received an email from a compatriot lamenting the planned demise of `reduce()` and `lambda` in **Python 3000**. After a few exchanges I think even he agreed that they can go. Here's a summary, including my reasons for dropping `lambda`, `map()` and `filter()`. I expect tons of disagreement in the feedback, all from ex-Lisp-or-Scheme folks. :-)
[Guido van Rossum](https://www.artima.com/weblogs/viewpost.jsp?thread=98196), _March 12, 2005_
%% Cell type:code id:fd96efc0 tags:
``` python
fromfunctoolsimportreduce
```
%% Cell type:code id:f3e79aed tags:
``` python
reduce(add,[47,11,42,13])
```
%%%% Output: execute_result
113
%% Cell type:markdown id:894e8c0e tags:

Image taken from: [Reducing a list](https://www.python-course.eu/lambda.php)
### Additional materials
*[Python functional programming how to](https://docs.python.org/3/howto/functional.html)
*[Joel Grus: Learning Data Science Using Functional Python](https://www.youtube.com/watch?v=ThS4juptJjQ)
%% Cell type:markdown id:53a406e0 tags:
## Toolz - functional Python
A set of utility functions for iterators, functions and dictionaries, and contains several functions (originally from `itertoolz` and `functoolz` libraries) that are absent in the standard [`itertools`](https://docs.python.org/3/library/itertools.html) and [`functools`](https://docs.python.org/3/library/functools.html) libraries.
Toolz functions have the following properties:
[Toolz](https://github.com/pytoolz/toolz) functions have the following properties:
* composability: interoperable due to use of core data structures
* functional purity: do not rely on external state or change input
* laziness: lazy evaluation
```bash
$ pip install toolz
```
%% Cell type:code id:cd348c7b tags:
``` python
importitertools
```
%% Cell type:markdown id:8b07be5b tags:
### A sample set of `itertoolz` functions
%% Cell type:code id:e252174a tags:
``` python
fromtoolz.itertoolzimporttake,drop,take_nth
importitertools
```
%% Cell type:markdown id:062c6ce7 tags:
#### Operations on `n` elements of a sequence
wrapper for `itertools.islice`
take first `n` elements
%% Cell type:code id:80222b27 tags:
``` python
take3=list(take(3,[1,2,3,4,5,6,7]))
print('take : ',take3)
# similar to itertools.islice(iterable,start)
# itertools.islice(iterable,start)
islice3=list(itertools.islice([1,2,3,4,5,6,7],3))
print('islice:',islice3)
```
%%%% Output: stream
take : [1, 2, 3]
islice: [1, 2, 3]
%% Cell type:markdown id:0528bf37 tags:
drop first `n` elements
%% Cell type:code id:f352e1c3 tags:
``` python
drop3=list(drop(3,[1,2,3,4,5,6,7]))
print('drop : ',drop3)
# similar to itertools.islice(iterable,start,None)