Skip to content

Commit 546e015

Browse files
committed
Add std.sort, fix #56
1 parent b8e56c0 commit 546e015

File tree

4 files changed

+41
-3
lines changed

4 files changed

+41
-3
lines changed

doc/stdlib.html

+5
Original file line numberDiff line numberDiff line change
@@ -506,6 +506,11 @@ <h4>std.flattenArrays(arrs)</h4>
506506
<p>Concatenate an array of arrays into a single array.</p>
507507

508508

509+
<h4>std.sort(arr)</h4>
510+
511+
<p>Sorts the array using the <= operator.</p>
512+
513+
509514
<h3>Base 64</h3>
510515

511516
<h4>std.base64(v)</h4>

doc/stdlib.html.jinja

+5
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ is suitable for constructing bash scripts and the like.</p>
317317
<p>Concatenate an array of arrays into a single array.</p>
318318

319319

320+
<h4>std.sort(arr)</h4>
321+
322+
<p>Sorts the array using the <= operator.</p>
323+
324+
320325
<h3>Base 64</h3>
321326

322327
<h4>std.base64(v)</h4>

std.jsonnet

+13-2
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ limitations under the License.
2626

2727
toString(a)::
2828
if std.type(a) == "string" then a else "" + a,
29-
29+
3030
substr(str, from, len)::
3131
if std.type(str) != "string" then
3232
error "substr first parameter should be a string, got " + std.type(str)
@@ -751,7 +751,18 @@ limitations under the License.
751751

752752
base64Decode(str)::
753753
local bytes = std.base64DecodeBytes(str);
754-
std.join("", std.map(function(b) std.char(b), bytes))
754+
std.join("", std.map(function(b) std.char(b), bytes)),
755755

756+
// Quicksort
757+
sort(arr)::
758+
local l = std.length(arr);
759+
if std.length(arr) == 0 then
760+
[]
761+
else
762+
local pivot = arr[0];
763+
local rest = std.makeArray(l - 1, function(i) arr[i + 1]);
764+
local left = std.filter(function(x) x <= pivot, rest);
765+
local right = std.filter(function(x) x > pivot, rest);
766+
std.sort(left) + [pivot] + std.sort(right),
756767

757768
}

test_suite/stdlib.jsonnet

+18-1
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,17 @@ See the License for the specific language governing permissions and
1414
limitations under the License.
1515
*/
1616

17+
// This file tests functions from the standard library (std.jsonnet and builtins).
18+
1719
// Can capture std from another file.
1820
std.assertEqual((import "lib/capture_std_func.jsonnet")().sqrt(4), 2) &&
1921

2022
// Each import has its own std.
21-
std.assertEqual(local std = { sqrt: function(x) x }; local lib = import "lib/capture_std.jsonnet"; lib.sqrt(4), 2) &&
23+
std.assertEqual(
24+
local std = { sqrt: function(x) x };
25+
local lib = import "lib/capture_std.jsonnet";
26+
lib.sqrt(4),
27+
2) &&
2228

2329
// Now, test each std library function in turn.
2430

@@ -245,4 +251,15 @@ std.assertEqual(std.base64Decode("SGVsbG8gV29ybGQ="), "Hello World") &&
245251
std.assertEqual(std.base64Decode("SGVsbG8gV29ybA=="), "Hello Worl") &&
246252
std.assertEqual(std.base64Decode(""), "") &&
247253

254+
std.assertEqual(std.sort([]), []) &&
255+
std.assertEqual(std.sort([1]), [1]) &&
256+
std.assertEqual(std.sort([1, 2]), [1, 2]) &&
257+
std.assertEqual(std.sort([2, 1]), [1, 2]) &&
258+
std.assertEqual(std.sort(["2", "1"]), ["1", "2"]) &&
259+
std.assertEqual(std.sort(["2", "1"]), ["1", "2"]) &&
260+
std.assertEqual(
261+
std.sort(["The", "rain", "in", "spain", "falls", "mainly", "on", "the", "plain."]),
262+
["The", "falls", "in", "mainly", "on", "plain.", "rain", "spain", "the"]) &&
263+
264+
248265
true

0 commit comments

Comments
 (0)