Skip to content
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

Call a function on every element of a complex value #4610

Open
philrz opened this issue May 25, 2023 · 3 comments · Fixed by #4806 or #4856
Open

Call a function on every element of a complex value #4610

philrz opened this issue May 25, 2023 · 3 comments · Fixed by #4806 or #4856

Comments

@philrz
Copy link
Contributor

philrz commented May 25, 2023

Repro is with Zed commit efdcd27.

Imagine you have an complex value such as the array of numbers [1, 4, 9, 16] and you want to execute some function on each element to produce a new array, such as multiplying each element by two. To accomplish this in Zed today requires a multi-part idiom:

$ zq -version
Version: v1.8.0-5-gefdcd272

$ echo '[1, 4, 9, 16]' | zq -z 'over this | yield this*2 | collect(this)' -
[2,8,18,32]

Compare this to something like JavaScript's map() (not to be confused with Zed's map type) which can perform the equivalent in one shot. We've recognized Zed should offer a similar shorthand.

While in JavaScript we see this approach applied to array types, it seems that in Zed it could be used for not only arrays but also sets, maybe maps, and perhaps into records (recursively?) though I'm not sure. Implementation TBD, I suppose.

@philrz philrz changed the title Call a function on every element of a complex type Call a function on every element of a complex value May 30, 2023
mattnibs added a commit that referenced this issue Oct 11, 2023
This is a function that applies a function to every element in an array
or set value.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Oct 12, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
@philrz philrz linked a pull request Oct 17, 2023 that will close this issue
mattnibs added a commit that referenced this issue Oct 19, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Nov 1, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
mattnibs added a commit that referenced this issue Nov 3, 2023
The map function that applies a function to every element in an array or set
value.

Also: rename the map aggregator to collect_map to avoid collision with
this new function.

Closes #4610
@nwt
Copy link
Member

nwt commented Nov 3, 2023

#4806 implements a solution for arrays and sets but for not maps or records.

@nwt nwt reopened this Nov 3, 2023
@philrz philrz linked a pull request Nov 3, 2023 that will close this issue
@philrz
Copy link
Contributor Author

philrz commented Nov 3, 2023

Here's verification in Zed commit 3a40788 of the map() function added in #4806 that handles this for arrays and sets.

Revisiting the original example above, instead of the over / collect() combination we had to use previously, now we can do:

$ zq -version
Version: v1.10.0-31-g3a40788b

$ echo '[1, 4, 9, 16]' | zq -z 'func mult_two(x): ( x * 2 ) yield map(this, mult_two)' -
[2,8,18,32]

Of course, built-in functions work as well.

$ echo '[1, 4, 9, 16]' | zq -z 'yield map(this, string)' -
["1","4","9","16"]

However, while verifying, I did spot what I think is a bug that's now tracked in #4855. I've also proposed some minor doc fixes in #4856.

Thanks @mattnibs!

@philrz philrz reopened this Nov 4, 2023
@philrz
Copy link
Contributor Author

philrz commented Nov 7, 2023

Related: When discussing the map() function that was added in #4806 we reached some consensus about adding syntax to the language for invoking functions the way methods are invoked in other languages, perhaps using |>.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants