New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: std.flattenDeepArray Concatenate an array containing values and arrays into a single flattened array. #1082
feat: std.flattenDeepArray Concatenate an array containing values and arrays into a single flattened array. #1082
Conversation
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
a3d6e12
to
2aa9ce1
Compare
It should give an error in that case. It's supposed to take an array of arrays and return an array. |
2aa9ce1
to
eb1f94d
Compare
@sparkprime I believe you're right. It would be misleading given the function name. I want to propose a new function to the stdlib, |
eb1f94d
to
9398f91
Compare
I think you can write it more simply like this:
But actually I wonder why exclude null by default? If you remove the part that checks for null and returns |
BTW it wasn't clear to me if you wanted to support > 2 levels of nesting or whether the way you were recursively calling fold from within the accumulator would actually achieve that. |
@sparkprime practically speaking, that is basically my proposal, but your implementation has the added benefit that if a single value is fed, it turns it into an array of a single value. It could be useful in some cases where the input could be either null or array(s). I also like the approach you mentioned to strip nulls: I usually prefer to represent the absence of values differently, and tend to strip nulls from third party libraries outputs, or transform them before feeding to other functions, but I guess it is a preference of mine rather than a good reason to not keep them. By simplifying the function to not handle null values makes it simpler (as you said): local old_std = std;
local std = old_std {
flattenDeep(value):
if std.type(value) == 'array' then
[y for x in value for y in std.flattenDeep(x)]
else
[value],
{
simpleVal: std.flattenDeep(0),
simpleArr: std.flattenDeep([1, 2, 3]),
mix: std.flattenDeep([1, [2, 3]]),
mixWithNulls: std.flattenDeep([null, 1, null, [null, 2, null, 3, null]]),
deeper: std.flattenDeep([[null], [[1], null], [null, 2, [null, [3, null]], null]]),
} So thinking that not removing nulls would serve more purposes, I believe your suggestion will be more beneficial to the stdlib. |
1a5c906
to
0cd7338
Compare
0cd7338
to
7d8b015
Compare
In some ways |
Proposal
The function
compactArray
recursively flattens multiple arrays, eliminating null values.Example:
Outputs: