Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

List: varargs for Append,Prepend,NewList #33

Merged
merged 1 commit into from
Dec 26, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 19 additions & 7 deletions immutable.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,14 @@ type List[T any] struct {
}

// NewList returns a new empty instance of List.
func NewList[T any]() *List[T] {
return &List[T]{
func NewList[T any](values ...T) *List[T] {
l := &List[T]{
root: &listLeafNode[T]{},
}
for _, value := range values {
l.append(value, true)
}
return l
}

// clone returns a copy of the list.
Expand Down Expand Up @@ -113,8 +117,12 @@ func (l *List[T]) set(index int, value T, mutable bool) *List[T] {
}

// Append returns a new list with value added to the end of the list.
func (l *List[T]) Append(value T) *List[T] {
return l.append(value, false)
func (l *List[T]) Append(values ...T) *List[T] {
other := l.clone()
for _, value := range values {
other.append(value, true)
}
return other
}

func (l *List[T]) append(value T, mutable bool) *List[T] {
Expand All @@ -136,9 +144,13 @@ func (l *List[T]) append(value T, mutable bool) *List[T] {
return other
}

// Prepend returns a new list with value added to the beginning of the list.
func (l *List[T]) Prepend(value T) *List[T] {
return l.prepend(value, false)
// Prepend returns a new list with value(s) added to the beginning of the list.
func (l *List[T]) Prepend(values ...T) *List[T] {
other := l.clone()
for i := len(values) - 1; i >= 0; i-- {
other.prepend(values[i], true)
}
return other
}

func (l *List[T]) prepend(value T, mutable bool) *List[T] {
Expand Down