aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorOskar Haarklou Veileborg <ohv1020@hotmail.com>2023-01-12 10:59:36 +0100
committerOskar Haarklou Veileborg <ohv1020@hotmail.com>2023-01-12 10:59:36 +0100
commitbdfdabc970ef286de9390ff0ab7740ff1145d388 (patch)
tree2e52253411977a11520a26fa368a214c9671c5bb
parentsets & maps: remove varargs APIs & SetMany variants (diff)
downloadpds-bdfdabc970ef286de9390ff0ab7740ff1145d388.tar.gz
pds-bdfdabc970ef286de9390ff0ab7740ff1145d388.tar.xz
list: fix Append & Prepend, remove varargs support
Similar reason for the API change as the previous commit.
-rw-r--r--immutable.go16
-rw-r--r--immutable_test.go13
2 files changed, 17 insertions, 12 deletions
diff --git a/immutable.go b/immutable.go
index e43bc70..666bb49 100644
--- a/immutable.go
+++ b/immutable.go
@@ -117,12 +117,8 @@ 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(values ...T) *List[T] {
- other := l.clone()
- for _, value := range values {
- other.append(value, true)
- }
- return other
+func (l *List[T]) Append(value T) *List[T] {
+ return l.append(value, false)
}
func (l *List[T]) append(value T, mutable bool) *List[T] {
@@ -145,12 +141,8 @@ func (l *List[T]) append(value T, mutable bool) *List[T] {
}
// 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) *List[T] {
+ return l.prepend(value, false)
}
func (l *List[T]) prepend(value T, mutable bool) *List[T] {
diff --git a/immutable_test.go b/immutable_test.go
index a878a78..9a0de37 100644
--- a/immutable_test.go
+++ b/immutable_test.go
@@ -223,6 +223,19 @@ func TestList(t *testing.T) {
}
})
+ t.Run("AppendImmutable", func(t *testing.T) {
+ outer_l := NewList[int]()
+ for N := 0; N < 1_000; N++ {
+ l1 := outer_l.Append(0)
+ outer_l.Append(1)
+ if actual := l1.Get(N); actual != 0 {
+ t.Fatalf("Append mutates list with %d elements. Got %d instead of 0", N, actual)
+ }
+
+ outer_l = outer_l.Append(0)
+ }
+ })
+
RunRandom(t, "Random", func(t *testing.T, rand *rand.Rand) {
l := NewTList()
for i := 0; i < 100000; i++ {