withRecursion
lib.generators.withRecursion
Docs pulled from | This Revision | 10 minutes ago
Recurses through a Value
limited to a certain depth. (depthLimit
)
If the depth is exceeded, an error is thrown, unless throwOnDepthLimit
is set to false
.
Inputs
- Structured function argument
-
- depthLimit (required)
- If this option is not null, the given value will stop evaluating at a certain depth
-
- throwOnDepthLimit (optional, default:
true
) - If this option is true, an error will be thrown, if a certain given depth is exceeded
- throwOnDepthLimit (optional, default:
- Value
- The value to be evaluated recursively
Noogle detected
Implementation
The following is the current implementation of this function.
withRecursion =
{
depthLimit,
throwOnDepthLimit ? true,
}:
assert isInt depthLimit;
let
specialAttrs = [
"__functor"
"__functionArgs"
"__toString"
"__pretty"
];
stepIntoAttr = evalNext: name: if elem name specialAttrs then id else evalNext;
transform =
depth:
if depthLimit != null && depth > depthLimit then
if throwOnDepthLimit then
throw "Exceeded maximum eval-depth limit of ${toString depthLimit} while trying to evaluate with `generators.withRecursion'!"
else
const "<unevaluated>"
else
id;
mapAny =
depth: v:
let
evalNext = x: mapAny (depth + 1) (transform (depth + 1) x);
in
if isAttrs v then
mapAttrs (stepIntoAttr evalNext) v
else if isList v then
map evalNext v
else
transform (depth + 1) v;
in
mapAny 0;