Skip to content

Instantly share code, notes, and snippets.

@candlerb
Last active October 27, 2020 11:03
Show Gist options
  • Save candlerb/8d26e3811622d72d83089934b5057379 to your computer and use it in GitHub Desktop.
Save candlerb/8d26e3811622d72d83089934b5057379 to your computer and use it in GitHub Desktop.
Nested binds
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)
@staticmethod
def unit(v):
return ValueAndLog(v, "")
wv3 = ( #wv3 =
ValueAndLog.unit(4) >> (lambda v0: # v0 <- ValueAndLog.unit(4)
ValueAndLog(v0*v0, "Squared %d\n" % v0) >> (lambda v1: # v1 <- ValueAndLog(v0*v0, ...)
ValueAndLog.unit(2*v1) >> (lambda v2: # v2 <- ValueAndLog.unit(2*v1)
ValueAndLog(1+v2, "Added 1 to %d\n" % v2) # ValueAndLog(1+v2, ...)
)
)
)
)
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