Skip to content

Commit 50dd6b4

Browse files
sbarzowskisparkprime
authored andcommitted
Add std.reverse
1 parent abc37c3 commit 50dd6b4

10 files changed

+53
-29
lines changed

benchmarks/bench.06.jsonnet

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
// A benchmark for builtin sort
22

3-
// TODO(sbarzowski) reverse functions should probably be a part of std
4-
local reverse(arr) =
5-
local l = std.length(arr);
6-
std.makeArray(l, function(i) arr[l - i - 1])
7-
;
8-
3+
local reverse = std.reverse;
94
local sort = std.sort;
105

116
true

doc/ref/stdlib.html

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1553,6 +1553,25 @@ <h4 id="flattenArrays">std.flattenArrays(arrs)</h4>
15531553
</div>
15541554
</div>
15551555

1556+
<div class="hgroup">
1557+
<div class="hgroup-inline">
1558+
<div class="panel">
1559+
<h4 id="sort">std.reverse(arr)</h4>
1560+
1561+
</div>
1562+
<div style="clear: both"></div>
1563+
</div>
1564+
</div>
1565+
1566+
<div class="hgroup">
1567+
<div class="hgroup-inline">
1568+
<div class="panel">
1569+
<p>Reverses an array.</p>
1570+
<em>Available in upcoming release.</em>
1571+
</div>
1572+
<div style="clear: both"></div>
1573+
</div>
1574+
</div>
15561575

15571576
<div class="hgroup">
15581577
<div class="hgroup-inline">

stdlib/std.jsonnet

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1132,6 +1132,10 @@ limitations under the License.
11321132
local bytes = std.base64DecodeBytes(str);
11331133
std.join('', std.map(function(b) std.char(b), bytes)),
11341134

1135+
reverse(arr)::
1136+
local l = std.length(arr);
1137+
std.makeArray(l, function(i) arr[l - i - 1]),
1138+
11351139
// Merge-sort for long arrays and naive quicksort for shorter ones
11361140
sort(arr, keyF=id)::
11371141
local quickSort(arr, keyF=id) =
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
RUNTIME ERROR: cannot test equality of functions
2-
std.jsonnet:1304:9-34 function <anonymous>
2+
std.jsonnet:1308:9-34 function <anonymous>
33
error.equality_function.jsonnet:17:1-33
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_array.jsonnet:18:18-32 thunk <array_element>
3-
std.jsonnet:1284:29-33 thunk <b>
4-
std.jsonnet:1284:21-33 function <anonymous>
5-
std.jsonnet:1284:21-33 function <aux>
6-
std.jsonnet:1287:15-31 function <anonymous>
7-
std.jsonnet:1288:11-23
3+
std.jsonnet:1288:29-33 thunk <b>
4+
std.jsonnet:1288:21-33 function <anonymous>
5+
std.jsonnet:1288:21-33 function <aux>
6+
std.jsonnet:1291:15-31 function <anonymous>
7+
std.jsonnet:1292:11-23
Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
RUNTIME ERROR: foobar
22
error.inside_equals_object.jsonnet:18:22-36 object <b>
3-
std.jsonnet:1298:50-54 thunk <b>
4-
std.jsonnet:1298:42-54 function <anonymous>
5-
std.jsonnet:1298:42-54 function <aux>
6-
std.jsonnet:1301:15-31 function <anonymous>
7-
std.jsonnet:1302:11-23
3+
std.jsonnet:1302:50-54 thunk <b>
4+
std.jsonnet:1302:42-54 function <anonymous>
5+
std.jsonnet:1302:42-54 function <aux>
6+
std.jsonnet:1305:15-31 function <anonymous>
7+
std.jsonnet:1306:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.invariant.equality.jsonnet:17:10-15 thunk <object_assert>
3-
std.jsonnet:1298:42-46 thunk <a>
4-
std.jsonnet:1298:42-54 function <anonymous>
5-
std.jsonnet:1298:42-54 function <anonymous>
6-
std.jsonnet:1302:11-23
3+
std.jsonnet:1302:42-46 thunk <a>
4+
std.jsonnet:1302:42-54 function <anonymous>
5+
std.jsonnet:1302:42-54 function <anonymous>
6+
std.jsonnet:1306:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: Object assertion failed.
22
error.obj_assert.fail1.jsonnet:20:23-29 thunk <object_assert>
3-
std.jsonnet:1298:42-46 thunk <a>
4-
std.jsonnet:1298:42-54 function <anonymous>
5-
std.jsonnet:1298:42-54 function <anonymous>
6-
std.jsonnet:1302:11-23
3+
std.jsonnet:1302:42-46 thunk <a>
4+
std.jsonnet:1302:42-54 function <anonymous>
5+
std.jsonnet:1302:42-54 function <anonymous>
6+
std.jsonnet:1306:11-23
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
RUNTIME ERROR: foo was not equal to bar
22
error.obj_assert.fail2.jsonnet:20:32-65 thunk <object_assert>
3-
std.jsonnet:1298:42-46 thunk <a>
4-
std.jsonnet:1298:42-54 function <anonymous>
5-
std.jsonnet:1298:42-54 function <anonymous>
6-
std.jsonnet:1302:11-23
3+
std.jsonnet:1302:42-46 thunk <a>
4+
std.jsonnet:1302:42-54 function <anonymous>
5+
std.jsonnet:1302:42-54 function <anonymous>
6+
std.jsonnet:1306:11-23

test_suite/stdlib.jsonnet

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ std.assertEqual(std.base64Decode('SGVsbG8gV29ybGQ='), 'Hello World') &&
323323
std.assertEqual(std.base64Decode('SGVsbG8gV29ybA=='), 'Hello Worl') &&
324324
std.assertEqual(std.base64Decode(''), '') &&
325325

326+
std.assertEqual(std.reverse([]), []) &&
327+
std.assertEqual(std.reverse([1]), [1]) &&
328+
std.assertEqual(std.reverse([1, 2]), [2, 1]) &&
329+
std.assertEqual(std.reverse([1, 2, 3]), [3, 2, 1]) &&
330+
std.assertEqual(std.reverse([[1, 2, 3]]), [[1, 2, 3]]) &&
331+
326332
std.assertEqual(std.sort([]), []) &&
327333
std.assertEqual(std.sort([1]), [1]) &&
328334
std.assertEqual(std.sort([1, 2]), [1, 2]) &&

0 commit comments

Comments
 (0)