jsonnet icon indicating copy to clipboard operation
jsonnet copied to clipboard

std.foldr documentation does not match behavior

Open zapster opened this issue 4 years ago • 1 comments

The documentation says

std.foldr(func, arr, init)

Classic foldr function. Calls the function on the result of the previous function call and each array element, or init in the case of the initial element. Traverses the array from right to left.

but the behaviour is "Calls the function on each array element and the result of the previous function call."

Example (adopted from #788):

[
  std.foldr(function(x, y) x + y, ["a", "b", "c"], "I")
]

returns

[
  "abcI"
]

although according to the documentation it would be

[
  "lcba"
]

zapster avatar Feb 12 '21 11:02 zapster

You're correct. In this case we should fix the documentation, because it makes more sense this way (rationale) and people depend on this behavior already.

I put some context below for posterity.

Here's what it does: Step 1: res = "I" Step 2: res = f("c", "I") = "cI" Step 3: res = f("b", "cI") = "bcI" Step 4: res = f("a", "bcI") = "abcI"

Here's the code (you can see the inverted arguments): https://github.com/google/jsonnet/blob/v0.17.0/stdlib/std.jsonnet#L788 https://github.com/google/jsonnet/blob/v0.17.0/stdlib/std.jsonnet#L796

sbarzowski avatar Feb 12 '21 14:02 sbarzowski