mirror of
https://github.com/Hizenberg469/Python-tutorial.git
synced 2026-04-19 23:12:24 +03:00
Functional programming finished
This commit is contained in:
19
Functional-programming/reduce.py
Normal file
19
Functional-programming/reduce.py
Normal file
@@ -0,0 +1,19 @@
|
||||
from functools import reduce
|
||||
|
||||
my_list = [1,2,3]
|
||||
|
||||
def accumalator(acc, item):
|
||||
print(acc, item)
|
||||
return acc + item
|
||||
|
||||
print(reduce(accumalator, my_list, 10))
|
||||
|
||||
# What reduce does?
|
||||
|
||||
# reduce take the function given and take item from the
|
||||
# iterable and pass to function given to it. The first
|
||||
# parameter is the value initialized as the 3rd argument
|
||||
# to the reduce function. It do the processing and returns
|
||||
# a value which is used as next initialiizing value for the
|
||||
# first argument of the function provided in the reduce function
|
||||
# and so on for all items in iterable.
|
||||
Reference in New Issue
Block a user