I am learning how to properly use toolz
and I am looking for something equivalent to:
apply_tuple = lambda f: lambda *args: f(args)
unpack_tuple = lambda f: lambda args: f(*args)
However I cannot find an equivalent in the standard library nor in toolz
.
Context to avoid XY Problem:
I am trying to write a function that add two numbers using Tacit Programming style
So far I have:
import toolz as tz
import operator
successor = tz.curry(operator.add)(1)
assert successor(0) == 1
assert successor(10) == 11
apply_tuple = lambda f: lambda *args: f(args)
unpack_tuple = lambda f: lambda args: f(*args)
addition = tz.compose(
unpack_tuple(tz.pipe),
tz.juxt(
apply_tuple(tz.compose(tz.curry(tz.iterate)(successor), tz.first)),
apply_tuple(tz.compose(tz.curry(tz.nth), tz.last)),
)
)
assert addition(1, 0) == 1
assert addition(0, 0) == 0
assert addition(0, 1) == 1
assert addition(10, 10) == 20
The code works as expected but I would like to replace the apply_tuple
and unpack_tuple
with either built-in or toolz
equivalents.
Alternative solutions to represent the addition function based on successor
will also be considered as valid solutions.