Iterate function until fixed point. Function has to take only Table arguments. Function has to return a single Table, a tuple of Tables, or a dict of Tables. Iterate returns the same shape of arguments as the ``func`` function: either a single Table, a tuple of Tables, or a dict of T
(
func,
iteration_limit: int | None = None,
**kwargs: table.Table | op.iterate_universe,
)
| 37 | |
| 38 | @check_arg_types |
| 39 | def iterate( |
| 40 | func, |
| 41 | iteration_limit: int | None = None, |
| 42 | **kwargs: table.Table | op.iterate_universe, |
| 43 | ): |
| 44 | """Iterate function until fixed point. |
| 45 | Function has to take only Table arguments. |
| 46 | Function has to return a single Table, a tuple of Tables, or a dict of Tables. |
| 47 | Iterate returns the same shape of arguments as the ``func`` function: |
| 48 | either a single Table, a tuple of Tables, or a dict of Tables, respectively. |
| 49 | Initial arguments to function are passed through kwargs. |
| 50 | |
| 51 | Example: |
| 52 | |
| 53 | >>> import pathway as pw |
| 54 | >>> def collatz_transformer(iterated): |
| 55 | ... @pw.udf(deterministic=True) |
| 56 | ... def collatz_step(x: int) -> int: |
| 57 | ... if x == 1: |
| 58 | ... return 1 |
| 59 | ... elif x % 2 == 0: |
| 60 | ... return x // 2 |
| 61 | ... else: |
| 62 | ... return 3 * x + 1 |
| 63 | ... return iterated.select(val=collatz_step(iterated.val)) |
| 64 | >>> tab = pw.debug.table_from_markdown(''' |
| 65 | ... val |
| 66 | ... 1 |
| 67 | ... 2 |
| 68 | ... 3 |
| 69 | ... 4 |
| 70 | ... 5 |
| 71 | ... 6 |
| 72 | ... 7 |
| 73 | ... 8''') |
| 74 | >>> ret = pw.iterate(collatz_transformer, iterated=tab) |
| 75 | >>> pw.debug.compute_and_print(ret, include_id=False) |
| 76 | val |
| 77 | 1 |
| 78 | 1 |
| 79 | 1 |
| 80 | 1 |
| 81 | 1 |
| 82 | 1 |
| 83 | 1 |
| 84 | 1 |
| 85 | """ |
| 86 | if iteration_limit is not None and iteration_limit < 1: |
| 87 | raise ValueError("wrong iteration limit") |
| 88 | fn_spec = function_spec(func) |
| 89 | return G.add_iterate( |
| 90 | fn_spec, lambda node: node(**kwargs), iteration_limit=iteration_limit |
| 91 | ) |
| 92 | |
| 93 | |
| 94 | @check_arg_types |
nothing calls this directly
no test coverage detected