Created
May 25, 2020 14:29
-
-
Save jigi-33/b2b5ff442ea3547b9cdd9b023019018d to your computer and use it in GitHub Desktop.
Функциональное программирование ближе к Middle
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
ЗАДАЧИ ПО ФУНКЦИОНАЛЬНОМУ ПРОГРАММИРОВАНИЮ | |
""" | |
# Написать функцию, которая суммирует все, что в нее передают(кортежи, списки, числа) | |
result = 0 | |
def sum_all(*args): | |
def sum_iter(args): | |
global result | |
for arg in args: | |
if not hasattr(arg, '__iter__'): | |
result += arg | |
else: | |
sum_iter(arg) | |
sum_iter(args) | |
return result | |
args = ( [ 1, 2, 3, 4, [5, 6] ], ( 7, 8, ( 9, 10 ) ) ) | |
q = sum_all(args) | |
print(q) | |
# Написать фунцию-фабрику сложения с аргументом | |
def addition(arg_1): | |
def inner(arg_2): | |
return arg_1 + arg_2 | |
return inner | |
# её же реализация с lambda функцией: | |
def add_lambda(arg_1): | |
return lambda x: arg_1 + x | |
add5 = addition(5) | |
add5(2) | |
# Функция аналог MAP'а | |
def anmappa(func, params): | |
if hasattr(func, '__iter__'): | |
return [tuple(f(param) for param in params) for f in func] | |
else: | |
return tuple(func(param) for param in list(params)) | |
a = anmappa(q, [1,2,3]) | |
print(a) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment