mirror of
https://github.com/blackboxprogramming/lucidia.git
synced 2026-03-17 06:57:10 -05:00
Update recursion_engine.py
This commit is contained in:
committed by
GitHub
parent
3d59553ec8
commit
c2f1c115b6
@@ -1 +1,35 @@
|
|||||||
print("Hello World")
|
from __future__ import annotations
|
||||||
|
|
||||||
|
from dataclasses import dataclass
|
||||||
|
from typing import Callable, Any
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class RecursionLimits:
|
||||||
|
max_depth: int = 5
|
||||||
|
max_nodes: int = 10_000
|
||||||
|
|
||||||
|
|
||||||
|
class RecursionEngine:
|
||||||
|
"""Safe, bounded recursion helper."""
|
||||||
|
def __init__(self, limits: RecursionLimits | None = None) -> None:
|
||||||
|
self.limits = limits or RecursionLimits()
|
||||||
|
self._nodes = 0
|
||||||
|
|
||||||
|
def recursive(self, fn: Callable[[Any], Any], x: Any, depth: int = 0) -> Any:
|
||||||
|
if depth > self.limits.max_depth:
|
||||||
|
raise RecursionError("max_depth exceeded")
|
||||||
|
if self._nodes >= self.limits.max_nodes:
|
||||||
|
raise RecursionError("max_nodes exceeded")
|
||||||
|
|
||||||
|
self._nodes += 1
|
||||||
|
y = fn(x)
|
||||||
|
# placeholder: stop when fn returns x unchanged
|
||||||
|
if y == x:
|
||||||
|
return y
|
||||||
|
return self.recursive(fn, y, depth + 1)
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
eng = RecursionEngine(RecursionLimits(max_depth=3))
|
||||||
|
print(eng.recursive(lambda n: n - 1 if n > 0 else n, 3))
|
||||||
|
|||||||
Reference in New Issue
Block a user