Getting Started

Most of chainlet should feel familiar if you are used to Python. This guide covers the two key concepts you need to learn:

  • Defining a chainlink to perform a single transformation
  • Chaining several chainlinks to perform multiple transformations

Epilogue: Pulling your chain

You can not just push input to a chainlet, but also pull from it. This requires a chainlink that returns data when receiving value=None [2]:

>>> import random
>>>
>>> @funclet
... def generate(value, maximum=4):
...    """Generate values"""
...    if value is None:  # indicator that a new value is desired
...        return random.random() * maximum
...    return min(value, maximum)  # chainlets may provide both transformation and generation

Such a producer can be linked into a chain the same way as other elements. The resulting chain will produce values by itself if you send(None) to it:

>>> rand24 = generate(maximum=1) >> chain_by24
>>> rand24.send(1)  # use explicit starting value
24
>>> rand24.send(None)  # use generated starting value
12.013380549968177

On top of the explicit send(None), such a chain also supports regular iteration. You can iter over its values, and get the next value:

>>> next(rand24)
3.6175271892905103
>>> for count, result in enumerate(rand24):  # implicitly uses iter(rand24)
...     print(count, ':', result)
...     if result > 12:
...        break
0 : 10.786272495589447
1 : 23.653323415316734
[1]Depending on the elements used, chainlet will not actually execute this. It merely provides the same result.
[2]This replicates the generator interface, where next(gen) is equivalent to gen.send(None). See the Generator-Iterator Methods.