Add a new utils.algorithms file for code helpers.

The first helper, recursive_apply, goes through recursive dict/lists to apply a function on non-recursable types
This commit is contained in:
Salamandar 2023-12-14 17:21:47 +01:00
parent 38db30cd70
commit dc8f51e2f8

13
src/utils/algorithms.py Normal file
View file

@ -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)