@@ -72,8 +72,9 @@ def pospartial(function, positional_arguments):
7272 which yield
7373 res == 23+32+19 == 74
7474
75- Return:
76- callable
75+ Returns
76+ -------
77+ out : Callable
7778 """
7879
7980 def wrapper (* args , fn = function , pas = positional_arguments , ** kwargs ):
@@ -83,8 +84,7 @@ def wrapper(*args, fn=function, pas=positional_arguments, **kwargs):
8384 return fn (* nargs , ** kwargs )
8485 return wrapper
8586
86- def compose_pair (f , g ):
87-
87+ class compose_pair :
8888 """
8989 Composes new function h = f(g(x)), from
9090 f and g.
@@ -96,33 +96,77 @@ def compose_pair(f, g):
9696 >>> h(1)
9797 >>> 4
9898
99- Return:
100- Callable
99+ Returns
100+ -------
101+ out : Callable
101102 """
102- def composed (* args , f = f , g = g , ** kwargs ):
103- return f (g (* args , ** kwargs ))
104103
105- return composed
104+ def __init__ (self , f , g ):
105+ self .f = f
106+ self .g = g
107+
108+ def __call__ (self , * args , ** kwargs ):
109+ return self .f (
110+ self .g (* args , ** kwargs )
111+ )
106112
107- def compose ( * functions ) :
113+ class compose :
108114
109115 """
110116 Sequence composite functions of `functions`.
111117 E.g. let fns be a list of functions [f, g, h] and
112118 compose(fns) would then represent lambda x : f(g(h(x)))
113119
114- Example:
120+ Examples
121+ --------
115122 >>> inc = lambda x: x+1
116123 >>> double = lambda x: x*2
117124 >>> f = compose(inc, double, inc)
118125 >>> f(2)
119- >>> 7
126+ 7
120127
121- Return:
122- function
128+ Returns
129+ -------
130+ out : Callable
131+ """
132+
133+ def __init__ (self , * functions ):
134+ self .fn = functools .reduce (compose_pair , functions )
135+
136+ def __call__ (self , * args , ** kwargs ):
137+ return self .fn (* args , ** kwargs )
138+
139+ class fnmap :
140+
141+ """
142+ Runs an iterable of functions with same arguments.
143+ Returns an iterable of result from each of the functions.
144+
145+ Examples
146+ --------
147+ >>> fn = fnmap(lambda x: x+1, lambda x: x+2)
148+ >>> list(fn(3))
149+ [4, 5]
150+
151+ >>> def add_one(x): return x+1
152+ >>> def mul_two(x): return x*2
153+ >>> fn = fnmap(add_one, mul_two)
154+ >>> list(fn(x=3))
155+ [4, 6]
156+
157+ Returns
158+ -------
159+ out : Callable
123160 """
124161
125- return functools .reduce (compose_pair , functions )
162+ def __init__ (self , * functions ):
163+ self .functions = functions
164+
165+ def __call__ (self , * args , ** kwargs ):
166+ return map (
167+ lambda fn : fn (* args , ** kwargs ),
168+ self .functions
169+ )
126170
127171def indexing (lst : list , index_item ):
128172
0 commit comments