-
Notifications
You must be signed in to change notification settings - Fork 9
feat(inspect): add deepMap function #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
inspect.libsonnet
Outdated
| ||| | ||
| `deepMap` traverses the whole tree of `x` and applies `func(item)` indiscriminately. | ||
| If the type of `item` is an object or array, then `func` must return the same type. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we want to enforce that in function?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Jsonnet will error out anyway, this is more of a warning than it is a rule.
inspect.libsonnet
Outdated
| deepMap(func, x): | ||
| if std.isObject(x) | ||
| then std.mapWithKey(function(_, y) self.deepMap(func, y), func(x)) | ||
| else if std.isArray(x) | ||
| then std.map(function(y) self.deepMap(func, y), func(x)) | ||
| else func(x), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should deepMap recurse first before applying func? At the moment we have a pre-order traversal, is that what people would expect? If their func modifies x, then we may never recurse into the children.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This worked for my use cases, another solution might work as well.
Would you do this instead?
| deepMap(func, x): | |
| if std.isObject(x) | |
| then std.mapWithKey(function(_, y) self.deepMap(func, y), func(x)) | |
| else if std.isArray(x) | |
| then std.map(function(y) self.deepMap(func, y), func(x)) | |
| else func(x), | |
| deepMap(func, x): | |
| func( | |
| if std.isObject(x) | |
| then std.mapWithKey(function(_, y) self.deepMap(func, y), x) | |
| else if std.isArray(x) | |
| then std.map(function(y) self.deepMap(func, y), x) | |
| else x | |
| ), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
To be honest, I'm really not sure what traversal people would expect. I can't remember the last time I performed a map over trees. I guess we can just document what traversal it is and people can use that to decide if this does what they want.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm inclined to apply this suggestion, it'll allow to drop the comment about return type as that would only apply if we execute func early. One less way to fail I guess.
jdbaldry
left a comment
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice
I've written this function at least twice, figured it would be useful as part of xtd.