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 elements of a slice [] T #240

Open
ArnoldoR opened this issue Jan 23, 2024 · 2 comments
Open

Add elements of a slice [] T #240

ArnoldoR opened this issue Jan 23, 2024 · 2 comments

Comments

@ArnoldoR
Copy link

Add elements of a slice [] T to data structures.

set.Add(slice)
list.Add(slice)
stack.Push(slice)

etc

@mxmlkzdh
Copy link

mxmlkzdh commented Sep 9, 2024

Is there still any interest in this? I can implement these if necessary and send a PR.

@jdimaria
Copy link

jdimaria commented Dec 2, 2024

Since Set and List's definitions of Add accept vararg params (values ...T), you can already call these with a slice, no?

set.Add(slice...)

However, it would still be nice to update stack.Push()'s signature to also be variadic:

// Before
Push(value T)
// After
Push(values ...T)

This should be backwards compatible, since the underlying data structures used in arraystack and linkedliststack already support this behavior

EDIT:
The main complication here is that arraystack.Push is append-based, while linkedliststack.Push is prepend-based, so even though both implementations currently satisfy stack.Push, in a variadic implementation, elements would be added in opposite order between the two:

arraystack.Push(1, 2, 3)
top := arraystack.Pop() // top == 3

linkedliststack.Push(1, 2, 3)
top = linkedliststack.Pop() // top == 1

Perhaps that's intended (as otherwise, writing around this case would cause linkedliststack.Push to not align with the behavior of linkedlist.Prepend), but it does make using the broader Stack interface less intuitive:

stack.Push(1, 2, 3)
top := stack.Pop() // top varies depending on which stack implementation is used

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

No branches or pull requests

3 participants