Skip to content

Conversation

@Duologic
Copy link
Member

I've written this function at least twice, figured it would be useful as part of xtd.

|||
`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.

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?

Copy link
Member Author

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.

Comment on lines 221 to 226
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),

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.

Copy link
Member Author

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?

Suggested change
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
),

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.

Copy link
Member Author

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.

@Duologic Duologic requested a review from jdbaldry March 18, 2025 09:01
Copy link

@jdbaldry jdbaldry left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice

@Duologic Duologic merged commit 4eee017 into master Mar 20, 2025
2 checks passed
@Duologic Duologic deleted the duologic/deepMap branch March 20, 2025 14:01
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants