@@ -247,9 +247,9 @@ def pipe(values0, *bodys):
247247 The only restriction is that each function must take as many positional
248248 arguments as the previous one returns.
249249
250- The output from each function is unpacked to the argument list of
251- the next one. If the duck test fails, the output is assumed to be
252- a single value, and is fed in to the next function as-is.
250+ At each step, if the output from a function is a list or a tuple,
251+ it is unpacked to the argument list of the next function. Otherwise,
252+ we assume the output is intended to be fed to the next function as-is.
253253
254254 If you only need a one-in-one-out chain, ``pipe1`` is faster.
255255
@@ -271,15 +271,12 @@ def pipe(values0, *bodys):
271271 lambda x, y, s: (x + y, s))
272272 assert (a, b) == (13, "got foo")
273273 """
274- def unpack_ctx (* args ): pass # just a context where we can use * to unpack
275274 xs = values0
276275 for update in bodys :
277- try :
278- unpack_ctx (* xs ) # duck test
279- except TypeError :
280- xs = update (xs ) # single x only
276+ if isinstance (xs , (list , tuple )):
277+ xs = update (* xs )
281278 else :
282- xs = update (* xs ) # multiple xs
279+ xs = update (xs )
283280 return xs
284281
285282class piped :
@@ -300,14 +297,10 @@ def __or__(self, f):
300297 return self ._xs
301298 else :
302299 cls = self .__class__
303- def unpack_ctx (* args ): pass # just a context where we can use * to unpack
304- xs = self ._xs
305- try :
306- unpack_ctx (* xs ) # duck test
307- except TypeError :
308- return cls (f (self ._xs )) # single x only
300+ if isinstance (self ._xs , (list , tuple )):
301+ return cls (* f (* self ._xs ))
309302 else :
310- return cls (* f ( * self ._xs )) # multiple xs
303+ return cls (f ( self ._xs ))
311304 def __repr__ (self ):
312305 return "<piped at 0x{:x}; values {}>" .format (id (self ), self ._xs )
313306
@@ -333,15 +326,12 @@ def __init__(self, *xs, _funcs=None):
333326 def __or__ (self , f ):
334327 """Pipe the values into f; but just plan to do so, don't perform it yet."""
335328 if f is get : # compute now
336- def unpack_ctx (* args ): pass # just a context where we can use * to unpack
337329 vs = self ._xs
338330 for g in self ._funcs :
339- try :
340- unpack_ctx (* vs ) # duck test
341- except TypeError :
342- vs = g (vs ) # single v only
331+ if isinstance (vs , (list , tuple )):
332+ vs = g (* vs )
343333 else :
344- vs = g (* vs ) # multiple vs
334+ vs = g (vs )
345335 return vs
346336 else :
347337 # just pass on the references to the original xs.
0 commit comments