Advertisement
Guest User

Untitled

a guest
Jul 26th, 2017
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.54 KB | None | 0 0
  1. def equals(value):
  2. '''
  3. Returns a function that when called will check the called value with the given value for equality
  4. '''
  5. return lambda called_value: value == called_value
  6.  
  7. def reverse(collection):
  8. return collection[::-1]
  9.  
  10.  
  11. def unpack_args(fn, args):
  12. '''
  13. Functionally unpack a collection of arguments.
  14. Handles case where args is not a collection and will call fn without unpacking..
  15. '''
  16. if isinstance(args, Iterable):
  17. return fn(*args)
  18. return fn(args)
  19.  
  20.  
  21. def compose(*fns):
  22. '''
  23. Returns a function composed of the given functions where the first function is the outermost in the composition.
  24.  
  25. Example:
  26. >>> compose(sum, list, set)([1,2,])
  27. 3
  28.  
  29. Equivalent to:
  30. >>> sum(list(set([1,2])))
  31. 3
  32. '''
  33. def comp(args, func, *fns):
  34. if fns:
  35. return func(comp(args, fns[0], *fns[1:]))
  36. return func(*args)
  37.  
  38. return lambda *args: comp(args, fns[0], *fns[1:])
  39.  
  40.  
  41. def partial_front(fn, *args):
  42. '''
  43. A partial where the returned function places it's given values in front of the partial values.
  44.  
  45. Example:
  46. >>> join = lambda a,b,c: a+b+c
  47. >>> partial_first(join, [1,2])([3], [4])
  48. [3, 4, 1, 2]
  49. '''
  50. return lambda *call_args: fn(*(call_args + args))
  51.  
  52.  
  53. def partial_as(fn, *args):
  54. '''
  55. A partial where the position of the called arguments are specified with {}.
  56.  
  57. Note: This is dangerous in the case that a partialized value may be an empty dict unintentionally
  58.  
  59. Example:
  60. >>> join = lambda a,b,c,d: a+b+c+d
  61. >>> fntools.partial_as(join, [1], {}, [3], {})([3], [4])
  62. [1, 3, 3, 4]
  63. '''
  64. def replace_placeholders(args, call_args):
  65. if len(filter(equals({}), args)) == len(call_args):
  66. for i, arg in enumerate(args):
  67. if arg == {}:
  68. args[i] = call_args.pop(0)
  69. return args
  70. if not args:
  71. return call_args
  72. raise Exception("Number of placeholders does not match number of given arguments")
  73.  
  74. return lambda *call_args: fn(*replace_placeholders(list(args), list(call_args)))
  75.  
  76.  
  77. def thread_with(value, partial_func, *forms):
  78. '''
  79. Thread the given value through the list of forms with partial_func applied.
  80. '''
  81. return compose(*map(partial(unpack_args, partial_func), reverse(forms)))(value)
  82.  
  83.  
  84. def thread_first(value, *forms):
  85. '''
  86. Returns the given value threaded through the given collection of forms as the first argument.
  87.  
  88. A form is a function or a tuple containing a function as its first element and a list of arguments.
  89. The return value of the previously called function will be threaded in as the first argument to the next function.
  90. Use this to flatten nested function calls.
  91.  
  92. Example:
  93. >>> def add(a, b):
  94. ... return a + b
  95. ...
  96.  
  97. >>> add(list((1,2,)), [3])
  98. [1, 2, 3]
  99.  
  100. >>> thread_first((1,2,), list, (add, [3]))
  101. [1, 2, 3]
  102. '''
  103. return thread_with(value, partial_front, *forms)
  104.  
  105.  
  106. def thread_last(value, *forms):
  107. '''
  108. Returns the given value threaded through the given collection of forms as the last argument.
  109. Refer to docs above.
  110.  
  111. Example:
  112. >>> thread_last((1,2,), list, (add, [3]))
  113. [3, 1, 2]
  114. '''
  115. return thread_with(value, partial, *forms)
  116.  
  117.  
  118. def thread_as(value, *forms):
  119. '''
  120. Returns the given value threaded through the given collection of forms in the position of the argument defined by a {}.
  121.  
  122. Example:
  123. >>> join = lambda a,b,c: a+b+c
  124. ...
  125. >>> thread_as([2], (join, [1], {}, [3]), set)
  126. set([1, 2, 3])
  127. '''
  128. return thread_with(value, partial_as, *forms)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement