Closures and functional programming
How to use the @closure decorator for stream processing
import os
import tinychain as tc
HOST = tc.host.Host(os.getenv("TC_HOST", "http://127.0.0.1:8702"))
ENDPOINT = "/transact/hypothetical"
# initialize a new execution context
cxt = tc.Context()
# instantiate a Tuple
cxt.tuple = tc.Tuple([1, 2, 3])
@tc.get_op
def pow(x: tc.Number):
return x**2
# create a new Tuple by squaring the elements in `cxt.Tuple`
cxt.raised = cxt.tuple.map(pow)
if __name__ == "__main__":
# check that the implementation works as expected
assert HOST.post(ENDPOINT, cxt) == [1, 4, 9]Examples
Last updated
Was this helpful?