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 push, pop, shift, & unshift functions. #588

Open
wants to merge 2 commits into
base: master
Choose a base branch
from

Conversation

FGasper
Copy link
Contributor

@FGasper FGasper commented Feb 6, 2025

This allows code like the following:

foo := []int{1, 2, 3}
lo.Push(&foo, 4)

… which is a bit more concise than Go’s familiar foo = append(foo, 4) idiom.

If the maintainers agree that these are good additions, I’ll add tests before merging.

Thanks!

Copy link

codecov bot commented Feb 6, 2025

Codecov Report

Attention: Patch coverage is 0% with 44 lines in your changes missing coverage. Please review.

Project coverage is 92.98%. Comparing base (70b763e) to head (27038f3).

Files with missing lines Patch % Lines
slice.go 0.00% 44 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master     #588      +/-   ##
==========================================
- Coverage   94.27%   92.98%   -1.29%     
==========================================
  Files          18       18              
  Lines        3178     3222      +44     
==========================================
  Hits         2996     2996              
- Misses        170      214      +44     
  Partials       12       12              
Flag Coverage Δ
unittests 92.98% <0.00%> (-1.29%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@samber
Copy link
Owner

samber commented Feb 16, 2025

Please move this work to mutable/ directory.

// Equivalent to, but more concise than:
//
// collection = append(elements, collection...)
func Unshift[T any, Slice ~[]T](collection *Slice, elements ...T) int {
Copy link
Owner

Choose a reason for hiding this comment

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

In respect with go semantic, i would replace Push/unshift by Append/Preppend.


func verifyNonEmpty[T any](slice []T) {
if len(slice) == 0 {
panic("Cannot remove element from empty slice!")
Copy link
Owner

Choose a reason for hiding this comment

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

I don't really like calls to panic in such utilitary lib, any other idea?

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.

2 participants