Skip to content

Commit 9dd6034

Browse files
committed
Add std.uniq, fix #57
1 parent 546e015 commit 9dd6034

File tree

4 files changed

+30
-1
lines changed

4 files changed

+30
-1
lines changed

doc/stdlib.html

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -511,6 +511,11 @@ <h4>std.sort(arr)</h4>
511511
<p>Sorts the array using the <= operator.</p>
512512

513513

514+
<h4>std.uniq(arr)</h4>
515+
516+
<p>Removes successive duplicates. When given a sorted array, removes all duplicates.</p>
517+
518+
514519
<h3>Base 64</h3>
515520

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

doc/stdlib.html.jinja

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,6 +322,11 @@ is suitable for constructing bash scripts and the like.</p>
322322
<p>Sorts the array using the <= operator.</p>
323323

324324

325+
<h4>std.uniq(arr)</h4>
326+
327+
<p>Removes successive duplicates. When given a sorted array, removes all duplicates.</p>
328+
329+
325330
<h3>Base 64</h3>
326331

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

std.jsonnet

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -765,4 +765,13 @@ limitations under the License.
765765
local right = std.filter(function(x) x > pivot, rest);
766766
std.sort(left) + [pivot] + std.sort(right),
767767

768+
uniq(arr)::
769+
local f(a, b) =
770+
if std.length(a) == 0 then
771+
[b]
772+
else if a[std.length(a) - 1] == b then
773+
a
774+
else
775+
a + [b];
776+
std.foldl(f , arr, []),
768777
}

test_suite/stdlib.jsonnet

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -255,11 +255,21 @@ std.assertEqual(std.sort([]), []) &&
255255
std.assertEqual(std.sort([1]), [1]) &&
256256
std.assertEqual(std.sort([1, 2]), [1, 2]) &&
257257
std.assertEqual(std.sort([2, 1]), [1, 2]) &&
258-
std.assertEqual(std.sort(["2", "1"]), ["1", "2"]) &&
258+
std.assertEqual(std.sort(["1", "2"]), ["1", "2"]) &&
259259
std.assertEqual(std.sort(["2", "1"]), ["1", "2"]) &&
260260
std.assertEqual(
261261
std.sort(["The", "rain", "in", "spain", "falls", "mainly", "on", "the", "plain."]),
262262
["The", "falls", "in", "mainly", "on", "plain.", "rain", "spain", "the"]) &&
263263

264+
std.assertEqual(std.uniq([]), []) &&
265+
std.assertEqual(std.uniq([1]), [1]) &&
266+
std.assertEqual(std.uniq([1, 2]), [1, 2]) &&
267+
std.assertEqual(std.uniq(["1", "2"]), ["1", "2"]) &&
268+
std.assertEqual(
269+
std.uniq(["The", "falls", "in", "mainly", "on", "plain.", "rain", "spain", "the"]),
270+
["The", "falls", "in", "mainly", "on", "plain.", "rain", "spain", "the"]) &&
271+
std.assertEqual(
272+
std.uniq(["ant", "bat", "cat", "dog", "dog", "elephant", "fish", "fish", "giraffe"]),
273+
["ant", "bat", "cat", "dog", "elephant", "fish", "giraffe"]) &&
264274

265275
true

0 commit comments

Comments
 (0)