Skip to content

Commit

Permalink
List: varargs for Append,Prepend,NewList
Browse files Browse the repository at this point in the history
  • Loading branch information
laher committed Dec 24, 2022
1 parent d78c769 commit 92e4330
Showing 1 changed file with 19 additions and 7 deletions.
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

0 comments on commit 92e4330

Please sign in to comment.