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

Syntactic sugar for elementwise operations between arrays #4106

Open
tapyu opened this issue May 9, 2024 · 2 comments
Open

Syntactic sugar for elementwise operations between arrays #4106

tapyu opened this issue May 9, 2024 · 2 comments
Labels
feature request New feature or request scripting About Typst's coding capabilities syntax About syntax, parsing, etc.

Comments

@tapyu
Copy link
Sponsor

tapyu commented May 9, 2024

Description

Consider

let a = (1,2,3)
let b = (4,5,6)

Inspired by Julia, I propose the following syntaxes:

  • a .+ b
  • a .- b
  • a .* b
  • a ./ b

For elementwise addition, subtraction, multiplication, and division, respectively.

Use Case

This is much simpler than, for instance

let a = (1,2,3)
let b = (4,5,6)

let c = a.zip((b)).map(x => x.sum())

Numerical calculations are where Typst can best outperform LaTeX, but some built-in operations are still required.

@tapyu tapyu added the feature request New feature or request label May 9, 2024
@tapyu tapyu closed this as completed May 9, 2024
@tapyu tapyu changed the title Syntactic sugar for elementwise arithmetic operations between arrays Syntactic sugar for elementwise operations between arrays May 10, 2024
@tapyu
Copy link
Sponsor Author

tapyu commented May 10, 2024

Some colleagues had warned me that a similar feature was already proposed in #3863, but I what I am proposing here is different. You said

Vectors are distinct enough from a general purpose collection type that this does not feel like something that should be built-in on arrays to me. I think such functionality is better left to packages.

What I am proposing here is not restricted to vectors.

For instance, if

#let a = ((1, 2),"Hello", 9)
#let b = ((4, 3)," World!",1)

then a .+ b would apply elementwise sum operation which would lead to array concatenation in the first two elements and arithmetic sum in the third element. This would also be equivalent to

a.zip(b).map(x => x.sum())

But if you ask me, I would say that

a .+ b

is much simpler and yet generic as this syntax is not restricted to vector operations. As long as these operations is defined between the data types, we can use it for whatever element within the array.

@tapyu tapyu reopened this May 10, 2024
@Enivex
Copy link
Collaborator

Enivex commented May 11, 2024

generic

The only (mostly) generic operation would be .+.

@Enivex Enivex added scripting About Typst's coding capabilities syntax About syntax, parsing, etc. labels May 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request New feature or request scripting About Typst's coding capabilities syntax About syntax, parsing, etc.
Projects
None yet
Development

No branches or pull requests

2 participants