diff options
author | Ben Johnson <benbjohnson@yahoo.com> | 2023-01-17 18:40:25 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-01-17 18:40:25 -0700 |
commit | d6cf261856fd48f51b6afa23ed4bcb5342c4a014 (patch) | |
tree | ab8c4c0cf317173da970218d5970e4ac18bb31a7 /immutable.go | |
parent | Merge pull request #35 from laher/sets-maps-append-multi (diff) | |
parent | Fix random seed for TestRandom (diff) | |
download | pds-d6cf261856fd48f51b6afa23ed4bcb5342c4a014.tar.gz pds-d6cf261856fd48f51b6afa23ed4bcb5342c4a014.tar.xz |
Merge pull request #39 from BarrensZeppelin/fix-sets
Ensure immutability of sets, maps & lists
Diffstat (limited to 'immutable.go')
-rw-r--r-- | immutable.go | 37 |
1 files changed, 4 insertions, 33 deletions
diff --git a/immutable.go b/immutable.go index b3d7dcb..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] { @@ -752,18 +744,6 @@ func (m *Map[K, V]) Set(key K, value V) *Map[K, V] { return m.set(key, value, false) } -// SetMany returns a map with the keys set to the new values. nil values are allowed. -// -// This function will return a new map even if the updated value is the same as -// the existing value because Map does not track value equality. -func (m *Map[K, V]) SetMany(entries map[K]V) *Map[K, V] { - n := m.clone() - for k, v := range entries { - n.set(k, v, true) - } - return n -} - func (m *Map[K, V]) set(key K, value V, mutable bool) *Map[K, V] { // Set a hasher on the first value if one does not already exist. hasher := m.hasher @@ -1642,15 +1622,6 @@ func (m *SortedMap[K, V]) Set(key K, value V) *SortedMap[K, V] { return m.set(key, value, false) } -// SetMany returns a map with the keys set to the new values. -func (m *SortedMap[K, V]) SetMany(entries map[K]V) *SortedMap[K, V] { - n := m.clone() - for k, v := range entries { - n.set(k, v, true) - } - return n -} - func (m *SortedMap[K, V]) set(key K, value V, mutable bool) *SortedMap[K, V] { // Set a comparer on the first value if one does not already exist. comparer := m.comparer |