Advertisement
Guest User

Untitled

a guest
Feb 9th, 2018
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Attribute VB_Name = "FuncTools"
  2. '*EN*
  3. ' This module implements some functional programming solutions
  4. '*RU*
  5. ' Модуль реализует некоторые ФП-решения
  6.  
  7. '*EN*
  8. ' Apply array of functions one-by-one to input_data
  9. ' Instead of out = f1(f2(f3(f4(f5(x))))) you can create array
  10. ' fnames = Array("f1", "f2", "f3", "f4", "f5") (sorry, there are not callbacks)
  11. ' and next apply them using for-loop one-by-one to input_data
  12. ' This implementation is as close to bash or haskell pipelining as possible
  13. ' It's NOT real functional programming, but it's better then Nothing
  14. '*RU*
  15. ' Применяет по очереди список функций к входным данным
  16. ' Вместо того, чтобы писать out = f1(f2(f3(f4(f5(x))))),
  17. ' можно создать массив, содержащий имена функций
  18. ' fnames = Array("f1", "f2", "f3", "f4", "f5") (это НЕ коллбэки. В VBA их нет)
  19. ' и следом их применить одну за одной ко входным данным
  20. ' Эта реализация настолько близка к пайплайнингу bash или Haskell,
  21. ' насколько это возможно
  22. ' ВНИМАНИЕ: Это НЕ функциональное программирование, но это лучше, чем ничего
  23. Function apply(array_function_names() As Variant, input_data As Variant) As Variant
  24. Dim N As Integer
  25. Dim function_name As Variant
  26.     apply = input_data
  27.     For N = LBound(array_function_names) To UBound(array_function_names)   'reduce
  28.        function_name = array_function_names(N)
  29.         apply = Application.Run(function_name, apply)
  30.     Next N
  31. End Function
  32.  
  33. '*EN*
  34. ' python-like all function all
  35. ' If all `conditions` is True, returns True
  36. '*RU*
  37. ' Python имеет встроенную функцию all
  38. ' Она проходит по всем условиям и возвращает False, как
  39. ' только встретит 1й False
  40. Public Function all(conditions() As Variant) As Boolean
  41. Dim i As Integer
  42.     all = True
  43.     For i = LBound(conditions) To UBound(conditions)
  44.         If Not conditions(i) Then
  45.             all = False
  46.             Exit Function
  47.         End If
  48.     Next i
  49. End Function
  50.  
  51. '*EN*
  52. ' python-like all function any
  53. ' return True when meet first of `conditions` equals True
  54. '*RU*
  55. ' Аналог функции any в Python. Возвращает False только если
  56. ' во входном массиве не было обнаружено ни одного True
  57. Public Function anyone(conditions() As Variant) As Boolean
  58. Dim i As Integer
  59.     anyof = False
  60.     For i = LBound(conditions) To UBound(conditions)
  61.         If conditions(i) Then
  62.             anyone = True
  63.             Exit Function
  64.         End If
  65.     Next i
  66. End Function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement