Skip to content

Commit

Permalink
Add std.reverse
Browse files Browse the repository at this point in the history
  • Loading branch information
sbarzowski authored and sparkprime committed May 15, 2019
1 parent abc37c3 commit 50dd6b4
Show file tree
Hide file tree
Showing 10 changed files with 53 additions and 29 deletions.
7 changes: 1 addition & 6 deletions benchmarks/bench.06.jsonnet
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
// A benchmark for builtin sort

// TODO(sbarzowski) reverse functions should probably be a part of std
local reverse(arr) =
local l = std.length(arr);
std.makeArray(l, function(i) arr[l - i - 1])
;

local reverse = std.reverse;
local sort = std.sort;

true
Expand Down
19 changes: 19 additions & 0 deletions doc/ref/stdlib.html
Original file line number Diff line number Diff line change
Expand Up @@ -1553,6 +1553,25 @@ <h4 id="flattenArrays">std.flattenArrays(arrs)</h4>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="sort">std.reverse(arr)</h4>

</div>
<div style="clear: both"></div>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>Reverses an array.</p>
<em>Available in upcoming release.</em>
</div>
<div style="clear: both"></div>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
Expand Down
4 changes: 4 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -1132,6 +1132,10 @@ limitations under the License.
local bytes = std.base64DecodeBytes(str);
std.join('', std.map(function(b) std.char(b), bytes)),

reverse(arr)::
local l = std.length(arr);
std.makeArray(l, function(i) arr[l - i - 1]),

// Merge-sort for long arrays and naive quicksort for shorter ones
sort(arr, keyF=id)::
local quickSort(arr, keyF=id) =
Expand Down
2 changes: 1 addition & 1 deletion test_suite/error.equality_function.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: cannot test equality of functions
std.jsonnet:1304:9-34 function <anonymous>
std.jsonnet:1308:9-34 function <anonymous>
error.equality_function.jsonnet:17:1-33
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_array.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_array.jsonnet:18:18-32 thunk <array_element>
std.jsonnet:1284:29-33 thunk <b>
std.jsonnet:1284:21-33 function <anonymous>
std.jsonnet:1284:21-33 function <aux>
std.jsonnet:1287:15-31 function <anonymous>
std.jsonnet:1288:11-23
std.jsonnet:1288:29-33 thunk <b>
std.jsonnet:1288:21-33 function <anonymous>
std.jsonnet:1288:21-33 function <aux>
std.jsonnet:1291:15-31 function <anonymous>
std.jsonnet:1292:11-23
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_object.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_object.jsonnet:18:22-36 object <b>
std.jsonnet:1298:50-54 thunk <b>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1298:42-54 function <aux>
std.jsonnet:1301:15-31 function <anonymous>
std.jsonnet:1302:11-23
std.jsonnet:1302:50-54 thunk <b>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1302:42-54 function <aux>
std.jsonnet:1305:15-31 function <anonymous>
std.jsonnet:1306:11-23
8 changes: 4 additions & 4 deletions test_suite/error.invariant.equality.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.invariant.equality.jsonnet:17:10-15 thunk <object_assert>
std.jsonnet:1298:42-46 thunk <a>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1302:11-23
std.jsonnet:1302:42-46 thunk <a>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1306:11-23
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail1.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.obj_assert.fail1.jsonnet:20:23-29 thunk <object_assert>
std.jsonnet:1298:42-46 thunk <a>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1302:11-23
std.jsonnet:1302:42-46 thunk <a>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1306:11-23
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail2.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: foo was not equal to bar
error.obj_assert.fail2.jsonnet:20:32-65 thunk <object_assert>
std.jsonnet:1298:42-46 thunk <a>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1298:42-54 function <anonymous>
std.jsonnet:1302:11-23
std.jsonnet:1302:42-46 thunk <a>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1302:42-54 function <anonymous>
std.jsonnet:1306:11-23
6 changes: 6 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,12 @@ std.assertEqual(std.base64Decode('SGVsbG8gV29ybGQ='), 'Hello World') &&
std.assertEqual(std.base64Decode('SGVsbG8gV29ybA=='), 'Hello Worl') &&
std.assertEqual(std.base64Decode(''), '') &&

std.assertEqual(std.reverse([]), []) &&
std.assertEqual(std.reverse([1]), [1]) &&
std.assertEqual(std.reverse([1, 2]), [2, 1]) &&
std.assertEqual(std.reverse([1, 2, 3]), [3, 2, 1]) &&
std.assertEqual(std.reverse([[1, 2, 3]]), [[1, 2, 3]]) &&

std.assertEqual(std.sort([]), []) &&
std.assertEqual(std.sort([1]), [1]) &&
std.assertEqual(std.sort([1, 2]), [1, 2]) &&
Expand Down

0 comments on commit 50dd6b4

Please sign in to comment.