Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions doc/ref/stdlib.html
Original file line number Diff line number Diff line change
Expand Up @@ -1399,6 +1399,34 @@ <h4 id="filterMap">std.filterMap(filter_func, map_func, arr)</h4>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<h4 id="flatMap">std.flatMap(func, arr)</h4>
</div>
<div style="clear: both"></div>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
<div class="panel">
<p>Apply the given function to every element of the array to form a new array then flatten the result.
It can be thought of as a generalized map, where each element can get mapped to 0, 1 or more elements.
</p>
<p>
Example: <code>std.flatMap(function(x) [x, x], [1, 2, 3])</code> yields <code>[1, 1, 2, 2, 3, 3])</code>.
</p>
<p>
Example: <code>std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3])</code> yields <code>[1, 3])</code>.
</p>
<p>
Example: <code>std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3])</code> yields <code>[3, 2, 9, 6])</code>.
</p>
</div>
<div style="clear: both"></div>
</div>
</div>

<div class="hgroup">
<div class="hgroup-inline">
Expand Down
9 changes: 9 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,15 @@ limitations under the License.
else
{ [k]: func(k, obj[k]) for k in std.objectFields(obj) },

flatMap(func, arr)::
if std.type(func) != 'function' then
error ('std.flatMap first param must be function, got ' + std.type(func))
else if std.isArray(arr) then
std.flattenArrays(std.makeArray(std.length(arr), function(i) func(arr[i])))
else if std.isString(arr) then
std.join('', std.makeArray(std.length(arr), function(i) func(arr[i])))
else error ('std.flatMap second param must be array / string, got ' + std.type(arr)),

join(sep, arr)::
local aux(arr, i, first, running) =
if i >= std.length(arr) then
Expand Down
4 changes: 4 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,10 @@ std.assertEqual(std.mapWithIndex(function(i, x) x * i, std.filter(function(x) x
std.assertEqual(std.mapWithKey(function(k, o) k + o, {}), {}) &&
std.assertEqual(std.mapWithKey(function(k, o) k + o, { a: 1, b: 2 }), { a: 'a1', b: 'b2' }) &&

std.assertEqual(std.flatMap(function(x) [x, x], [1, 2, 3]), [1, 1, 2, 2, 3, 3]) &&
std.assertEqual(std.flatMap(function(x) if x == 2 then [] else [x], [1, 2, 3]), [1, 3]) &&
std.assertEqual(std.flatMap(function(x) if x == 2 then [] else [x * 3, x * 2], [1, 2, 3]), [3, 2, 9, 6]) &&

std.assertEqual(std.filterMap(function(x) x >= 0, function(x) x * x, [-3, -2, -1, 0, 1, 2, 3]), [0, 1, 4, 9]) &&

std.assertEqual(std.foldl(function(x, y) [x, y], [], 'foo'), 'foo') &&
Expand Down