higher_func.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. """Для теста"""
  32. def square(num):
  33. return num ** 2
  34. def inc(num):
  35. return num + 1
  36. def dec(num):
  37. return num - 1
  38. def is_even(num):
  39. return num % 2 == 0
  40. def get_add(x, y):
  41. return x + y
  42. def main():
  43. # print(compute([inc, dec, square], 10, 20, 30, 40))
  44. # print(compute_new([inc, square, dec], 10, 20, 30, 40))
  45. # numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  46. # even_numbers = filter_list(is_even, numbers) # берем только четные
  47. # print(even_numbers)
  48. # numbers = (1, 2, 3, 4, 5, 6, 7, 8, 9, 10)
  49. # even_numbers = filter_collection(is_even, numbers)
  50. # print(even_numbers)
  51. # print(filter_collection(lambda x: x not in 'aeiou', 'I never heard those lyrics before'))
  52. print(aggregation(get_add, [5, 2, 4, 3, 5]))
  53. if __name__ == '__main__':
  54. main()