diff --git a/src/utils/algorithms.py b/src/utils/algorithms.py new file mode 100644 index 000000000..02e6205ee --- /dev/null +++ b/src/utils/algorithms.py @@ -0,0 +1,13 @@ +#!/usr/bin/env python3 + +from typing import Any, Callable + + +def recursive_apply(function: Callable, data: Any) -> Any: + if isinstance(data, dict): # FIXME: hashable? + return {key: recursive_apply(value, function) for key, value in data.items()} + + if isinstance(data, list): # FIXME: iterable? + return [recursive_apply(value, function) for value in data] + + return function(data)