higher_func.py 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. from typing import Callable
  2. def apply(func, objects):
  3. return [func(x) for x in objects]
  4. def compute(func_list, *args):
  5. return [func(x) for func in func_list for x in args]
  6. def compute_new(func_list, *args):
  7. lst = []
  8. value = 0
  9. for x in args:
  10. value = x
  11. for func in func_list:
  12. value = func(value)
  13. lst.append(value)
  14. return lst
  15. # def filter_list(f: Callable, lst: list):
  16. def filter_list(f, lst):
  17. return [value for value in lst if f(value)]
  18. def filter_collection(f, collection):
  19. lst = [value for value in collection if f(value)]
  20. if isinstance(collection, str):
  21. return ''.join(lst)
  22. return type(collection)(lst)
  23. def aggregation(func, sequence):
  24. new_list = []
  25. foo = func(sequence[0], sequence[1])
  26. new_list.append(foo)
  27. for x in range(2, len(sequence)):
  28. foo = func(foo, sequence[x])
  29. new_list.append(foo)
  30. return new_list
  31. def aggregation_2(func, sequence):
  32. new_list = []
  33. foo = func(sequence[0], sequence[1])
  34. new_list.append(foo)
  35. for x in range(2, len(sequence)):
  36. foo = func(foo, sequence[x])
  37. new_list.append(foo)
  38. return new_list[-1]
  39. def aggregation_3(func, sequence, initial=None):
  40. new_list = []
  41. if initial != None:
  42. foo = initial
  43. for x in sequence:
  44. foo = func(foo, x)
  45. else:
  46. foo = func(sequence[0], sequence[1])
  47. for x in range(2, len(sequence)):
  48. foo = func(foo, sequence[x])
  49. return foo
  50. """Для теста"""
  51. def square(num):
  52. return num ** 2
  53. def inc(num):
  54. return num + 1
  55. def dec(num):
  56. return num - 1
  57. def is_even(num):
  58. return num % 2 == 0
  59. def get_add(x, y):
  60. return x + y
  61. def get_max(x, y):
  62. return max(x, y)
  63. def get_product(x, y):
  64. return x * y
  65. def main():
  66. # print(compute([inc, dec, square], 10, 20, 30, 40))
  67. # print(compute_new([inc, square, dec], 10, 20, 30, 40))
  68. # numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  69. # even_numbers = filter_list(is_even, numbers) # берем только четные
  70. # print(even_numbers)
  71. # numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  72. # even_numbers = filter_collection(is_even, numbers)
  73. # print(even_numbers)
  74. # print(filter_collection(lambda x: x not in 'aeiou', 'I never heard those lyrics before'))
  75. # print(aggregation(get_add, [5, 2, 4, 3, 5]))
  76. # print(aggregation_2(get_max, [1, 4, 5, 7, 6, 5, 8, 10, 5]))
  77. # print(aggregation_2(get_add, [5, 2, 4, 3, 5]))
  78. # print(aggregation_3(lambda x, y: x + y, [4, 5, 6], initial=100))
  79. print(aggregation_3(get_product, [2, 5, 10, 1, 2]))
  80. if __name__ == '__main__':
  81. main()