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 append
method to array
#3970
Conversation
For two arrays, this can indeed be accomplished using the + operator. |
Nope, it works for any number of arrays already. |
This operation can only be performed on two arrays. For example, this works: #let res = (1, 2) + (3, 4) + (5, 6) + (7, 8); But this is not: #let arr = ((3, 4), (5, 6), (7, 8));
#let res = (1, 2) + ..arr; |
I updated the description, where I described in more detail why this feature is worth adding. |
This is already possible through #((1, 2), (3, 4)).join() |
Yes, this is indeed possible too. You can also do this: #let arr = ((3, 4), (5, 6), (7, 8));
#let res = ((1, 2), ..arr); But what you give are simple examples. However, I think we need to pay attention not only to the fact that #let res = arr
.map(x => some_function_with_long_name(x))
.filter(x => other_function_with_long_name(x))
.append(..arr2.map(x => x * 2)) // <-- In the middle of the chain!
.map(x => additional_function(x)); In addition, functions similar to join exist in other languages. For example, in the same Gleam there are methods similar to join: concat, flatten. That is, all |
One thing that I'm not a big fan of with Overall, I think sometimes things just break up a chain and I'm not sure whether it's desirable to add new separate ways to write all those cases. Adding the occasional let binding isn't that bad. |
As I understand correctly, from EcoVec doc, what you say takes place if we have another owner of the vector. If we use this method in a chain of methods (which is usually done), then this method receives a vector by value and returns it. Therefore, this case, i.e. returning a new vector can simply be specified in the documentation.
Well, I don’t know what to say here. I can only point out that all functional languages contain this function and it is one of the key functions, along with Besides, it's just an array method - one of more than 10 types! Adding this function does not change the syntax of the language and does not introduce new constructs. Analogues of similar functions exist in both ancient languages (hello Lisp) and completely new ones (Gleam). This suggests that this function/method is ergonomic, in demand and time-tested (for more than 60 years). There are no other arguments left 😅. |
What I meant isn't related to
If there was an
This would be an inconsistency people could trip over.
I'm not arguing that the problem is the addition of another method, but that this method would introducy inconsistency. Note that in Rust,
What set of methods makes sense and is consistent varies by language. Gleam for instance doesn't seem to have a mutable push operation (from a quick look). |
How does Typst pass arguments by value? Does Typst always clone it? I expected that it would simply transfer the array itself, and cloning would only occur if we had a second owner. And this can be described in the documentation? Something like that the method is optimized for use in method chains and when applied, it accepts an array, and if there is no second reference to this array, then it returns the same received array, with new elements added. If there is an additional reference to an array, it returns a cloned array with new elements added. It just seems that in Typst you can’t return |
Typst does use clone-on-write internally, but that's not the point I'm trying to make. It is about how the API is used by a user: Typst's |
I understand you, I just don’t understand why |
I think append could be associated with push because it (a) does something quite similar to push and (b) there is no clear consensus among languages whether it is in-place or not. With map and filter on the other hand, people that are familiar with those methods will know immediately that they return something new. With Ultimately this is a very subjective topic, but based on my naming concerns and the discussion and comments from other people, I don't see enough motivation to merge this. Thank you for your PR and effort still! |
Add
append
method to array that takes several arrays and creates a new array that sequentially contains all the elements of the provided arrays.User-facing API
Analogues in other languages
Why is this function needed?
This function is a standard function on lists in almost all programming languages.
I really missed this feature when working with path, that is, when translating vertices from
typst
format tosvg
format. It is very inconvenient that in order to connect two arrays of arrays you have to use afor
loop. This function, in turn, allows you to ergonomically create a chain of functions, for example like this: