Skip to content

Instantly share code, notes, and snippets.

Last active Oct 27, 2020
What would you like to do?
A chain of assignments with wrapped values
class ValueAndLog:
def __init__(self, value, log):
self.value = value
self.log = log
def __rshift__(self, func):
result = func(self.value)
return ValueAndLog(result.value, self.log + result.log)
def unit(v):
return ValueAndLog(v, "")
wv0 = ValueAndLog.unit(4)
wv1 = wv0 >> (lambda x: ValueAndLog(x*x, "Squared %d\n" % x))
wv2 = wv1 >> (lambda x: ValueAndLog.unit(2*x))
wv3 = wv2 >> (lambda x: ValueAndLog(1+x, "Added 1 to %d\n" % x))
print("Answer:", wv3.value)
print("Log:", wv3.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment