-
Notifications
You must be signed in to change notification settings - Fork 132
Open
Labels
Description
http://www.python.org/dev/peps/pep-3113/ removed tuple param unpacking so in Python 3:
(
seq((1, 2), (3, 4))
.map(lambda a, b: a + b) # TypeError: <lambda>() missing 1 required positional argument: 'b'
)
more background:
https://stackoverflow.com/questions/21892989/what-is-the-good-python3-equivalent-for-auto-tuple-unpacking-in-lambda
https://stackoverflow.com/questions/11328312/python-lambda-does-not-accept-tuple-argument
Seeing as how unpacking can be implemented explicitly using more functions:
import functools
def unpack(f):
@functools.wraps(f)
def unpacked(args):
return f(*args)
return unpacked
(
seq((1, 2), (3, 4))
.map(unpack(lambda a, b: a + b))
) # => [3, 7]
How about building that functionality as an option, directly into all the seq()
methods? e.g.:
(
seq((1, 2), (3, 4))
.map(unpack=lambda a, b: a + b)
)
Also, a more powerful nested unpacking variation by (ab)using comprehensions:
(
seq((1, (2, 3)), (3, (4, 5)))
.map(lambda t: next(a + b + c for a, (b, c) in (t,)))
)
but not sure that one can be smoothly worked in like unpack
giuseppe16180 and aandrejevas