Skip to content

Instantly share code, notes, and snippets.

@candlerb
Last active October 27, 2020 11:01
Show Gist options
  • Save candlerb/ba4881041cb50fc045959df912d37f97 to your computer and use it in GitHub Desktop.
Save candlerb/ba4881041cb50fc045959df912d37f97 to your computer and use it in GitHub Desktop.
Mixing 'let' and '<-'
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 = do
(lambda v0: # let v0 = 4
ValueAndLog(v0*v0, "Squared %d\n" % v0) >> (lambda v1: # v1 <- ValueAndLog(v0*v0, ...)
(lambda v2: # let v2 = 2*v1
ValueAndLog(1+v2, "Added 1 to %d\n" % v2) # ValueAndLog(1+v2, ...)
)(2*v1)
)
)(4)
)
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