Skip to content

Consider adding array.flatten built-in function #8226

@anderseknert

Description

@anderseknert

A few of the builtins that deal with collections in some way have a "normal" version that accepts 2 arguments, and a _n version that takes an array of any number of items. A notable exception is array.concat, which currently has no array.concat_n sibling for cases where more than arrays should be merged. It would be nice to be able to replace nested array.concat calls with a single call to array.concat_n. Implementation-wise I believe this should be simple enough not to put too much of a burden on the maintainers of alternative OPA implementations either.

Should we add an array.concat_n built-in function?

If we do, I'm entertaining the idea of perhaps having it accept not just an array of arrays (but that would of course work as expected), but an array of any, where non-array items simply get appended to the output array. e.g:

array.concat_n([[1, 2], [3], [4]]) # => [1, 2, 3, 4]
array.concat_n([[1, 2], [3], 4])   # => [1, 2, 3, 4]
array.concat_n([[1], 2, [3, 4]])   # => [1, 2, 3, 4]

This would make it quite ergonomic to build arrays out of whatever parts you've got, without having to create a wrapper array around anything that isn't already one. Looking at the array.concat calls in Regal's policies and the majority looks like this: array.concat([node], path) (i.e. one or both arguments wrapped in []).

I haven't given this too much thought yet though, so perhaps that approach comes with some drawbacks I haven't considered? Happy to hear what others think!

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions