From 6be994878d30616b470f54a1267798dde857ca17 Mon Sep 17 00:00:00 2001 From: Hercules Merscher Date: Sun, 7 May 2023 13:19:49 +0200 Subject: [PATCH 1/2] std.compactArray recursively flattens multiple arrays --- doc/_stdlib_gen/stdlib-content.jsonnet | 14 ++++++++++++++ stdlib/std.jsonnet | 10 ++++++++++ test_suite/stdlib.jsonnet | 5 +++++ 3 files changed, 29 insertions(+) diff --git a/doc/_stdlib_gen/stdlib-content.jsonnet b/doc/_stdlib_gen/stdlib-content.jsonnet index e9554a88c..4d8783421 100644 --- a/doc/_stdlib_gen/stdlib-content.jsonnet +++ b/doc/_stdlib_gen/stdlib-content.jsonnet @@ -1270,6 +1270,20 @@ local html = import 'html.libsonnet'; }, ], }, + { + name: 'compactArrays', + params: ['arrs'], + availableSince: 'upcoming', + description: ||| + Concatenate an array of arrays into a single flattened array, removing null values. + |||, + examples: [ + { + input: 'std.compactArrays([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])', + output: std.compactArrays([1, 2, 3, 4, 5, 6, 7, 8]), + }, + ], + }, { name: 'reverse', params: ['arrs'], diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index 0349fb5df..cf0af19d2 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -865,6 +865,16 @@ limitations under the License. flattenArrays(arrs):: std.foldl(function(a, b) a + b, arrs, []), + compactArray(arrs):: + local foldable = function(accumulator, value) + if std.isArray(value) then + accumulator + std.compactArray(value) + else if value == null then + accumulator + else + accumulator + [value]; + std.foldl(foldable, arrs, []), + manifestIni(ini):: local body_lines(body) = std.join([], [ diff --git a/test_suite/stdlib.jsonnet b/test_suite/stdlib.jsonnet index d69c763da..1a5614d31 100644 --- a/test_suite/stdlib.jsonnet +++ b/test_suite/stdlib.jsonnet @@ -313,6 +313,11 @@ std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') && std.assertEqual(std.flattenArrays([[1, 2, 3], [4, 5, 6], []]), [1, 2, 3, 4, 5, 6]) && +std.assertEqual(std.compactArray([]), []) && +std.assertEqual(std.compactArray([1, 2, 3]), [1, 2, 3]) && +std.assertEqual(std.compactArray([1, [2, 3]]), [1, 2, 3]) && +std.assertEqual(std.compactArray([[1], [2, 3], []]), [1, 2, 3]) && + std.assertEqual( std.manifestIni({ main: { a: '1', b: '2' }, From 7d8b01530793b91bdb2eb744bc93de1e50aceebd Mon Sep 17 00:00:00 2001 From: Hercules Merscher Date: Sat, 3 Jun 2023 12:16:30 +0200 Subject: [PATCH 2/2] Changing compactArray function to flattenDeepArray --- doc/_stdlib_gen/stdlib-content.jsonnet | 10 +++++----- stdlib/std.jsonnet | 14 +++++--------- test_suite/stdlib.jsonnet | 8 ++++---- 3 files changed, 14 insertions(+), 18 deletions(-) diff --git a/doc/_stdlib_gen/stdlib-content.jsonnet b/doc/_stdlib_gen/stdlib-content.jsonnet index 4d8783421..ada55c5ca 100644 --- a/doc/_stdlib_gen/stdlib-content.jsonnet +++ b/doc/_stdlib_gen/stdlib-content.jsonnet @@ -1271,16 +1271,16 @@ local html = import 'html.libsonnet'; ], }, { - name: 'compactArrays', - params: ['arrs'], + name: 'flattenDeepArray', + params: ['value'], availableSince: 'upcoming', description: ||| - Concatenate an array of arrays into a single flattened array, removing null values. + Concatenate an array containing values and arrays into a single flattened array. |||, examples: [ { - input: 'std.compactArrays([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])', - output: std.compactArrays([1, 2, 3, 4, 5, 6, 7, 8]), + input: 'std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]])', + output: std.flattenDeepArray([[1, 2], [], [3, [4]], [[5, 6, [null]], [7, 8]]]), }, ], }, diff --git a/stdlib/std.jsonnet b/stdlib/std.jsonnet index cf0af19d2..393fd3321 100644 --- a/stdlib/std.jsonnet +++ b/stdlib/std.jsonnet @@ -865,15 +865,11 @@ limitations under the License. flattenArrays(arrs):: std.foldl(function(a, b) a + b, arrs, []), - compactArray(arrs):: - local foldable = function(accumulator, value) - if std.isArray(value) then - accumulator + std.compactArray(value) - else if value == null then - accumulator - else - accumulator + [value]; - std.foldl(foldable, arrs, []), + flattenDeepArray(value):: + if std.isArray(value) then + [y for x in value for y in std.flattenDeepArray(x)] + else + [value], manifestIni(ini):: local body_lines(body) = diff --git a/test_suite/stdlib.jsonnet b/test_suite/stdlib.jsonnet index 1a5614d31..78f95b717 100644 --- a/test_suite/stdlib.jsonnet +++ b/test_suite/stdlib.jsonnet @@ -313,10 +313,10 @@ std.assertEqual(std.lines(['a', null, 'b']), 'a\nb\n') && std.assertEqual(std.flattenArrays([[1, 2, 3], [4, 5, 6], []]), [1, 2, 3, 4, 5, 6]) && -std.assertEqual(std.compactArray([]), []) && -std.assertEqual(std.compactArray([1, 2, 3]), [1, 2, 3]) && -std.assertEqual(std.compactArray([1, [2, 3]]), [1, 2, 3]) && -std.assertEqual(std.compactArray([[1], [2, 3], []]), [1, 2, 3]) && +std.assertEqual(std.flattenDeepArray([]), []) && +std.assertEqual(std.flattenDeepArray([1, 2, 3]), [1, 2, 3]) && +std.assertEqual(std.flattenDeepArray([1, [2, 3]]), [1, 2, 3]) && +std.assertEqual(std.flattenDeepArray([[1], [2, 3], [[null]]]), [1, 2, 3, null]) && std.assertEqual( std.manifestIni({