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

Add exact argument to array.zip #4030

Merged
merged 6 commits into from May 6, 2024
Merged

Add exact argument to array.zip #4030

merged 6 commits into from May 6, 2024

Conversation

T0mstone
Copy link
Contributor

@T0mstone T0mstone commented Apr 28, 2024

This adds a named argument exact: bool (default false) to array.zip that, when enabled, checks whether all given arrays are actually the same length and errors if they are not.

Motivation

I had a situation where a table was missing a row because I constructed it using array.zip and I accidentally made one of my columns shorter than the others. This lets users easily enforce the abscence of such a bug.

Alternatives

Of course, this behavior can be implemented in typst already:

let checked-zip(first, ..arrs) = {
  for arr in arrs.pos() {
    if arr.len() != first.len() {
      panic("arguments have different lengths")
    }
  }
  array.zip(first, ..arrs)
}

However, I think that this is still something important enough to warrant inclusion in the method itself,
and doing it natively also allows for better error messages.

Documentation and Tests

I haven't written tests and documentation for this yet to save on effort in case this PR is rejected. I will add them if it is approved.

Copy link
Member

@laurmaedje laurmaedje left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it makes sense to have this.

crates/typst/src/foundations/array.rs Outdated Show resolved Hide resolved
crates/typst/src/foundations/array.rs Outdated Show resolved Hide resolved
crates/typst/src/foundations/array.rs Outdated Show resolved Hide resolved
crates/typst/src/foundations/array.rs Outdated Show resolved Hide resolved
@T0mstone
Copy link
Contributor Author

T0mstone commented May 1, 2024

Should be good to merge now.

@T0mstone
Copy link
Contributor Author

T0mstone commented May 1, 2024

Ok, now it should be good.

@laurmaedje laurmaedje added this pull request to the merge queue May 6, 2024
@laurmaedje
Copy link
Member

Thanks, looks great!

Merged via the queue into typst:main with commit c4c53ab May 6, 2024
6 checks passed
@T0mstone T0mstone deleted the zip-exact branch May 7, 2024 15:00
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants