elvish icon indicating copy to clipboard operation
elvish copied to clipboard

Features of lists

Open gergelyk opened this issue 2 years ago • 2 comments

It feels like some common features of lists are missing:

  • del x[i] does the job with maps, but is not implemented for lists.
  • same with dissoc
  • seems like checking if element exists in list is a popular use case (operator in in python)
  • it would be nice to have support syntax like [$@x $@x[2..]] which would do:
var x = [10 20 30 40]
var s = $x[2..]
echo [$@x $@s]

gergelyk avatar Apr 11 '22 11:04 gergelyk

What should del x[$i] do? Shift the remaining list members left, so the list is one item shorter? But that would have a rather different semantics than the similar operation on a map. It could assign $nil to that list member, but if so, why not do the assignment yourself? It is clearer.

As for your suggested $@x[2..] syntax, well that is already syntactically ok, but it does something else:

⬥ var x = [abcd ABCD]
⬥ put $@x[2..]
⮕ cd
⮕ CD

This is consistent with syntax involving braced expressions:

⬥ put {abcd ABCD}[2..]
⮕ cd
⮕ CD

and so it seems wrong to change the semantics.

I agree that it would be nice to have syntax for taking a slice from a list and then explode it. As of now, you must write (all $x[2...]) to get what you want. The difficult thing is to come up with a good syntax that fits well with other syntax of the language. If we make up new syntax for everything that we might like to be able to do, we could end up in perl hell. 😉

hanche avatar Apr 14 '22 08:04 hanche

Yes, shifting is what I mean. Idea is inspired by Python:

In [1]: x = [10, 20 ,30, 40]
In [2]: del x[2]
In [3]: x
Out[3]: [10, 20, 40]

I was not aware of the syntax issue that you mentioned. Trick with all does the job and allows for writing this inline. Thanks for the lesson. Let's don't change anything in this context.

gergelyk avatar Apr 14 '22 09:04 gergelyk

I feel deleting elements from a list and shifting all remaining elements isn't "primitive" enough an operation to warrant support from del. Apart from Python, a lot of languages don't have this in their standard library, so I'll leave it out for now unless a compelling reason arises later.

Testing whether a list contains an element is supported by has-value.

xiaq avatar Jan 16 '24 16:01 xiaq